Skip to content

Commit 0218450

Browse files
authored
Split Tree Traversal Challange (#141)
1 parent 4f5cc52 commit 0218450

13 files changed

+695
-321
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ multiple times and be persistent over time.
103103
- [Get duplicated arguments](src/test/kotlin/com/igorwojda/string/getduplicatedarguments)
104104
- [Find pair with target average](src/test/kotlin/com/igorwojda/list/pairaverage)
105105
- [Binary search tree](src/test/kotlin/com/igorwojda/tree/binarysearchtree)
106-
- [Tree traversal](src/test/kotlin/com/igorwojda/tree/multiway/traversal)
106+
- [Tree traversal (breath first)](src/test/kotlin/com/igorwojda/tree/multiway/traversal/breathfirst)
107+
- [Tree traversal (depth first)](src/test/kotlin/com/igorwojda/tree/multiway/traversal/depthfirst)
107108
- [Tree level width](src/test/kotlin/com/igorwojda/tree/multiway/levelwidth)
108109
- [Binary search tree (insert)](src/test/kotlin/com/igorwojda/tree/binarytree/insert)
109110
- [Binary search tree (validate)](src/test/kotlin/com/igorwojda/tree/binarytree/validate)

misc/ChallengeGroups.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ List of problems that can be solved using [recursion](https://en.wikipedia.org/w
1919
- [Capitalize first](../src/test/kotlin/com/igorwojda/list/capitalizefirst)
2020
- [Merge sort](../src/test/kotlin/com/igorwojda/list/sort/mergesort)
2121
- [Quick sort](../src/test/kotlin/com/igorwojda/list/sort/quicksort)
22+
- [Tree traversal (depth first)](../src/test/kotlin/com/igorwojda/tree/multiway/traversal/depthfirst)
2223

2324
### Recursion with helper function
2425

@@ -166,12 +167,12 @@ We use sliding window instead of nested loops which decreases complexity from `O
166167

167168
- [Queue](../src/test/kotlin/com/igorwojda/queue/basic)
168169
- [Combine two queues](../src/test/kotlin/com/igorwojda/queue/combine)
169-
- [Tree traversal](../src/test/kotlin/com/igorwojda/tree/multiway/traversal)
170+
- [Tree traversal (breath first)](../src/test/kotlin/com/igorwojda/tree/multiway/traversal/breathfirst)
170171

171172
## Stack
172173

173174
- [Stack](../src/test/kotlin/com/igorwojda/stack/basic)
174-
- [Tree traversal](../src/test/kotlin/com/igorwojda/tree/multiway/traversal)
175+
- [Tree traversal (depth first)](../src/test/kotlin/com/igorwojda/tree/multiway/traversal/depthfirst)
175176

176177
## Heap
177178

src/test/kotlin/com/igorwojda/tree/multiway/traversal/Solution.kt

-188
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.igorwojda.tree.multiway.traversal.breathfirst
2+
3+
import org.amshove.kluent.shouldBeEqualTo
4+
import org.junit.jupiter.api.Test
5+
6+
private fun traverseBreathFirst(tree: BinarySearchTree<Char>): List<Char> {
7+
TODO("Add your solution here")
8+
}
9+
10+
private class BinarySearchTree<E : Comparable<E>> {
11+
var root: BinaryNode<E>? = null
12+
private set
13+
14+
fun add(element: E) {
15+
val newNode = BinaryNode(element)
16+
17+
if (root == null) {
18+
root = newNode
19+
return
20+
}
21+
22+
var current: BinaryNode<E> = root ?: return
23+
24+
while (true) {
25+
when {
26+
current.data == element -> {
27+
return
28+
}
29+
element < current.data -> {
30+
if (current.left == null) {
31+
current.left = newNode
32+
return
33+
}
34+
35+
current.left?.let { current = it }
36+
}
37+
element > current.data -> {
38+
if (current.right == null) {
39+
current.right = newNode
40+
return
41+
}
42+
43+
current.right?.let { current = it }
44+
}
45+
}
46+
}
47+
}
48+
49+
fun contains(element: E): Boolean {
50+
var current = root
51+
52+
while (true) {
53+
if (current == null) {
54+
break
55+
} else if (current.data == element) {
56+
return true
57+
} else if (element < current.data) {
58+
current = current.left
59+
} else if (element > current.data) {
60+
current = current.right
61+
}
62+
}
63+
64+
return false
65+
}
66+
67+
fun isEmpty() = root == null
68+
}
69+
70+
private data class BinaryNode<E : Comparable<E>>(
71+
val data: E,
72+
var left: BinaryNode<E>? = null,
73+
var right: BinaryNode<E>? = null,
74+
)
75+
76+
private class Test {
77+
// ---------Tree------------
78+
//
79+
// F
80+
// / \
81+
// B G
82+
// / \ \
83+
// A D I
84+
// / \ /
85+
// C E H
86+
//
87+
// --------------------------
88+
private fun getTree() = BinarySearchTree<Char>().apply {
89+
add('F')
90+
add('B')
91+
add('A')
92+
add('D')
93+
add('C')
94+
add('E')
95+
add('G')
96+
add('I')
97+
add('H')
98+
}
99+
100+
@Test
101+
fun `traverse breath first`() {
102+
traverseBreathFirst(getTree()) shouldBeEqualTo listOf(
103+
'F',
104+
'B',
105+
'G',
106+
'A',
107+
'D',
108+
'I',
109+
'C',
110+
'E',
111+
'H',
112+
)
113+
}
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Tree traversal
2+
3+
## Nice to solve before
4+
5+
[Queue](../../../../queue/basic/README.md)
6+
7+
## Instructions
8+
9+
Traverse [tree](https://en.wikipedia.org/wiki/Tree_(data_structure)) using `Breath-First` traversal.
10+
11+
Implement breath-first traversal - visit every node on a level before going to a lower level.
12+
13+
![breadth_first_traversal.svg](misc/breadth_first_traversal.svg)
14+
15+
## Examples
16+
17+
```kotlin
18+
// ---------Tree------------
19+
//
20+
// A
21+
// / \
22+
// B C
23+
//
24+
// --------------------------
25+
26+
27+
val tree = BinarySearchTree<Char>()
28+
tree.add('A')
29+
tree.add('B')
30+
tree.add('C')
31+
32+
tree.traverseBreathFirst() // listOf('A', 'B', 'C')
33+
```
34+

0 commit comments

Comments
 (0)