手写二叉树(从实现,到遍历,查找,获取高度,结点数等方法)
项目工程文件:(包括节点类Node.java 接口类MyBTreeInterface.java 二叉树类MyBTree.java 和测试类MyBTree_test_1.java)
Node.java
1 /*
2 二叉树的结点类
3 */
4 public class Node {
5 public Object obj;
6 public Node leftNode;
7 public Node rightNode;
8 //构造方法
9
10 public Node(Object obj) {
11 this.obj = obj;
12 }
13 public Node(Object obj, Node leftNode, Node rightNode) {
14 this.obj = obj;
15 this.leftNode = leftNode;
16 this.rightNode = rightNode;
17 }
18
19 @Override
20 public String toString() {
21 return "Node{" +
22 "obj=" + obj +
23 ", leftNode=" + leftNode +
24 ", rightNode=" + rightNode +
25 '}';
26 }
27 }
接口类 MyBTreeInterface.java
1 /*
2 我的二叉树的接口
3 */
4
5 public interface MyBTreeInterface {
6 //二叉树的高度
7 public int heigh();
8 //二叉树的结点数量
9 public int size();
10 //二叉树是否为空
11 public boolean isEmpty();
12 //递归前序遍历
13 public void perPrint();
14 //递归中序遍历
15 public void inPrint();
16 //递归后序遍历
17 public void postPrint();
18 //查找结点
19 public Node findKey(Object obj);
20 //层次遍历(借助队列)
21 public void print();
22 //非递归中序遍历
23 public void inPrintByStack();
24 //非递归前序遍历
25 public void perPrintByStack();
26 //非递归后序遍历
27 public void postPrintByStack();
28 }
二叉树类 MyBTree.java
1 import java.util.ArrayDeque;
2 import java.util.ArrayList;
3 import java.util.Deque;
4 import java.util.Queue;
5
6 public class MyBTree implements MyBTreeInterface {
7 private Node root;
8
9 public MyBTree(Node root) {
10 this.root = root;
11 }
12
13 @Override
14 public int heigh() {
15 int height=height(this.root);
16 return height;
17 }
18 private int height(Node node){
19 if(node==null){
20 return 0;
21 }
22 int left=height(node.leftNode);
23 int right=height(node.rightNode);
24 int count=left>right?left:right;
25 return count+1;
26 }
27 @Override
28 public int size() {
29 int height=size(this.root);
30 return height;
31 }
32 private int size(Node node){
33 if(node==null){
34 return 0;
35 }
36 int left=size(node.leftNode);
37 int right=size(node.rightNode);
38 int count=left+right;
39 return count+1;
40 }
41
42 @Override
43 public boolean isEmpty() {
44 return this.root==null;
45 }
46
47 @Override
48 public void perPrint() {
49 System.out.println("前序遍历:");
50 perPrint(this.root);
51 System.out.println();
52 }
53 private void perPrint(Node node){
54 if(node!=null){
55 System.out.print(node.obj+" ");
56 perPrint(node.leftNode);
57 perPrint(node.rightNode);
58 }
59 }
60
61 @Override
62 public void inPrint() {
63 System.out.println("中序遍历:");
64 inPrint(this.root);
65 System.out.println();
66 }
67 private void inPrint(Node node){
68 if(node!=null){
69 inPrint(node.leftNode);
70 System.out.print(node.obj+" ");
71 inPrint(node.rightNode);
72 }
73 }
74 @Override
75 public void postPrint() {
76 System.out.println("后序遍历:");
77 postPrint(this.root);
78 System.out.println();
79 }
80
81 @Override
82 public Node findKey(Object obj) {
83 Node node=findKey(obj,this.root);
84 return node;
85 }
86 private Node findKey(Object obj,Node node){
87 if(node==null){
88 return null;
89 }
90 if(node.obj==obj){
91 return node;
92 }
93 Node node1=findKey(obj,node.leftNode);
94 if(node1.obj==obj){
95 return node1;
96 }
97 Node node2=findKey(obj,node.rightNode);
98 if(node2.obj==obj){
99 return node2;
100 }else{
101 return null;
102 }
103 }
104 private void postPrint(Node node){
105 if(node!=null){
106 postPrint(node.leftNode);
107 postPrint(node.rightNode);
108 System.out.print(node.obj+" ");
109 }
110 }
111 @Override
112 public void print() {
113 System.out.println("顺序遍历:");//借助队列
114 Queue<Node> queue = new ArrayDeque<Node>();
115 Node node=this.root;
116 queue.add(node);
117 while(!queue.isEmpty()){
118 Node temp=queue.poll();
119 System.out.print(temp.obj+" ");
120 Node node1=temp.leftNode;
121 Node node2=temp.rightNode;
122 if(node1!=null){
123 queue.add(node1);
124 }
125 if(node2!=null){
126 queue.add(node2);
127 }
128 }
129 System.out.println();
130 }
131
132 @Override
133 public void inPrintByStack() { //中序借助栈遍历
134 System.out.println("借助栈中序遍历");
135 Deque<Node> deque=new ArrayDeque<>();
136 Node node=this.root;
137 while(node!=null||!deque.isEmpty()){
138 while(node!=null){
139 deque.push(node);
140 node=node.leftNode;
141 }
142 if(!deque.isEmpty()){
143 node=deque.pop();
144 System.out.print(node.obj+" ");
145 node=node.rightNode;
146 }
147
148 }
149
150 }
151
152 @Override
153 public void perPrintByStack() {
154 Deque<Node> deque=new ArrayDeque<>();
155 Node node=this.root;
156
157
158 }
159
160 @Override
161 public void postPrintByStack() {
162
163 }
164 }
所拟要构建的二叉树原型:
测试类 MyBTree_text_1.java
1 /*
2 我的二叉树测试类
3 */
4 public class MyBTree_Test_1 {
5 public static void main(String[] args) {
6 Node node6=new Node(6,null,null);
7 Node node4=new Node(4,null,null);
8 Node node7=new Node(7,null,null);
9 Node node5=new Node(5,node6,null);
10 Node node3=new Node(3,null,node7);
11 Node node2=new Node(2,node4,node5);
12 Node node1=new Node(1,node2,node3);
13 MyBTree b=new MyBTree(node1);
14 //判断二叉树是否为空
15 System.out.println("二叉树是否为空:"+b.isEmpty());
16 //二叉树的前序遍历
17 b.perPrint();
18 //二叉树的中序遍历
19 b.inPrint();
20 //二叉树的后序遍历
21 b.postPrint();
22 //树的高度
23 System.out.print("树的高度:");
24 System.out.println(b.heigh());
25 //树的结点数
26 System.out.print("树的结点数:");
27 System.out.println(b.size());
28 //二叉树的顺序遍历
29 b.print();
30 //二叉树的查找
31 System.out.println(b.findKey(2));
32 //二叉树的借助栈中序遍历
33 b.inPrintByStack();
34 }
35 }
运行结果: