-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateBSTFromRange.kt
More file actions
37 lines (33 loc) · 875 Bytes
/
CreateBSTFromRange.kt
File metadata and controls
37 lines (33 loc) · 875 Bytes
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
package exercices.binarySearchTrees
/**
* Creates a binary search tree from a range of numbers
* Complexity O(n)
*
* @param start Int
* @param end Int
* @return Returns a binary search tree from a range of numbers
*/
fun createBSTFromRange(start: Int, end: Int): Node<Int>? {
//non-existing interval
if (end - start <= 0) return null
//variable initialization
val mid = end - (end - start) / 2
val interval = (start..end)
var leftElem = mid - 1
var rightElem = mid + 1
val root = Node(mid, null, null)
var parent = root
//loop to go through every element in interval
while (parent.item in interval) {
if (parent.left == null) {
parent.left = Node(leftElem, null, null)
leftElem--
} else {
parent.right = Node(rightElem, null, null)
rightElem++
}
parent = if (isComplete(parent)) parent.left!!
else parent.right!!
}
return root
}