Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8945005

Browse files
refactored BinaryTree
1 parent 7ec0bf4 commit 8945005

File tree

2 files changed

+235
-148
lines changed

2 files changed

+235
-148
lines changed
Lines changed: 112 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,72 @@
11
package structures
22

3+
import java.util.LinkedList
4+
35
/**
46
*
5-
* data structure: binary tree
7+
* Binary tree consists of nodes each of which has a maximum of two children.
8+
*
9+
* Child nodes satisfy the following requirements:
10+
*
11+
* - the left child is less than the parent
12+
* - right child is larger than parent
613
*
7-
* description: consists of nodes, each of which has a maximum of two children,
8-
* child nodes satisfy the following requirements:
9-
* - the left child is less than the parent;
10-
* - right child is larger than parent;
14+
* Hint: the worst time may be O(n) because the situation is possible when the elements follow each other 1,2,3,4...
15+
* and the tree takes the following form:
1116
*
12-
* average search time: log(n)
13-
* worst search time: n
14-
* because the situation is possible when the elements follow each other 1,2,3,4... and the tree takes the following form:
1517
* 1
1618
* \
1719
* 2
1820
* \
1921
* 3
2022
* \
2123
* 4
22-
* the same complexity is true for adding and removing nodes
2324
*
2425
*/
2526

