-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.kt
270 lines (236 loc) · 6.82 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package structures
/**
* data structure: binary tree
*
* description: 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;
*
* average search time: log(n)
* worst search time: 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
* the same complexity is true for adding and removing nodes
*
*/
class BinaryTree {
/**
* binary tree root
*/
private var root: Node? = null
/**
* adding a new element to the tree
*
* @param value - element value
*/
fun add(value: Int) {
fun addRec(current: Node?, value: Int) : Node {
if (current == null) {
return Node(value)
}
if (value < current.value()) {
current.changeLeft(addRec(current.leftNode(), value))
} else if (value > current.value()) {
current.changeRight(addRec(current.rightNode(), value))
}
return current
}
root = addRec(root, value)
}
/**
* checks the tree for emptiness
*
* @return returns true if the tree does not contain any nodes
*/
fun isEmpty() = root == null
/**
* removing an element from the tree
*
* @param value - the value of the element to be removed
*/
fun remove(value: Int) {
fun smallestValue(root: Node) : Int {
return if (root.leftNode() == null) root.value() else smallestValue(root.leftNode()!!)
}
fun removeRec(current: Node?, value: Int) : Node? {
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(removeRec(current.rightNode(), smallestValue))
return current
}
if (value < current.value()) {
current.changeLeft(removeRec(current.leftNode(), value))
} else {
current.changeRight(removeRec(current.rightNode(), value))
}
return current
}
root = removeRec(root, value)
}
/**
* checking for the existence of an element in the tree
*
* @param value - element value
*
* @return - returns true if the element exists
*/
fun contains(value: Int) : Boolean {
fun containsRec(current: Node?, value: Int) : Boolean {
if (current == null) {
return false
}
if (value == current.value()) {
return true
}
return if (value < current.value()) {
containsRec(current.leftNode(), value)
} else {
containsRec(current.rightNode(), value)
}
}
return containsRec(root, value)
}
/**
* traversal of the binary tree in depth
*
* first the left child, then the parent, then the right child
*
* @return returns the elements of the tree
*/
fun traverseInOrder() : List<Int> {
fun traverseInOrderRec(node: Node?, nodes: MutableList<Int>) {
if (node != null) {
traverseInOrderRec(node.leftNode(), nodes)
nodes.add(node.value())
traverseInOrderRec(node.rightNode(), nodes)
}
}
return mutableListOf<Int>().apply {
traverseInOrderRec(root, this)
}
}
/**
* traversal of the binary tree in depth
*
* parent first, then left and right children
*
* @return returns the elements of the tree
*/
fun traversePreOrder() : List<Int> {
fun traversePreOrderRec(node: Node?, nodes: MutableList<Int>) {
if (node != null) {
nodes.add(node.value())
traversePreOrderRec(node.leftNode(), nodes)
traversePreOrderRec(node.rightNode(), nodes)
}
}
return mutableListOf<Int>().apply {
traversePreOrderRec(root, this)
}
}
/**
* traversal of the binary tree in depth
*
* first the left and right children, then the parent
*
* @return returns the elements of the tree
*/
fun traversePostOrder() : List<Int> {
fun traversePostOrderRec(node: Node?, nodes: MutableList<Int>) {
if (node != null) {
traversePostOrderRec(node.leftNode(), nodes)
traversePostOrderRec(node.rightNode(), nodes)
nodes.add(node.value())
}
}
return mutableListOf<Int>().apply {
traversePostOrderRec(root, this)
}
}
/**
* 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
*
* @return returns the elements of the tree
*/
fun traverseLevelOrder() : List<Int> {
val root = this.root ?: return listOf()
val queue = java.util.LinkedList<Node>()
queue.add(root)
val items = mutableListOf<Int>()
while (queue.isNotEmpty()) {
val node = queue.remove()
items.add(node.value())
node.leftNode()?.let(queue::add)
node.rightNode()?.let(queue::add)
}
return items
}
}
/**
* represents a tree node
*
* @constructor
* @property value - node value
* @property left - left child node
* @property right - right child node
*
*/
class Node(
private var value: Int,
private var left: Node? = null,
private var right: Node? = null
) {
/**
* returns the value of the node
*/
fun value() = value
/**
* changes the value of a node
*/
fun changeValue(value: Int) {
this.value = value
}
/**
* changes the left child node
*/
fun changeLeft(left: Node?) {
this.left = left
}
/**
* changes the right child node
*/
fun changeRight(right: Node?) {
this.right = right
}
/**
* returns the left child node
*/
fun leftNode() = left
/**
* returns the right child node
*/
fun rightNode() = right
}