Repository navigation
Expand file tree
/
Copy pathBST.java
More file actions
205 lines (183 loc) · 6.14 KB
/
Copy pathBST.java
File metadata and controls
205 lines (183 loc) · 6.14 KB
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
class BST {
static NodeBST root;
public BST() {
root = new NodeBST();
}
public static void main(String[] args) {
BST tree = new BST();
insertRec(root, 6);
insertRec(root, 3);
insertRec(root, 2);
insertRec(root, 4);
//insertRec(root, 5);
deleteRec(root, 3);
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: " + findMinRec(root).value);
//System.out.println("FindMaxRec: " + findMaxRec(root).value);
//System.out.println("Element next after 3: " + findNextRec(root, 3).value);
//System.out.println("Element next before 5: " + findPrevRec(root, 5).value);
}
public static void insertRec(NodeBST root, int element) {
// Start at root of tree
if (root.value == -12345) {
root.value = element;
return;
}
// if element < root.value, go left
if (element < root.value) {
if (root.leftChild == null) // if left child is null, make a new node with element
{
root.leftChild = new NodeBST(element);
root.leftChild.parent = root;
}
else // otherwise keep going to the left
{
insertRec(root.leftChild, element);
}
}
else {
if (root.rightChild == null) // if right child is null, make a new node with element
{
root.rightChild = new NodeBST(element);
root.rightChild.parent = root;
}
else // otherwise keep going to the right
{
insertRec(root.rightChild, element);
}
}
}
public static void deleteRec(NodeBST root, int element) {
// Search for the node
NodeBST node = root;
root = findNode(root, element);
if (root.leftChild == null && root.rightChild == null) // If it's a leaf, just delete
{
if (root.parent.leftChild == root)
{
root.parent.leftChild = null;
}
else
{
root.parent.rightChild = null;
}
root = null;
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.rightChild.parent = root.parent;
root = null;
return;
}
if (root.leftChild != null && root.rightChild == null)
{
root.parent.leftChild = root.leftChild;
root.leftChild.parent = root.parent;
root = null;
return;
}
}
NodeBST next = findNextRec(node, element);
if (next.leftChild != null || next.rightChild != null)
deleteRec(root, next.value);
if (root.leftChild == next)
{
root.leftChild = null;
}
else
{
root.rightChild = null;
}
root.value = next.value;
next = null;
}
public static NodeBST findNextRec(NodeBST root, int element) {
NodeBST max = findMaxRec(root);
// Search for the node with element
root = findNode(root, element);
if (max.value == element)
return null;
if (root.rightChild != null) // Check right child
return findMinRec(root.rightChild);
else
return findNextParent(root.parent, element); //Go up until you find parent that is > element
}
public static NodeBST findPrevRec(NodeBST root, int element) {
// Search for the node with element
NodeBST min = findMinRec(root);
root = findNode(root, element);
if (min.value == element)
return null;
if (root.leftChild != null) // Check the left Child then find max
return findMaxRec(root.leftChild);
else
return findPrevParent(root.parent, element); // If no left children, go up to parent that is < element
}
public static NodeBST findMinRec(NodeBST root) {
if (root.leftChild == null)
return root;
return findMinRec(root.leftChild);
}
public static NodeBST findMaxRec(NodeBST root) {
if (root.rightChild == null)
return root;
return findMaxRec(root.rightChild);
}
public static NodeBST findNode(NodeBST root, int element)
{
if (root.value == element)
return root;
if (element < root.value)
return findNode(root.leftChild, element);
else {
return findNode(root.rightChild, element);
}
}
public static NodeBST findNextParent(NodeBST root, int element)
{
if (root.value > element)
return root;
else
return findNextParent(root.parent, element);
}
public static NodeBST findPrevParent(NodeBST root, int element)
{
if (root.value < element)
return root;
else
return findNextParent(root.parent, element);
}
public static void inOrder(NodeBST root)
{
if (root == null)
return;
inOrder(root.leftChild);
System.out.print(root.value + " ");
inOrder(root.rightChild);
}
}
class NodeBST {
int value;
NodeBST leftChild;
NodeBST rightChild;
NodeBST parent;
public NodeBST() {
value = -12345;
rightChild = null;
leftChild = null;
parent = null;
}
public NodeBST(int value) {
this.value = value;
rightChild = null;
leftChild = null;
}
}