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 857afc4

Browse files
Binary Search Tree
1 parent d4deae7 commit 857afc4

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

‎src/main/kotlin/pl/dmichalski/algorithms/data_structures/_2_doubly_linked_list/Runner.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ object Runner {
3131
println("------------------ Getting the last element ------------------")
3232
doublyLinkedList = getDoublyLinkedLit()
3333
val lastElement = doublyLinkedList.get(doublyLinkedList.getLength() - 1)
34-
println("lastElement = ${lastElement}")
34+
println("lastElement = $lastElement")
3535

3636
println("\n------------------ Setting new first element ------------------ ")
3737
doublyLinkedList = getDoublyLinkedLit()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package pl.dmichalski.algorithms.data_structures._5_tree
2+
3+
class BinarySearchTree {
4+
5+
private var root: Node? = null
6+
7+
fun insert(value: Int): BinarySearchTree {
8+
val node = Node(value)
9+
10+
if (root == null) {
11+
root = node
12+
return this
13+
} else {
14+
var current = this.root
15+
while (true) {
16+
if (value < current!!.getValue()) {
17+
if (current.getLeft() == null) {
18+
current.setLeft(node)
19+
return this
20+
} else {
21+
current = current.getLeft()
22+
}
23+
} else if (value > current.getValue()) {
24+
if (current.getRight() == null) {
25+
current.setRight(node)
26+
return this
27+
} else {
28+
current = current.getRight()
29+
}
30+
} else {
31+
return this
32+
}
33+
}
34+
35+
}
36+
}
37+
38+
fun exists(value: Int): Boolean {
39+
if (root == null) {
40+
return false
41+
} else {
42+
var current = this.root
43+
while (true) {
44+
current = if (value < current!!.getValue()) {
45+
if (current.getLeft() == null) {
46+
return false
47+
} else {
48+
current.getLeft()
49+
}
50+
} else if (value > current.getValue()) {
51+
if (current.getRight() == null) {
52+
return false
53+
} else {
54+
current.getRight()
55+
}
56+
} else {
57+
return true
58+
}
59+
}
60+
61+
}
62+
}
63+
64+
fun getRoot(): Node? {
65+
return root
66+
}
67+
68+
override fun toString(): String {
69+
return "Root: $root\n"
70+
}
71+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package pl.dmichalski.algorithms.data_structures._5_tree
2+
3+
class Node(private var value: Int) {
4+
5+
private var left: Node?
6+
private var right: Node?
7+
8+
init {
9+
this.left = null
10+
this.right = null
11+
}
12+
13+
fun getValue(): Int {
14+
return value
15+
}
16+
17+
fun setValue(value: Int) {
18+
this.value = value
19+
}
20+
21+
fun getLeft(): Node? {
22+
return left
23+
}
24+
25+
fun setLeft(left: Node) {
26+
this.left = left
27+
}
28+
29+
fun getRight(): Node? {
30+
return right
31+
}
32+
33+
fun setRight(right: Node) {
34+
this.right = right
35+
}
36+
37+
override fun toString(): String {
38+
return "Node(value='$value', " +
39+
"left=$left, " +
40+
"right=$right)"
41+
}
42+
43+
44+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package pl.dmichalski.algorithms.data_structures._5_tree
2+
3+
object Runner {
4+
5+
@JvmStatic
6+
fun main(args: Array<String>) {
7+
println("------------------ Initial binary search tree ------------------ ")
8+
var binarySearchTree = getBinarySearchTree()
9+
printTree(binarySearchTree)
10+
11+
println("------------------ Searching for an element ------------------ ")
12+
binarySearchTree = getBinarySearchTree()
13+
val searchingNumber1 = 7
14+
val searchingNumber2 = 1
15+
val exists1 = binarySearchTree.exists(searchingNumber1)
16+
val exists2 = binarySearchTree.exists(searchingNumber2)
17+
println("Number $searchingNumber1 exists = $exists1")
18+
println("Number $searchingNumber2 exists = $exists2")
19+
}
20+
21+
private fun getBinarySearchTree(): BinarySearchTree {
22+
val binarySearchTree = BinarySearchTree()
23+
24+
binarySearchTree.insert(4)
25+
binarySearchTree.insert(2)
26+
binarySearchTree.insert(7)
27+
28+
return binarySearchTree
29+
}
30+
31+
private fun printTree(binarySearchTree: BinarySearchTree) {
32+
println(binarySearchTree)
33+
}
34+
}

0 commit comments

Comments
(0)

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