-
Notifications
You must be signed in to change notification settings - Fork 108
/
BinaryTree.kt
247 lines (211 loc) · 6.45 KB
/
BinaryTree.kt
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package structures
import java.util.LinkedList
/**
*
* Binary tree consists of nodes each of which has a maximum of two children.
*
* Child nodes satisfy the following requirements:
*
* - the left child is less than the parent
* - right child is larger than parent
*
* Hint: the worst time may be O(n) because the situation is possible when the elements follow each other 1,2,3,4...
* and the tree takes the following form:
*
* 1
* \
* 2
* \
* 3
* \
* 4
*
*/
class BinaryTree<T : Comparable<T>> {
private var root: Node<T>? = null
val isEmpty: Boolean
get() = root == null
/**
* Complexity:
* worst time: O(n), read the hint in the description
* best time: O(log(n))
* average time: O(log(n))
*/
fun add(value: T) {
fun addRecursive(current: Node<T>?, value: T): Node<T> {
if (current == null) {
return Node(value)
}
if (value < current.value()) {
current.changeLeft(addRecursive(current.leftNode(), value))
} else if (value > current.value()) {
current.changeRight(addRecursive(current.rightNode(), value))
}
return current
}
root = addRecursive(root, value)
}
/**
* Complexity:
* worst time: O(n), read the hint in the description
* best time: O(1)
* average time: O(log(n))
*/
fun remove(value: T) {
fun smallestValue(root: Node<T>): T {
val leftNode = root.leftNode()
if (leftNode === null) return root.value()
return smallestValue(leftNode)
}
fun removeRecursive(current: Node<T>?, value: T): Node<T>? {
if (current == null) {
return null
}
if (value == current.value()) {
if (current.leftNode() == null && current.rightNode() == null) {
return null
}
if (current.leftNode() == null) {
return current.rightNode()
}
if (current.rightNode() == null) {
return current.leftNode()
}
val smallestValue = smallestValue(current.rightNode()!!)
current.changeValue(smallestValue)
current.changeRight(removeRecursive(current.rightNode(), smallestValue))
return current
}
if (value < current.value()) {
current.changeLeft(removeRecursive(current.leftNode(), value))
} else {
current.changeRight(removeRecursive(current.rightNode(), value))
}
return current
}
root = removeRecursive(root, value)
}
/**
* Complexity:
* worst time: O(n), read the hint in the description
* best time: O(1)
* average time: O(log(n))
*/
fun contains(value: T): Boolean {
fun containsRecursive(current: Node<T>?, value: T): Boolean {
if (current == null) {
return false
}
if (value == current.value()) {
return true
}
return if (value < current.value()) {
containsRecursive(current.leftNode(), value)
} else {
containsRecursive(current.rightNode(), value)
}
}
return containsRecursive(root, value)
}
/**
*
* Traversal of the binary tree in depth
*
* order: the left child, the parent, the right child
*
*/
fun traverseInOrder(): List<T> {
fun traverseInOrderRecursive(node: Node<T>?, nodes: MutableList<T>) {
if (node != null) {
traverseInOrderRecursive(node.leftNode(), nodes)
nodes.add(node.value())
traverseInOrderRecursive(node.rightNode(), nodes)
}
}
val nodes = mutableListOf<T>()
traverseInOrderRecursive(root, nodes)
return nodes
}
/**
*
* Traversal of the binary tree in depth
*
* order: the parent, the left child, the right child
*
*/
fun traversePreOrder(): List<T> {
fun traversePreOrderRecursive(node: Node<T>?, nodes: MutableList<T>) {
if (node != null) {
nodes.add(node.value())
traversePreOrderRecursive(node.leftNode(), nodes)
traversePreOrderRecursive(node.rightNode(), nodes)
}
}
val nodes = mutableListOf<T>()
traversePreOrderRecursive(root, nodes)
return nodes
}
/**
*
* Traversal of the binary tree in depth
*
* order: the left child, the right child, the parent
*
*/
fun traversePostOrder(): List<T> {
fun traversePostOrderRec(node: Node<T>?, nodes: MutableList<T>) {
if (node != null) {
traversePostOrderRec(node.leftNode(), nodes)
traversePostOrderRec(node.rightNode(), nodes)
nodes.add(node.value())
}
}
val nodes = mutableListOf<T>()
traversePostOrderRec(root, nodes)
return nodes
}
/**
*
* Traversal of the binary tree in breadth uses an additional data structure - a queue into which new tree
*
* nodes are added until the last node is added
*
*/
fun traverseLevelOrder(): List<T> {
val current = root ?: return emptyList()
val queue = LinkedList<Node<T>>()
queue.add(current)
val nodeValues = mutableListOf<T>()
while (queue.isNotEmpty()) {
val node = queue.removeFirst()
nodeValues.add(node.value())
val leftNode = node.leftNode()
if (leftNode != null) {
queue.add(leftNode)
}
val rightNode = node.rightNode()
if (rightNode != null) {
queue.add(rightNode)
}
}
return nodeValues
}
class Node<T>(
private var value: T,
private var left: Node<T>? = null,
private var right: Node<T>? = null
) {
fun value() = value
fun changeValue(newValue: T) {
value = newValue
}
fun leftNode() = left
fun changeLeft(node: Node<T>?) {
left = node
}
fun rightNode() = right
fun changeRight(node: Node<T>?) {
right = node
}
}
}