java实现二叉树的顺序存储

1.树的顺序存储
树的顺序存储一般是用来存储满二叉树或者完全二叉树的,如果用来存储一般的二叉树会有空间浪费。
2.注意要点
1. .对于完全二叉树,若从上往下,从左到右,则编号为i的结点,其左孩子编号必定为2i,其右边孩子编号必定为2i+1,其双亲结点编号必定为i/2.
2.深度为k的二叉树至多有2^k-1个结点
3.第i层有2^(k-1)个结点
4.对于任何一棵二叉树,若2度的结点数有n2个,则叶子树n0必定为n2+1(即n0=n2+1)
5.抽象的结构
1.初始化增加结点
2.返回指定结点的左节点
3.返回指定节点的右节点
4.添加指定节点的左右节点
5.判断树是否为空
3.实现细节
1.有三种构造器,分别无无参构造器,一个参数(树的深度)的构造器,两个参数(树的深度和根结点)的构造器。定义了DEFAULE_KEEP:默认深度;定义了树的长度;定义了一个数组用来存储树种结点的信息。
2.添加结点的方法写了两种,一种是初始化直接一次性添加,然后输入第0的时候退出添加;二种是添加指定结点的左节点和右节点,添加的时候进行判断指定结点的左节点和右节点是否为空,如果为空是否选择覆盖。
3.返回指定结点的左节点,右节点和父节点。当前结点下标为i,左节点是2i,右节点是2i+1,父节点是i/2;需要注意的是满足这些条件的前提示,根结点的下标是从1开始。
  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 }

猜你喜欢

转载自www.cnblogs.com/had1314/p/11268099.html