26-
class BinaryTree {
27+
class BinaryTree<T:Comparable<T>> {
2728

28-
/**
29-
* binary tree root
30-
*/
31-
privatevar root:Node?= null
29+
privatevar root:Node<T>?=null
30+
31+
val isEmpty:Boolean
32+
get() = root == null
3233

3334
/**
34-
* adds a new element [value] to the tree
35+
* Complexity:
36+
* worst time: O(n), read the hint in the description
37+
* best time: O(log(n))
38+
* average time: O(log(n))
3539
*/
36-
fun add(value: Int) {
37-
fun addRec(current: Node?, value: Int) : Node {
40+
fun add(value: T) {
41+
fun addRecursive(current: Node<T>?, value: T): Node<T> {
3842
if (current == null) {
3943
return Node(value)
4044
}
4145
if (value < current.value()) {
42-
current.changeLeft(addRec(current.leftNode(), value))
46+
current.changeLeft(addRecursive(current.leftNode(), value))
4347
} else if (value > current.value()) {
44-
current.changeRight(addRec(current.rightNode(), value))
48+
current.changeRight(addRecursive(current.rightNode(), value))
4549
}
4650
return current
4751
}
4852

49-
root = addRec(root, value)
53+
root = addRecursive(root, value)
5054
}
5155

5256
/**
53-
* checks the tree for emptiness and returns true if the tree does not contain any nodes
57+
* Complexity:
58+
* worst time: O(n), read the hint in the description
59+
* best time: O(1)
60+
* average time: O(log(n))
5461
*/
55-
fun isEmpty() = root == null
56-
57-
/**
58-
* removes an element [value] from the tree
59-
*/
60-
fun remove(value: Int) {
61-
fun smallestValue(root: Node) : Int {
62-
return if (root.leftNode() == null) root.value() else smallestValue(root.leftNode()!!)
62+
fun remove(value: T) {
63+
fun smallestValue(root: Node<T>): T {
64+
val leftNode = root.leftNode()
65+
if (leftNode === null) return root.value()
66+
return smallestValue(leftNode)
6367
}
6468

65-
fun removeRec(current: Node?, value: Int) : Node? {
69+
fun removeRecursive(current: Node<T>?, value: T): Node<T>? {
6670
if (current == null) {
6771
return null
6872
}
@@ -80,180 +84,164 @@ class BinaryTree {
8084

8185
val smallestValue = smallestValue(current.rightNode()!!)
8286
current.changeValue(smallestValue)
83-
current.changeRight(removeRec(current.rightNode(), smallestValue))
87+
current.changeRight(removeRecursive(current.rightNode(), smallestValue))
8488
return current
8589
}
8690

8791
if (value < current.value()) {
88-
current.changeLeft(removeRec(current.leftNode(), value))
92+
current.changeLeft(removeRecursive(current.leftNode(), value))
8993
} else {
90-
current.changeRight(removeRec(current.rightNode(), value))
94+
current.changeRight(removeRecursive(current.rightNode(), value))
9195
}
9296

9397
return current
9498
}
9599

96-
root = removeRec(root, value)
100+
root = removeRecursive(root, value)
97101
}
98102

99103
/**
100-
* checks for the existence of an element [value] in the tree, returns true if the element exists
104+
* Complexity:
105+
* worst time: O(n), read the hint in the description
106+
* best time: O(1)
107+
* average time: O(log(n))
101108
*/
102-
fun contains(value: Int) : Boolean {
103-
fun containsRec(current: Node?, value: Int) : Boolean {
109+
fun contains(value: T): Boolean {
110+
fun containsRecursive(current: Node<T>?, value: T): Boolean {
104111
if (current == null) {
105112
return false
106113
}
107114
if (value == current.value()) {
108115
return true
109116
}
110117
return if (value < current.value()) {
111-
containsRec(current.leftNode(), value)
118+
containsRecursive(current.leftNode(), value)
112119
} else {
113-
containsRec(current.rightNode(), value)
120+
containsRecursive(current.rightNode(), value)
114121
}
115122
}
116123

117-
return containsRec(root, value)
124+
return containsRecursive(root, value)
118125
}
119126

120127
/**
121-
* traversal of the binary tree in depth
122128
*
123-
* first the left child, then the parent, then the right child
129+
* Traversal of the binary tree in depth
130+
*
131+
* order: the left child, the parent, the right child
124132
*
125-
* @return returns the elements of the tree
126133
*/
127-
fun traverseInOrder(): List<Int> {
128-
fun traverseInOrderRec(node: Node?, nodes: MutableList<Int>) {
134+
fun traverseInOrder(): List<T> {
135+
fun traverseInOrderRecursive(node: Node<T>?, nodes: MutableList<T>) {
129136
if (node != null) {
130-
traverseInOrderRec(node.leftNode(), nodes)
137+
traverseInOrderRecursive(node.leftNode(), nodes)
131138
nodes.add(node.value())
132-
traverseInOrderRec(node.rightNode(), nodes)
139+
traverseInOrderRecursive(node.rightNode(), nodes)
133140
}
134141
}
135142

136-
returnmutableListOf<Int>().apply {
137-
traverseInOrderRec(root, this)
138-
}
143+
val nodes =mutableListOf<T>()
144+
traverseInOrderRecursive(root, nodes)
145+
return nodes
139146
}
140147

141148
/**
142-
* traversal of the binary tree in depth
143149
*
144-
* parent first, then left and right children
150+
* Traversal of the binary tree in depth
151+
*
152+
* order: the parent, the left child, the right child
145153
*
146-
* @return returns the elements of the tree
147154
*/
148-
fun traversePreOrder(): List<Int> {
149-
fun traversePreOrderRec(node: Node?, nodes: MutableList<Int>) {
155+
fun traversePreOrder(): List<T> {
156+
fun traversePreOrderRecursive(node: Node<T>?, nodes: MutableList<T>) {
150157
if (node != null) {
151158
nodes.add(node.value())
152-
traversePreOrderRec(node.leftNode(), nodes)
153-
traversePreOrderRec(node.rightNode(), nodes)
159+
traversePreOrderRecursive(node.leftNode(), nodes)
160+
traversePreOrderRecursive(node.rightNode(), nodes)
154161
}
155162
}
156163

157-
returnmutableListOf<Int>().apply {
158-
traversePreOrderRec(root, this)
159-
}
164+
val nodes =mutableListOf<T>()
165+
traversePreOrderRecursive(root, nodes)
166+
return nodes
160167
}
161168

162169
/**
163-
* traversal of the binary tree in depth
164170
*
165-
* first the left and right children, then the parent
171+
* Traversal of the binary tree in depth
172+
*
173+
* order: the left child, the right child, the parent
166174
*
167-
* @return returns the elements of the tree
168175
*/
169-
fun traversePostOrder(): List<Int> {
170-
fun traversePostOrderRec(node: Node?, nodes: MutableList<Int>) {
176+
fun traversePostOrder(): List<T> {
177+
fun traversePostOrderRec(node: Node<T>?, nodes: MutableList<T>) {
171178
if (node != null) {
172179
traversePostOrderRec(node.leftNode(), nodes)
173180
traversePostOrderRec(node.rightNode(), nodes)
174181
nodes.add(node.value())
175182
}
176183
}
177184

178-
returnmutableListOf<Int>().apply {
179-
traversePostOrderRec(root, this)
180-
}
185+
val nodes =mutableListOf<T>()
186+
traversePostOrderRec(root, nodes)
187+
return nodes
181188
}
182189

183190
/**
184-
* traversal of the binary tree in breadth
185191
*
186-
* uses an additional data structure - a queue into which new tree
192+
* Traversal of the binary tree in breadth uses an additional data structure - a queue into which new tree
193+
*
187194
* nodes are added until the last node is added
188195
*
189-
* @return returns the elements of the tree
190196
*/
191-
fun traverseLevelOrder(): List<Int> {
192-
val root = this.root ?: return listOf()
197+
fun traverseLevelOrder(): List<T> {
198+
val current = root ?: return emptyList()
193199

194-
val queue = java.util.LinkedList<Node>()
195-
queue.add(root)
200+
val queue = LinkedList<Node<T>>()
201+
queue.add(current)
196202

197-
val items = mutableListOf<Int>()
203+
val nodeValues = mutableListOf<T>()
198204

199205
while (queue.isNotEmpty()) {
200-
val node = queue.remove()
201-
items.add(node.value())
206+
val node = queue.removeFirst()
202207

203-
node.leftNode()?.let(queue::add)
204-
node.rightNode()?.let(queue::add)
208+
nodeValues.add(node.value())
209+
210+
val leftNode = node.leftNode()
211+
if (leftNode != null) {
212+
queue.add(leftNode)
213+
}
214+
215+
val rightNode = node.rightNode()
216+
if (rightNode != null) {
217+
queue.add(rightNode)
218+
}
205219
}
206220

207-
return items
221+
return nodeValues
208222
}
209223

210-
}
224+
class Node<T>(
225+
private var value: T,
226+
private var left: Node<T>? = null,
227+
private var right: Node<T>? = null
228+
) {
211229

212-
/**
213-
* represents a tree node
214-
*
215-
* @property value - node value
216-
* @property left - left child node
217-
* @property right - right child node
218-
*/
219-
class Node(
220-
private var value: Int,
221-
private var left: Node? = null,
222-
private var right: Node? = null
223-
) {
224-
/**
225-
* returns the value of the node
226-
*/
227-
fun value() = value
230+
fun value() = value
231+
fun changeValue(newValue: T) {
232+
value = newValue
233+
}
228234

229-
/**
230-
* changes the value of a node
231-
*/
232-
fun changeValue(value: Int) {
233-
this.value = value
234-
}
235+
fun leftNode() = left
236+
fun changeLeft(node: Node<T>?) {
237+
left = node
238+
}
235239

236-
/**
237-
* changes the left child node
238-
*/
239-
fun changeLeft(left: Node?) {
240-
this.left = left
241-
}
240+
fun rightNode() = right
241+
fun changeRight(node: Node<T>?) {
242+
right = node
243+
}
242244

243-
/**
244-
* changes the right child node
245-
*/
246-
fun changeRight(right: Node?) {
247-
this.right = right
248245
}
249246

250-
/**
251-
* returns the left child node
252-
*/
253-
fun leftNode() = left
254-
255-
/**
256-
* returns the right child node
257-
*/
258-
fun rightNode() = right
259247
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /