二叉树——Java实现

  1 package struct;
  2  
  3 interface Tree{
  4     //插入元素
  5     void insert(int value);
  6     //中序遍历
  7     void inOrder();
  8     //先序遍历
  9     void perOrder();
 10     //后序遍历
 11     void postOrder();
 12     //层序遍历
 13     //void levelOrder();
 14     //求最小值
 15     int getMinValue();
 16     //求最小值
 17     int getMaxValue();
 18     //指定元素删除
 19     boolean delete(int value);
 20     //求元素个数
 21     int length();
 22     //求树的高度
 23     int height();
 24 }
 25  
 26 //工厂类
 27 class Factory1{
 28     //构造函数
 29     private Factory1(){}
 30     public static Tree getTreeInstance(){
 31         return new BinaryTreeImpl();
 32     }
 33 }
 34  
 35 class BinaryTreeImpl implements Tree{
 36     //根节点
 37     private Node root;
 38     private int size;
 39     class Node{
 40         //定义左子树
 41         private Node leftChild;
 42         //定义右子树
 43         private Node rightChild;
 44         private int data;
 45         //以下为构造方法
 46         public Node(int data){
 47             this.data = data;
 48         }
 49         public Node(Node leftChild, Node rightChild, int data) {
 50             super();
 51             this.leftChild = leftChild;
 52             this.rightChild = rightChild;
 53             this.data = data;
 54         }
 55     }
 56     
 57     //插入元素
 58     public void insert(int data) {
 59         Node node = new Node(data);
 60         //空树
 61         if(root == null){
 62             root = node;
 63             root.leftChild = null;
 64             root.rightChild = null;
 65             size++;
 66         }else{
 67             //非空树
 68             Node current = root;
 69             Node parent = null;
 70             while(true){
 71                 if(data < current.data){
 72                     parent = current;
 73                     current = parent.leftChild;
 74                     //当前元素小于根节点,当前节点为空
 75                     if(current == null){
 76                         parent.leftChild = node;
 77                         size++;
 78                         break;
 79                     }
 80                 }else if(data > current.data){
 81                     parent = current;
 82                     current = parent.rightChild;
 83                     //当前元素大于根节点,且当前节点为空,否则继续循环使当前的current为根节点
 84                     if(current == null){
 85                         parent.rightChild = node;
 86                         size++;
 87                         break;
 88                     }
 89                 }else{
 90                     System.out.println("have same data in the binary tree;");
 91                 }
 92             }//end of  while
 93         }
 94     }
 95     //中序遍历
 96     public void inOrder() {
 97         System.out.println("中序遍历:");
 98         inOrder1(root);
 99         System.out.println();
100     }
101     //中序遍历递归函数
102     private void inOrder1(Node node){
103         if( node == null){
104             return;
105         }
106         inOrder1(node.leftChild);
107         display(node);
108         inOrder1(node.rightChild);
109     }
110     
111     //打印函数
112     private void display(Node node){
113         System.out.print(node.data+" ");
114     }
115     
116     //前序遍历
117     public void perOrder() {
118         System.out.println("前序遍历:");
119         perOrder1(root);
120         System.out.println();
121     }
122     //前序遍历递归函数
123     private void perOrder1(Node node){
124         if(node == null){
125             return;
126         }
127         display(node);
128         perOrder1(node.leftChild);
129         perOrder1(node.rightChild);
130     }
131     
132     //后序遍历
133     public void postOrder() {
134         System.out.println("后序遍历:");
135         postOrder1(root);
136         System.out.println();
137     }
138     //后续遍历递归函数
139     private void postOrder1(Node node){
140         if(node == null){
141             return;
142         }
143         postOrder1(node.leftChild);
144         postOrder1(node.rightChild);
145         display(node);
146     }
147     //层序遍历
148     /*
149     public void levelOrder() {
150     }
151     */
152     //求取树中元素最小值
153     public int getMinValue() {
154         Node node = root;
155         //空树无最小元素
156         if(root == null){
157             return -1;
158         }
159         Node current = node.leftChild;
160         while(true){
161             if(current.leftChild == null){
162                 return current.data;
163             }
164             current = current.leftChild;
165         }
166     }
167     //求树中最大元素
168     public int getMaxValue(){
169         Node node = root;
170         if(node == null){
171             return -1;
172         }
173         Node current = node.rightChild;
174         while(true){
175             if(current.rightChild == null){
176                 return current.data;
177             }
178             current = current.rightChild;
179         }
180     }
181     //删除树中元素
182     public boolean delete(int value) {
183         return false;
184     }
185     //求树中元素个数
186     public int length(){
187         return size;
188     }
189     //求树的高度
190     public int height(){
191         if(root == null){
192             return 0;
193         }
194         return height1(root);
195     }
196     private int height1(Node node) {
197         if(node!=null){
198             int lheight = height1(node.leftChild);
199             int rheight = height1(node.rightChild);    
200             return lheight > rheight ? lheight+1:rheight+1;
201         }
202         return 0;
203     }
204 }
205 public class BinaryTree {
206     public static void main(String[] args) {
207         Tree tree = Factory1.getTreeInstance();
208         System.out.println("===============测试insert函数=====================");
209         tree.insert(2);
210         tree.insert(1);
211         tree.insert(5);
212         tree.insert(20);
213         tree.insert(3);
214         tree.insert(7);
215         tree.insert(0);
216         tree.insert(10);
217         System.out.println("\n"+"===============测试length函数====================="+"\n");
218         System.out.println(tree.length());
219         System.out.println("\n"+"===============测试inOrder函数====================="+"\n");
220         tree.inOrder();
221         System.out.println("\n"+"===============测试perOrder函数====================="+"\n");
222         tree.perOrder();
223         System.out.println("\n"+"===============测试postOrder函数====================="+"\n");
224         tree.postOrder();
225         System.out.println("\n"+"===============测试getMinValue函数====================="+"\n");
226         System.out.println(tree.getMinValue());
227         System.out.println("\n"+"===============测试getMaxValue函数====================="+"\n");
228         System.out.println(tree.getMaxValue());
229         System.out.println("\n"+"===============测试height函数====================="+"\n");
230         System.out.println(tree.height());
231         }
232 }
View Code

 

 

 

posted @ 2020-03-29 21:34  edda_huang  阅读(173)  评论(0编辑  收藏  举报