順次ストレージバイナリツリーを達成するためのJava

1.ツリー順次ストレージ
バイナリツリーは、一般記憶するために使用された場合に格納された順次ツリーは、一般的に、完全なバイナリツリーの完全なバイナリツリーを格納するために使用されるか、またはされた空間を無駄にしています。
注意すべき2ポイント
1 、左から右へ、上から下にあれば、完全なバイナリツリーについては、その左の子が2iのように番号しなければならない私は、ノードの数は、それが子2I + 1のための右の数でなければならない、その親ノード番号なければなりませんI / 2です。
2 ほとんどの2 ^ k-1個のノードでの深さkの。バイナリツリー
3 。i番目のレイヤ2(K-1)^ノード
4 バイナリツリーノードの数は2度とn2を有する場合、ツリーの葉は1 + N2必ずしもN0(1 +すなわちN0 = N2)のために
抽象的構造
1.ノード増加を初期化
2.指定されたノードの左ノードを返します。
3.右側のノードに指定されたノードを返します。
4.ノードの周りに指定されたノードを追加します。
木が空であるかどうかを決意
3.実装の詳細
1. 3つのコンストラクタ、それぞれいいえ引数コンストラクタ、構造(ツリーの深さ)のパラメータ、二つのパラメータ(ツリーの深さとルートノード)コンストラクタが存在します。これはDEFAULE_KEEPを定義:デフォルト深さ、木の長さを定義した情報は、ツリーのノードを格納する配列を定義します。
2. 2つのメソッドを書き込むノードを追加し、一つは一度初期化時に付加され、その後0を加えた最初の出口時刻を入力し、指定されたノードの二種類を追加左ノードと右ノードで、指定を追加するときを決定カバレッジを選択するかどうか、空の場合は左ノードとノードの右ノードか否か、空です。
3.指定されたノードの左ノード、右ノードと親ノードを返します。現在のノードのインデックスは2Iノードが残っているI、であり、右のノードが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