-
Notifications
You must be signed in to change notification settings - Fork 463
/
Tree.java
177 lines (152 loc) · 3.68 KB
/
Tree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Java Binary Search Tree
public class Tree {
Node root;
// some basic test code
public static void main(String[] args) {
Tree tree = new Tree();
int[] items = {5, 8, 7, 1, 9, 3, 0, 4, 6, 2};
for (int i : items)
tree.insert(i);
System.out.println("Tree Height: " + tree.getHeight());
System.out.println("Tree Size: " + tree.getSize());
System.out.println("Find 3: " + tree.find(3));
tree.inorder();
}
public boolean insert(int val) {
if (root == null) {
root = new Node(val);
return true;
}
else
return root.insert(val);
}
public boolean find(int val) {
if (root == null)
return false;
else
return root.find(val);
}
public int getHeight() {
if (root != null)
return root.getHeight();
else
return 0;
}
public int getSize() {
if (root != null)
return root.getSize();
else
return 0;
}
public void preorder() {
if (root != null) {
System.out.println("Preorder:");
root.preorder();
}
}
public void postorder() {
if (root != null) {
System.out.println("Postorder:");
root.postorder();
}
}
public void inorder() {
if (root != null) {
System.out.println("Inorder:");
root.inorder();
}
}
private class Node {
private Node leftChild;
private Node rightChild;
private int data;
private Node(int val) {
data = val;
leftChild = null;
rightChild = null;
}
private boolean insert(int val) {
boolean added = false;
if (this == null) {
this.data = val;
return true;
}
else {
if (val < this.data) {
if (this.leftChild == null) {
this.leftChild = new Node(val);
return true;
}
else
added = this.leftChild.insert(val);
}
else if (val > this.data) {
if (this.rightChild == null) {
this.rightChild = new Node(val);
return true;
}
else
added = this.rightChild.insert(val);
}
}
return added;
}
private boolean find(int val) {
boolean found = false;
if (this == null)
return false;
else {
if (val == this.data)
return true;
else if (val < this.data && this.leftChild != null)
found = this.leftChild.find(val);
else if (val > this.data && this.rightChild != null)
found = this.rightChild.find(val);
}
return found;
}
private int getHeight() {
int leftHeight = 0, rightHeight = 0;
if (this.leftChild != null)
leftHeight = this.leftChild.getHeight();
if (this.rightChild != null)
rightHeight = this.rightChild.getHeight();
return 1 + Math.max(leftHeight, rightHeight);
}
private int getSize() {
int leftSize = 0, rightSize = 0;
if (this.leftChild != null)
leftSize = this.leftChild.getSize();
if (this.rightChild != null)
rightSize = this.rightChild.getSize();
return 1 + leftSize + rightSize;
}
private void preorder() {
if (this != null) {
System.out.println(this.data);
if (this.leftChild != null)
this.leftChild.preorder();
if (this.rightChild != null)
this.rightChild.preorder();
}
}
private void postorder() {
if (this != null) {
if (this.leftChild != null)
this.leftChild.postorder();
if (this.rightChild != null)
this.rightChild.postorder();
System.out.println(this.data);
}
}
private void inorder() {
if (this != null) {
if (this.leftChild != null)
this.leftChild.inorder();
System.out.println(this.data);
if (this.rightChild != null)
this.rightChild.inorder();
}
}
}
}