-
Notifications
You must be signed in to change notification settings - Fork 47
/
BinarySearchTree.java
171 lines (152 loc) · 3.45 KB
/
BinarySearchTree.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
public class BinarySearchTree {
class Node {
public int mKey;
public Object mObject;
public Node mParentNode;
public Node mLeftNode;
public Node mRightNode;
}
private Node mRootNode;
// Recursive add method.
public void add(Node node, Node parentNode, int key, Object object) {
if (node == null) {
Node newNode = new Node();
newNode.mKey = key;
newNode.mObject = object;
newNode.mParentNode = parentNode;
if (parentNode != null) {
if (key < parentNode.mKey) {
parentNode.mLeftNode = newNode;
} else {
parentNode.mRightNode = newNode;
}
} else {
mRootNode = newNode;
}
return;
}
if (key < node.mKey) {
add(node.mLeftNode, node, key, object);
} else {
add(node.mRightNode, node, key, object);
}
}
public void add(int key, Object object) {
add(mRootNode, null, key, object);
}
// Iterative add method.
public void add2(Node node, int key, Object object) {
Node prevNode = null;
while (node != null) {
prevNode = node;
if (key < node.mKey) {
node = node.mLeftNode;
} else {
node = node.mRightNode;
}
}
Node newNode = new Node();
newNode.mKey = key;
newNode.mObject = object;
newNode.mParentNode = prevNode;
if (prevNode == null) {
mRootNode = newNode;
} else {
if (key < prevNode.mKey) {
prevNode.mLeftNode = newNode;
} else {
prevNode.mRightNode = newNode;
}
}
}
public void add2(int key, Object object) {
add2(mRootNode, key, object);
}
// Recursive search method.
public Object search(Node node, int key) {
if(node == null) {
return null;
}
if (node.mKey == key) {
return node.mObject;
}
if (key < node.mKey) {
return search(node.mLeftNode, key);
} else {
return search(node.mRightNode, key);
}
}
public Object search(int key) {
return search(mRootNode, key);
}
// Iterative search method.
public Object search2(Node node, int key) {
if(node == null) {
return null;
}
while (node != null && node.mKey != key) {
if (key < node.mKey) {
node = node.mLeftNode;
} else {
node = node.mRightNode;
}
}
if (node != null) {
return node.mObject;
} else {
return null;
}
}
public Object search2(int key) {
return search2(mRootNode, key);
}
public Node min(Node node) {
while (node.mLeftNode != null) {
node = node.mLeftNode;
}
return node;
}
public Node max(Node node) {
while (node.mRightNode != null) {
node = node.mRightNode;
}
return node;
}
public Node successor(Node node) {
if (node.mRightNode != null) {
return min(node.mRightNode);
}
Node parentNode = node.mParentNode;
while (parentNode != null && parentNode.mRightNode.equals(node)) {
node = parentNode;
parentNode = node.mParentNode;
}
return parentNode;
}
// Inorder walk over the tree.
String printBinarySearchTree(Node node) {
String string = "";
if (node != null) {
string += printBinarySearchTree(node.mLeftNode);
string += node.mObject + ", ";
string += printBinarySearchTree(node.mRightNode);
}
return string;
}
public String toString() {
return printBinarySearchTree(mRootNode);
}
public static void main(String[] args) {
BinarySearchTree bst = new BinarySearchTree();
bst.add(2, "2");
bst.add2(1, "1");
bst.add(7, "7");
bst.add2(9, "9");
bst.add(5, "5");
bst.add2(3, "3");
bst.add(6, "6");
System.out.println(bst.search(9));
System.out.println(bst.search2(3));
System.out.println(bst.toString());
}
}