-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchTree.java
More file actions
180 lines (167 loc) · 3.94 KB
/
SearchTree.java
File metadata and controls
180 lines (167 loc) · 3.94 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
/**
* Binary Search tree implementation.
*/
package searchtree;
import java.util.ArrayList;
import java.util.Arrays;
public class SearchTree {
//Root element.
Node root;
/**
* Sets the tree, calls functions and prints results.
* @param args Array of arguments for launching the program. Ignored.
*/
public static void main(String[] args) {
SearchTree tree = new SearchTree();
tree.add(10);
tree.add(20);
tree.add(2);
tree.add(29);
tree.add(21);
tree.add(11);
tree.add(1);
System.out.println(tree);
System.out.println("Removing 29");
tree.remove(29);
System.out.println(tree);
System.out.println("Removing 21");
tree.remove(21);
System.out.println(tree);
tree.add(12);
tree.add(13);
tree.add(3);
System.out.println("Adding 3 more elements");
System.out.println(tree);
System.out.println("Removing 10");
tree.remove(10);
System.out.println(tree);
System.out.println("Removing 2");
tree.remove(2);
System.out.println(tree);
}
/**
* Adds an element to the tree.
* @param element Node to add to the heap.
*/
public void add(int value) {
Node n = new Node(value);
if (root == null) {
root = n;
return;
}
Node curElem = root;
while (true) {
if (curElem.value > value) {
if (curElem.leftChild != null) {
curElem = curElem.leftChild;
} else {
curElem.addChild(n);
break;
}
} else {
if (curElem.rightChild != null) {
curElem = curElem.rightChild;
} else {
curElem.addChild(n);
break;
}
}
}
}
/**
* Removes element by value.
* @param value Value of the node to remove.
*/
public void remove(int value) {
Node n = findByValue(value);
if (n == null) {
return;
}
if (n.childNum() == 0) {
if (n == root) {
root = null;
} else {
n.parent.removeChild(n);
}
return;
}
if (n.childNum() == 1) {
if (n == root) {
root = n.getTheOnlyChild();
} else {
n.parent.removeChild(n);
n.parent.addChild(n.getTheOnlyChild());
}
return;
}
Node localMax = getMaxFrom(n);
remove(localMax.value);
n.value = localMax.value;
Node curElem = n;
while (true) {
if (curElem.rightChild == null ||
curElem.value < curElem.rightChild.value) {
return;
}
int tmp = curElem.rightChild.value;
curElem.rightChild.value = curElem.value;
curElem.value = tmp;
curElem = curElem.rightChild;
}
}
/**
* Gets the maximum starting from the node.
* @param n Node to start with.
* @return Node with the maximum value.
*/
Node getMaxFrom(Node n) {
Node curElem = n;
while (curElem.rightChild != null) {
curElem = curElem.rightChild;
}
return curElem;
}
/**
* Finds the node in a tree by the value provided.
* @param value Value to search for.
* @return Node or null if not found.
*/
public Node findByValue(int value) {
Node curElem = root;
while (curElem != null) {
if (curElem.value == value) {
return curElem;
}
if (curElem.value > value) {
curElem = curElem.leftChild;
} else {
curElem = curElem.rightChild;
}
}
return null;
}
/**
* Returns the tree as a string starting with the root value.
* @return String to print.
*/
@Override
public String toString() {
String result = "";
if (root == null) {
return "";
}
ArrayList<Node> queue = new ArrayList<Node>();
queue.add(root);
while (queue.size() > 0) {
Node curElem = queue.remove(0);
result += curElem.toString() + "\n";
if (curElem.leftChild != null) {
queue.add(curElem.leftChild);
}
if (curElem.rightChild != null) {
queue.add(curElem.rightChild);
}
}
return result;
}
}