java achieve sequential storage binary tree

1. Tree sequential storage
Stored sequentially tree is generally used to store the full binary tree complete binary tree or, if the binary tree is used to store general have wasted space.
2. Points to note
1. For a complete binary tree, if from top to bottom, left to right, the number of node i, its left child must be numbered as 2i, it must be the right number for the child 2i + 1, its parent node numbers must is i / 2.
2 . Binary tree of depth k at most 2 ^ k-1 nodes
3 . The i-th layer 2 (k-1) nodes ^
4 for any binary tree, when the number of nodes has degree 2 and n2, the leaves of the tree is necessarily n0 n2 + 1 (i.e. n0 = n2 + 1)
The abstract structure
1. Initialize node increases
2. Return the left node of the specified node.
3. Return the specified node to the right node
4. Add the specified node around the node
The determination whether the tree is empty
3. The implementation details
1. There are three constructors, respectively No No argument constructor, a parameter (depth of the tree) structure, two parameters (depth of the tree and the root node) constructor. It defines DEFAULE_KEEP: default depth; defines the length of the tree; information defining an array to store the tree nodes.
2. Add the nodes to write two methods, one is added at one time initialization, then enter the first exit time 0 was added; add two kinds of the specified node is the left node and a right node, determining when to add the specified whether a left node and a right node of the node is empty, if empty whether to select coverage.
3. Return the specified node left node, right node and the parent node. The current node index is i, 2i nodes are left, right node is 2i + 1, the parent node is i / 2; Note that the front tips of these conditions are met, the root index from 1 starts.
  1 package OrderBinaryTree;
  2 
  3 import java.util.Scanner;
  4 
  5 
  6 public class BinaryTree {
  7     final private int DEFAULT_PEEK=6;
  8     private int deep;
  9     private int length;
 10     private String[] array;
 11     /**
 12      * 无参构造器
 13      */
 14     public BinaryTree() {
 15         this.deep=DEFAULT_PEEK;
 16         this.length=(int)(Math.pow(2,deep)-1);
 17         array=new String[length];
 18     }
 19     /**
 20      * 传入深度参数的构造器
 21      */
 22     public BinaryTree(int deep) {
 23         this.deep=deep;
 24         this.length=(int)(Math.pow(2,deep)-1);
 25         array=new String[length];
 26     }
 27     /**
 28      * 传入参数深度和根结点的构造器
 29      */
 30     public BinaryTree(int deep,String data) {
 31         this.deep=deep;
 32         this.length=(int)(Math.pow(2,deep)-1);
 33         array=new String[length];
 34         array[1]=data;
 35     }
 36     /**
 37      * 添加初始化结点
 38      */
 39     public void addNode() {
 40         Scanner input=new Scanner(System.in);
 41         System.out.println("请输入二叉树信息,输入值为0结束输入");
 42             for (int i = 1; i < array.length; i++) {
 43                 if (array[i]==null||array[i].equals("0")==true) {
 44                     array[i]=input.nextLine();
 45                     if (array[i].equals("0")==true) {
 46                         break;
 47                 }
 48             }
 49         }
 50     }
 51     /**
 52      * 判断二叉树是否为空
 53      */
 54     public boolean isEmpaty() {
 55         if (array[1]!=null&&array[1].equals("0")!=true) {
 56             return false;
 57         }else {
 58             return true;
 59         }
 60     }
 61     /**
 62      * 返回指定结点的值
 63      */
 64     public String returnData(int index) {
 65         if (index<1||index>array.length-1) {
 66             return null;
 67         }else {
 68             return array[index];
 69         }
 70     }
 71     /**
 72      * 返回指定结点的父节点
 73      */
 74     public String getParent(int index) {
 75         if (index<1||index>array.length-1) {
 76             return null;
 77         }else {
 78             return array[index/2];
 79         }
 80     }
 81     
 82     /**
 83      * 添加指定结点的左节点
 84      */
 85     public void addNodeLeft(int index,String data) {
 86         if (array[2*index]!=null&&array[2*index].equals("0")!=true) {
 87             System.out.println("当前结点左节点已有值,是否覆盖?Y/N");
 88             Scanner input=new Scanner(System.in);
 89             String in=input.nextLine();
 90             if (in.equals("Y")) {
 91                 array[index*2]=data;
 92             }else if (in.equals("N")) {
 93                 return;
 94             }
 95         }
 96     }
 97     
 98     /**
 99      * 添加指定结点的左节点
100      */
101     public void addNodeRight(int index,String data) {
102         if (array[2*index+1]!=null&&array[2*index+1].equals("0")!=true) {
103             System.out.println("当前结点右节点已有值,是否覆盖?Y/N");
104             Scanner input=new Scanner(System.in);
105             String in=input.nextLine();
106             if (in.equals("Y")) {
107                 array[index*2+1]=data;
108             }else if(in.equals("N")) {
109                 return;
110             }
111         }
112     }
113     
114     /**
115      * 返回指定结点的左节点
116      */
117     public String getLeftNode(int index) {
118         if (array[2*index]!=null&&array[2*index].equals("0")!=true) {
119             return array[index*2];
120         }
121         else {
122             return null;
123         }
124     }
125     
126     /**
127      * 返回指定结点的右节点
128      */
129     public String getRightNode(int index) {
130         if (array[2*index+1]!=null&&array[2*index+1].equals("0")!=true) {
131             return array[index*2+1];
132         }
133         else {
134             return null;
135         }
136     }
137 }

测试类:

 1 package OrderBinaryTree;
 2 public class TreeText {
 3     public static void main(String[] args) {
 4         BinaryTree bin=new BinaryTree();
 5         bin.addNode();
 6         System.out.println("左节点是:");
 7         System.out.println(bin.getLeftNode(2));
 8         System.out.println("右节点是:");
 9         System.out.println(bin.getRightNode(2));
10         System.out.println("====================");
11         bin.addNodeLeft(2, "insert");
12         bin.addNodeRight(2, "insert");
13         System.out.println("插入后左节点是:");
14         System.out.println(bin.getLeftNode(2));
15         System.out.println("====================");
16         System.out.println("插入后右节点是:");
17         System.out.println(bin.getRightNode(2));
18     }
19 
20 }

 

Guess you like

Origin www.cnblogs.com/had1314/p/11268099.html