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 cc69d51

Browse files
Breadth first search tree traversal
1 parent 857afc4 commit cc69d51

File tree

5 files changed

+244
-0
lines changed

5 files changed

+244
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package pl.dmichalski.algorithms.data_structures._6_tree_traversal._1_breadth_first_search
2+
3+
class BinarySearchTree {
4+
5+
private var root: TreeNode? = null
6+
7+
fun insert(value: Int): BinarySearchTree {
8+
val node = TreeNode(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 breadthFistSearch(): List<Int> {
39+
if (this.root == null) {
40+
return mutableListOf()
41+
}
42+
43+
val data = mutableListOf<Int>()
44+
val queue = Queue()
45+
var node = this.root
46+
47+
queue.enqueue(node!!)
48+
while (queue.getSize() > 0) {
49+
node = queue.dequeue()
50+
data.add(node!!.getValue())
51+
if (node.getLeft() != null) {
52+
queue.enqueue(node.getLeft()!!)
53+
}
54+
if (node.getRight() != null) {
55+
queue.enqueue(node.getRight()!!)
56+
}
57+
}
58+
59+
return data
60+
}
61+
62+
fun getRoot(): TreeNode? {
63+
return root
64+
}
65+
66+
override fun toString(): String {
67+
return "Root: $root\n"
68+
}
69+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package pl.dmichalski.algorithms.data_structures._6_tree_traversal._1_breadth_first_search
2+
3+
class Node(private var treeNode: TreeNode) {
4+
5+
private var next: Node?
6+
7+
init {
8+
this.next = null
9+
}
10+
11+
fun getTreeNode(): TreeNode {
12+
return treeNode
13+
}
14+
15+
fun setTreeNode(treeNode: TreeNode) {
16+
this.treeNode = treeNode
17+
}
18+
19+
fun getNext(): Node? {
20+
return next
21+
}
22+
23+
fun setNext(nextNode: Node?) {
24+
next = nextNode
25+
}
26+
27+
override fun toString(): String {
28+
return "Node(treeNode='$treeNode', next=$next)"
29+
}
30+
31+
32+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package pl.dmichalski.algorithms.data_structures._6_tree_traversal._1_breadth_first_search
2+
3+
class Queue {
4+
5+
private var first: Node?
6+
private var last: Node?
7+
private var size: Int
8+
9+
init {
10+
this.first = null
11+
this.last = null
12+
this.size = 0
13+
}
14+
15+
fun enqueue(treeNode: TreeNode): Int {
16+
val node = Node(treeNode)
17+
18+
if (this.first == null) {
19+
this.first = node
20+
this.last = node
21+
} else {
22+
this.last!!.setNext(node)
23+
this.last = node
24+
}
25+
return ++this.size
26+
}
27+
28+
fun dequeue(): TreeNode? {
29+
if (this.size == 0) {
30+
return null
31+
}
32+
33+
val temp = this.first
34+
35+
if (this.first == this.last) {
36+
this.last = null
37+
}
38+
this.first = this.first!!.getNext()
39+
this.size--
40+
return temp!!.getTreeNode()
41+
}
42+
43+
fun getSize(): Int {
44+
return this.size
45+
}
46+
47+
override fun toString(): String {
48+
return "First: $first\n" +
49+
"Last: $last\n" +
50+
"Size: $size\n"
51+
}
52+
53+
54+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package pl.dmichalski.algorithms.data_structures._6_tree_traversal._1_breadth_first_search
2+
3+
object Runner {
4+
5+
@JvmStatic
6+
fun main(args: Array<String>) {
7+
println("------------------ Initial binary search tree ------------------ ")
8+
val binarySearchTree = getBinarySearchTree()
9+
printTree(binarySearchTree)
10+
11+
println("------------------ Breadth first search traversal ------------------ ")
12+
val breadthFistSearchTreeValues = binarySearchTree.breadthFistSearch()
13+
println("Breadth first search values: $breadthFistSearchTreeValues")
14+
}
15+
16+
/**
17+
* It returns BST like this:
18+
* <pre>
19+
* 4
20+
* / \
21+
* 2 7
22+
* / / \
23+
* 1 6 20
24+
* \
25+
* 23
26+
* </pre>
27+
*/
28+
private fun getBinarySearchTree(): BinarySearchTree {
29+
val binarySearchTree = BinarySearchTree()
30+
31+
binarySearchTree.insert(4)
32+
binarySearchTree.insert(2)
33+
binarySearchTree.insert(7)
34+
binarySearchTree.insert(1)
35+
binarySearchTree.insert(6)
36+
binarySearchTree.insert(20)
37+
binarySearchTree.insert(23)
38+
39+
return binarySearchTree
40+
}
41+
42+
private fun printTree(binarySearchTree: BinarySearchTree) {
43+
println(binarySearchTree)
44+
}
45+
}
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._6_tree_traversal._1_breadth_first_search
2+
3+
class TreeNode(private var value: Int) {
4+
5+
private var left: TreeNode?
6+
private var right: TreeNode?
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(): TreeNode? {
22+
return left
23+
}
24+
25+
fun setLeft(left: TreeNode) {
26+
this.left = left
27+
}
28+
29+
fun getRight(): TreeNode? {
30+
return right
31+
}
32+
33+
fun setRight(right: TreeNode) {
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+
}

0 commit comments

Comments
(0)

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