-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.java
174 lines (155 loc) · 5.23 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
class BST {
static Node root;
public BST() {
root = new Node();
}
public static void main(String[] args) {
BST tree = new BST();
insertRec(root, 5);
insertRec(root, 7);
insertRec(root, 3);
insertRec(root, 4);
insertRec(root, 1);
//deleteRec(root, 7);
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(Node 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 Node(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 Node(element);
root.rightChild.parent = root;
}
else // otherwise keep going to the right
{
insertRec(root.rightChild, element);
}
}
}
public static void deleteRec(Node root, int element) {
// Search for the node
Node 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;
}
}
Node next = findNextRec(node, element);
root = next;
next = null;
}
public static Node findNextRec(Node root, int element) {
Node 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 Node findPrevRec(Node root, int element) {
// Search for the node with element
Node 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 Node findMinRec(Node root) {
if (root.leftChild == null)
return root;
return findMinRec(root.leftChild);
}
public static Node findMaxRec(Node root) {
if (root.rightChild == null)
return root;
return findMaxRec(root.rightChild);
}
public static Node findNode(Node 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 Node findNextParent(Node root, int element)
{
if (root.value > element)
return root;
else
return findNextParent(root.parent, element);
}
public static Node findPrevParent(Node root, int element)
{
if (root.value < element)
return root;
else
return findNextParent(root.parent, element);
}
}
class Node {
int value;
Node leftChild;
Node rightChild;
Node parent;
public Node() {
value = -12345;
rightChild = null;
leftChild = null;
parent = null;
}
public Node(int value) {
this.value = value;
rightChild = null;
leftChild = null;
}
}