-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.java
212 lines (181 loc) · 5.8 KB
/
BST.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import java.util.*;
public class BST<T extends Comparable<T>> {
protected class Node {
T data;
Node left = null;
Node right = null;
Node parent = null;
public Node(T data) {
this.data = data;
}
}
protected Node root;
private Random random = new Random();
public boolean isBalanced() {
return isBalanced(root);
}
private boolean isBalanced(Node node) {
if (node == null) { return true; }
if (Math.abs(height(node.left) - height(node.right)) <= 1) {
return isBalanced(node.right) && isBalanced(node.left);
} else {
return false;
}
}
private int height(Node node) {
if(node == null) { return 0; }
return Math.max(height(node.left), height(node.right)) + 1;
}
public void add(T data) {
Node newNode = new Node(data);
if(root == null) {
System.out.println("Root added: " + data);
root = newNode;
return;
}
Node cur = root;
Node prvs = null;
boolean lastMove = false; //right
while(cur != null) {
prvs = cur;
if(cur.data.compareTo(data) > 0) { // go left
//System.out.println("DEBUG: Moving Left...");
lastMove = true;
cur = cur.left;
} else {
//System.out.println("DEBUG: Moving Right...");
lastMove = false;
cur = cur.right;
}
}
if(lastMove) {
//System.out.println("DEBUG: Adding to left: " + data);
prvs.left = newNode;
} else {
//System.out.println("DEBUG: Adding to right: " + data);
prvs.right = newNode;
}
newNode.parent = prvs;
}
public String inorder() {
StringBuffer sb = new StringBuffer();
inorder(this.root, sb);
return sb.toString();
}
public String preorder() {
StringBuffer sb = new StringBuffer();
preorder(this.root, sb);
return sb.toString();
}
private void preorder(Node cur, StringBuffer sb) {
if (cur == null) {
sb.append("_");
return;
}
sb.append(cur.data + "{");
preorder(cur.left,sb);
sb.append(",");
preorder(cur.right, sb);
sb.append("}");
}
private void inorder(Node cur, StringBuffer sb) {
if (cur == null) return;
inorder(cur.left,sb);
sb.append(cur.data + " ");
inorder(cur.right, sb);
}
public Node search(T data) {
Node curr = this.root;
while (curr != null && curr.data != data) {
if (curr.data.compareTo(data) > 0) {
curr = curr.left;
} else {
curr = curr.right;
}
}
return curr;
}
public T findPredecessor(T data) {
Node found = findPredecessor(search(data));
return found == null ? null : found.data;
}
private Node findPredecessor(Node node) {
if (node == null) {
return null;
}
if (node.left != null) {
return findMax(node.left);
}
Node parent = node.parent;
while (parent != null && node != parent.right) {
node = parent;
parent = parent.parent;
}
return parent;
}
public T findSuccessor(T data) {
Node found = findSuccessor(search(data));
return found == null ? null : found.data;
}
private Node findSuccessor(Node node) {
if (node == null) {
return null;
}
if (node.right != null) {
return findMin(node.right);
}
Node parent = node.parent;
while (parent != null && node != parent.left) {
node = parent;
parent = parent.parent;
}
return parent;
}
private Node findMin(Node node) {
while(node !=null && node.left != null) {
node = node.left;
}
return node;
}
private Node findMax(Node node) {
while (node != null && node.right != null) {
node = node.right;
}
return node;
}
public Map<Integer, List<T>> traversePerLevel() {
Map<Integer, List<T>> nodesPerLevel = new HashMap<Integer, List<T>>();
traversePerLevel(this.root, 0, nodesPerLevel);
return nodesPerLevel;
}
private void traversePerLevel(Node node, int level, Map<Integer, List<T>> nodesPerLevel) {
if(node == null) { return; }
List<T> curLevelList = nodesPerLevel.get(level);
if(curLevelList == null) {
curLevelList = new LinkedList<T>();
nodesPerLevel.put(level, curLevelList);
}
curLevelList.add(node.data);
traversePerLevel(node.left, level + 1, nodesPerLevel);
traversePerLevel(node.right, level + 1, nodesPerLevel);
}
public void createTreeWithMinHeight(T[] values) {
this.root = new Node(null);
this.createTreeWithMinHeight(values, 0, values.length - 1, this.root);
}
public void createTreeWithMinHeight(T[] values, int start, int end, Node root) {
if (root == null) { throw new IllegalStateException(); }
int mid = start + (end - start) / 2;
root.data = values[mid];
//System.out.println("DEBUG: start=" + start + ", end=" + end + ", mid=" + mid);
//System.out.println("DEBUG: root.data=" + root.data);
if(mid - start > 0) {
root.left = new Node(null);
createTreeWithMinHeight(values, start, mid - 1, root.left);
}
if(end - mid > 0) {
root.right = new Node(null);
createTreeWithMinHeight(values, mid + 1, end, root.right);
}
}
}