zoukankan      html  css  js  c++  java
  • 完全二叉树 数组存储 层次构造

    对于完全二叉树,如果将其中的元素按层次遍历顺序存放入一个一维数组中:
    设数组大小为n(节点数为n),节点标号(key)为数组下标i,即0,1,2,3,4,,,
    那么:
    1.完全二叉树的高度为: ceil(log2(n+1))
    2.i = 0: 根节点,root,无父节点。
    i >= 1: 父节点为 floor((i-1)/2);
    3.若2*i<n-1: 节点i的左子女为 2*i + 1
    若2*i<n-2: 节点i的右子女为 2*i + 2
    4.若节点编号 i 为奇数,i != 1, 它处于右兄弟位置,则它的左兄弟为节点 i-1
    5.若节点编号 i 为偶数,i != 1, 它处于左兄弟位置,则它的右兄弟为节点 i+1
    6.节点i所在的层次为 floor(log2(i-1))+1;

    Java实现:
     1  
     2 /**
     3  * Created by XuTao on 2018/11/18 11:54
     4  * 完全二叉树:
     5  * 用数组按层次序排列
     6  *
     7  */
     8 
     9  
    10 public class BT {
    11     private Node root;
    12     private int [] data;
    13     public BT_ArrayStore(int []a){
    14         this.data =a;
    15         this.root = create(a,0);
    16     }
    17 
    18     public int getHeight(){
    19         return (int)Math.floor(Math.log(data.length)/Math.log(2))+1;  //这里涉及到Math,易错
    20     }
    21     public int getFather(int i){
    22         if (i==0) return 0;
    23         return (int)(Math.floor((i-1)/2));
    24     }
    25 
    26     public int getLeft(int i){
    27         return 2*i+1;
    28     }
    29     public int getRight(int i){
    30         return 2*i+2;
    31     }
    32 
    33     public int getBrother(int i){
    34         if (i%2==0)
    35             return i-1;
    36         else return i+1;
    37     }
    38     public int  getLevel(int i){
    39         return (int)(Math.floor(Math.log(i-1)/Math.log(2)))+1;
    40     }
    41 
    42 
    43     private Node create(int[] arr, int index) {
    44         if (index >= arr.length)       // 可以不需要,但是所有的值必须要写满,任一个#都要写,不然会越界
    45             return null;
    46         else if (String.valueOf(arr[index]).equals("#")||String.valueOf(arr[index]).equals("*")) {
    47             return null;
    48         } else {
    49             Node node = new Node(arr[index]);
    50             node.left = create(arr, 2 * index + 1);
    51             node.right  = create(arr, 2 * index + 2);
    52             return node;
    53         }
    54     }
    55 
    56     class Node{
    57         private Node left;
    58         private Node right;
    59         private int data;
    60         public Node(int data){
    61             this.data = data;
    62         }
    63     }
    64 
    65     public static void main(String[] args) {
    66         int []a = new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
    67         BT bt = new BT (a);
    68         System.out.println(bt.getHeight());
    69         System.out.println(bt.getFather(14));
    70         System.out.println(bt.getLeft(4));
    71         System.out.println(bt.getRight(5));
    72         System.out.println(bt.getBrother(5));
    73         System.out.println(bt.getLevel(5));
    74 
    75     }
    76 }

    如果是1---n, 那么上述公式每个i加1即可。

  • 相关阅读:
    HDFS 常用Shell命令
    Hadoop单点伪分布模式安装
    部署及更新应用
    Linux云服务器下Tomcat部署超详细
    Android基础知识总结系列(一)——Android 系统框架
    Linux下的暴力密码在线破解工具Hydra安装及其组件安装-使用
    Luogu1501 Tree Ⅱ
    C#中Dictionary的用法(转)
    Lua协程
    SSH Secure Shell Client
  • 原文地址:https://www.cnblogs.com/XT-xutao/p/9977769.html
Copyright © 2011-2022 走看看