Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create post order,IN order, Pre order, DFS,BFS examples in Kotlin #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions DFS/BFS pre/post order examples in Kotlin
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
Node data structure

data class Node<T>(val value: T,
var leftNode: Node<T>?,
var rightNode: Node<T>?,
var depth: Int = 0) {
fun link(left: Node<T>?, right: Node<T>?) = this.apply {
linkLeft(left).linkRight(right)
}

fun linkLeft(left: Node<T>?) = this.apply { leftNode = left }

fun linkRight(right: Node<T>?) = this.apply { rightNode = right }

fun depth(value: Int) = this.apply { depth = value }

/**
* Nodes on the left are in yellow, and those on the right are blue.
*/
override fun toString(): String {
return StringBuffer().apply {
append("{${value.toString().green()}")
if (leftNode != null)
append(", ${leftNode.toString().yellow()}")
if (rightNode != null)
append(", ${rightNode.toString().blue()}}")
}.toString()
}
}

Building the tree

/**
* [Image of the generated tree
* [A]
* / \
* [B] [C]
* / \ / \
* [D] [E] [F] [G]
* / \
* [H] [I]
*/
fun buildTree(): Node<Char> {
val a = Node('a', null, null)
val b = Node('b', null, null)
val c = Node('c', null, null)
val d = Node('d', null, null)
val e = Node('e', null, null)
val f = Node('f', null, null)
val g = Node('g', null, null)
val h = Node('h', null, null)
val i = Node('i', null, null)

a.link(b, c)
b.link(d, e)
c.link(f, g)
g.link(h, i)

return a
}

Pre-order recursive traversal
fun <T> traversalPreOrder(node: Node<T>?, list: MutableList<T>) {
if (node != null) {
list.add(node.value)
traversalPreOrder(node.leftNode, list)
traversalPreOrder(node.rightNode, list)
}
}
in-order traversals
fun <T> traversalInOrder(node: Node<T>?, list: MutableList<T>) {
if (node != null) {
traversalInOrder(node.leftNode, list)
list.add(node.value)
traversalInOrder(node.rightNode, list)
}
}
post-order traversals
fun <T> traversalPostOrder(node: Node<T>?, list: MutableList<T>) {
if (node != null) {
traversalPostOrder(node.leftNode, list)
traversalPostOrder(node.rightNode, list)
list.add(node.value)
}
}
BFS (breadth first search) using a Queue
fun <T> breadthFirstTraversal(root: Node<T>): MutableList<Node<T>> {
val queue = LinkedList<Node<T>>()
val traversalList = mutableListOf<Node<T>>()

// Add first node.
queue.add(root)

// Use queue to create breadth first traversal.
while (queue.isNotEmpty()) {
val currentNode = queue.poll()
val depth = currentNode.depth

// Add left node first.
if (currentNode.leftNode != null)
queue.add(currentNode.leftNode!!.depth(depth + 1))

// Add right node next.
if (currentNode.rightNode != null)
queue.add(currentNode.rightNode!!.depth(depth + 1))

// Add the node to the traversal list.
traversalList.add(currentNode)
}

return traversalList
}
DFS (depth first search) using a Stack
fun <T> depthFirstTraversal(root: Node<T>): MutableList<Node<T>> {
val stack = LinkedList<Node<T>>()
val traversalList = mutableListOf<Node<T>>()

// Add first node.
stack.push(root)

// Use stack to create breadth first traversal.
while (stack.isNotEmpty()) {
val currentNode = stack.pop()
val depth = currentNode.depth

// Push right child to stack FIRST (so this will be processed LAST).
if (currentNode.rightNode != null)
stack.push(currentNode.rightNode!!.depth(depth + 1))

// Push left child to stack LAST (so this will be processed FIRST).
if (currentNode.leftNode != null)
stack.push(currentNode.leftNode!!.depth(depth + 1))

// Add to traversal list.
traversalList.add(currentNode)
}

return traversalList
}