-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
156 lines (154 loc) · 3.3 KB
/
BST.java
File metadata and controls
156 lines (154 loc) · 3.3 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
package ds;
public class BST<T> {
BSTNode<T> root, current;
/** Creates a new instance of BST */
public BST() {
root = current = null;
}
public boolean empty() {
return root == null;
}
public boolean full() {
return false;
}
public T retrieve () {
return current.data;
}
public boolean update(int key, T data){
remove_key(current.key);
return insert(key, data);
}
public void deleteSubtree(){
if(current == root){
current = root = null;
}
else {
BSTNode<T> p = current;
find(Relative.Parent);
if(current.left == p)
current.left = null;
else
current.right = null;
current = root;
}
}
public boolean find(Relative rel){
switch (rel) {
case Root: // Easy case
current = root;
return true;
case Parent:
if(current == root)
return false;
current = findparent(current, root);
return true;
case LeftChild:
if(current.left == null)
return false;
current = current.left;
return true;
case RightChild:
if(current.right == null)
return false;
current = current.right;
return true;
default:
return false;
}
}
private BSTNode<T> findparent(BSTNode<T> p, BSTNode<T> t) {
if(t == null)
return null; // empty tree
if(t.right == null && t.left == null)
return null;
else if(t.right == p || t.left == p)
return t; // parent is t
else {
BSTNode<T> q = findparent(p, t.left);
if (q != null)
return q;
else
return findparent(p, t.right);
}
}
public boolean findkey(int tkey) {
BSTNode<T> p = root, q = root;
if(empty())
return false;
while(p != null) {
q = p;
if(p.key == tkey) {
current = p;
return true;
}
else if(tkey < p.key)
p = p.left;
else
p = p.right;
}
current = q;
return false;
}
public boolean insert(int k, T val) {
BSTNode<T> p, q = current;
if(findkey(k)) {
current = q; // findkey() modified current
return false; // key already in the BST
}
p = new BSTNode<T>(k, val);
if (empty()) {
root = current = p;
return true;
}
else {
// current is pointing to parent of the new key
if (k < current.key)
current.left = p;
else
current.right = p;
current = p;
return true;
}
}
public boolean remove_key(int tkey){
BooleanWrapper removed = new BooleanWrapper(false);
BSTNode<T> p;
p = remove_aux(tkey, root, removed);
current = root = p;
return removed.getValue();
}
private BSTNode<T> remove_aux(int key, BSTNode<T> p, BooleanWrapper flag) {
BSTNode<T> q, child = null;
if(p == null)
return null;
if(key < p.key)
p.left = remove_aux(key, p.left, flag); //go left
else if(key > p.key)
p.right = remove_aux(key, p.right, flag); //go right
else { // key is found
flag.setValue(true);
if (p.left != null && p.right != null) { //two children
q = find_min(p.right);
p.key = q.key;
p.data = q.data;
p.right = remove_aux(q.key, p.right, flag);
}
else {
if (p.right == null) //one child
child = p.left;
else if (p.left == null) //one child
child = p.right;
return child;
}
}
return p;
}
private BSTNode<T> find_min(BSTNode<T> p){
if(p == null)
return null;
while(p.left != null){
p = p.left;
}
return p;
}
}