-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTIter.java
192 lines (170 loc) · 5.62 KB
/
BSTIter.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
class BSTIter {
static NodeIter root;
public static int numLevels = 0;
public BSTIter() {
root = new NodeIter();
}
public static void main(String[] args) {
BSTIter tree = new BSTIter();
insert(root, 5);
insert(root, 7);
insert(root, 3);
insert(root, 4);
insert(root, 1);
System.out.println(root.value);
System.out.println(root.rightChild.value);
System.out.println(root.leftChild.value);
System.out.println(root.leftChild.rightChild.value);
System.out.println(root.leftChild.leftChild.value);
System.out.println("FindMinRec: " + findMin(root));
System.out.println("FindMaxRec: " + findMax(root));
System.out.println("Element next after 3: " + findNext(root, 3));
System.out.println("Element next before 1: " + findPrev(root, 1));
}
public static void insert(NodeIter root, int element) {
// Start at root of tree
if (root.value == -12345) {
root.value = element;
return;
}
while (true) {
numLevels++;
if (element < root.value && root.leftChild == null)
{
NodeIter node = new NodeIter(element);
root.leftChild = node;
break;
}
else
if (element > root.value && root.rightChild == null)
{
NodeIter node = new NodeIter(element);
root.rightChild = node;
break;
}
// if element < root.value, go left
if (element < root.value) {
root = root.leftChild;
} else {
root = root.rightChild;
}
}
}
public static void delete(NodeIter root, int element) {
// Search for the node
NodeIter node = root;
root = findNode(root, element);
if (root.leftChild == null && root.rightChild == null) // If it's a leaf, just delete
{
root.value = -1;
return;
}
else
{
if ((root.leftChild == null && root.rightChild != null)) // If only one child, move child to root's spot
{
root.parent.rightChild = root.rightChild;
root.value = -1;
return;
}
if (root.leftChild != null && root.rightChild == null)
{
root.parent.leftChild = root.leftChild;
root.value = -1;
return;
}
}
NodeIter next = new NodeIter(findNext(node, element));
root = next;
next = null;
}
public static int findNext(NodeIter root, int element) {
int max = findMax(root);
// Search for the node with element
root = findNode(root, element);
if (max == element)
return -1;
if (root.rightChild != null) // Check right child
return findMin(root.rightChild);
else
return findNextParent(root.parent, element).value; //Go up until you find parent that is > element
}
public static int findPrev(NodeIter root, int element) {
// Search for the node with element
int min = findMin(root);
root = findNode(root, element);
if (min == element)
return -1;
if (root.leftChild != null) // Check the left Child then find max
return findMax(root.leftChild);
else
return findPrevParent(root.parent, element).value; // If no left children, go up to parent that is < element
}
public static int findMin(NodeIter root) {
while (root.leftChild != null) {
root = root.leftChild;
}
return root.value;
}
public static int findMax(NodeIter root) {
while (root.rightChild != null) {
root = root.rightChild;
}
return root.value;
}
public static NodeIter findNode(NodeIter root, int element)
{
if (root.value == element)
return root;
while (root.value != element) { // While the element isn't found
if (root.value == element) // If the value equals element return the node
return root;
if (element < root.value) // If element < value, go left
{
root = root.leftChild;
}
else { // If element > value, go right
root = root.rightChild;
}
}
return root;
}
public static NodeIter findNextParent(NodeIter root, int element)
{
while (root.value < element) {
if (root.value > element)
return root;
else
root = root.parent;
}
return root;
}
public static NodeIter findPrevParent(NodeIter root, int element)
{
while (root.value > element) {
if (root.value < element)
return root;
else
root = root.parent;
}
return root;
}
}
class NodeIter {
int value;
NodeIter leftChild;
NodeIter rightChild;
NodeIter parent;
public NodeIter() {
value = -12345;
rightChild = null;
leftChild = null;
parent = null;
}
public NodeIter(int value) {
this.value = value;
rightChild = null;
leftChild = null;
parent = null;
}
}