-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
197 lines (186 loc) · 5.77 KB
/
BinarySearchTree.java
File metadata and controls
197 lines (186 loc) · 5.77 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
import java.awt.*;
import java.util.*;
public class BinarySearchTree {
private BinaryNode root;
private ArrayList<BinaryNode> nodes = new ArrayList<>();
public BinarySearchTree() {
root = null;
}
public void add(BinaryNode x) {
if (root == null) {
root = x;
return;
}
add(root, x);
}
private void add(BinaryNode parent, BinaryNode x) {
if (parent == null) {
return;
}
if (x.getValue() < parent.getValue()) {
if (parent.getLeft() == null) {
parent.setLeft(x);
} else {
add(parent.getLeft(), x);
}
} else {
if (parent.getRight() == null) {
parent.setRight(x);
} else {
add(parent.getRight(), x);
}
}
}
public BinaryNode remove(int target) {
if (root == null) return null;
BinaryNode temp = root;
BinaryNode inorderSuccessor;
if (root.getValue() == target) {
if (root.getLeft() == null && root.getRight() == null) {
root = null;
return temp;
} else if (root.getLeft() == null) {
root = root.getRight();
temp.setRight(null);
return temp;
} else if (root.getRight() == null) {
root = root.getLeft();
temp.setLeft(null);
return temp;
} else {
inorderSuccessor = successor(root);
swap(root, inorderSuccessor);
if(root.getRight()==inorderSuccessor)
{
root.setRight(inorderSuccessor.getRight());
inorderSuccessor.setRight(null);
return inorderSuccessor;
}
return remove(root.getRight(), target);
}
}
return remove(root, target);
}
private BinaryNode successor(BinaryNode k) {
BinaryNode temp = k;
temp = temp.getRight();
while (temp.getLeft() != null) {
temp = temp.getLeft();
}
return temp;
}
private void swap(BinaryNode x, BinaryNode y) {
int k = x.getValue();
x.setValue(y.getValue());
y.setValue(k);
}
private BinaryNode search(BinaryNode parent, int target)
{
if(parent == null) return null;
if(parent.getLeft()!=null && parent.getLeft().getValue()==(target)||
parent.getRight()!=null && parent.getRight().getValue()==(target))
return parent;
else if(target<parent.getValue())
return search(parent.getLeft(),target);
else
return search(parent.getLeft(),target);
}
private BinaryNode remove(BinaryNode startNode, int target)
{
BinaryNode nodeToRemove, inorderSuccessor;
BinaryNode parent = search(startNode,target);
if(parent == null) return null;
//decide if it is a left or right child
boolean isLeft = parent.getLeft()!=null &&
parent.getLeft().getValue()==(target);
nodeToRemove = isLeft ? parent.getLeft() : parent.getRight();
//degree 0
if(nodeToRemove.getLeft() == null && nodeToRemove.getRight() == null)
{
if(isLeft)
parent.setLeft(null);
else
parent.setRight(null);
return nodeToRemove;
}
//degree 1
else if(nodeToRemove.getLeft() == null)
{
if(isLeft)
parent.setLeft(nodeToRemove.getRight());
else
parent.setRight(nodeToRemove.getRight());
nodeToRemove.setRight(null);
return nodeToRemove;
}
else if(nodeToRemove.getRight() == null)
{
if(isLeft)
parent.setLeft(nodeToRemove.getLeft());
else
parent.setRight(nodeToRemove.getLeft());
nodeToRemove.setLeft(null);
return nodeToRemove;
}
//degree 2
else
{
inorderSuccessor = successor(nodeToRemove);
swap(inorderSuccessor, nodeToRemove);
if(nodeToRemove.getRight()==inorderSuccessor)
{
nodeToRemove.setRight(inorderSuccessor.getLeft());
inorderSuccessor.setRight(null);
return inorderSuccessor;
}
return remove(nodeToRemove.getRight(), target);
}
}
public String levelOrder()
{
String temp = "";
Queue<BinaryNode> queue = new LinkedList<BinaryNode>();
queue.offer(root);
while(!queue.isEmpty())
{
BinaryNode k = queue.poll();
temp += k.getValue()+" ";
if(k.getLeft()!=null)
queue.offer(k.getLeft());
if(k.getRight()!=null)
queue.offer(k.getRight());
}
return temp.trim();
}
public ArrayList<BinaryNode> preOrder(){
nodes = new ArrayList<BinaryNode>();
preOrder(root, 1, 1);
return nodes;
}
private void preOrder(BinaryNode k, int pos, int height){
if(k == null) {
BinaryNode tempNode = new BinaryNode();
tempNode.setX(pos);
tempNode.setY(height);
nodes.add(tempNode);
return;
}
k.setX(pos);
k.setY(height);
nodes.add(k);
if(k != null)
{
// go left
preOrder(k.getLeft(), pos*2-1, height+1);
// go right
preOrder(k.getRight(), pos*2, height+1);
}
}
public int getHeight(){
return getHeight(root);
}
private int getHeight(BinaryNode k){
if(k == null) return -1;
return 1 + Math.max(getHeight(k.getLeft()), getHeight(k.getRight()));
}
}