diff --git a/DFS/BFS pre/post order examples in Kotlin b/DFS/BFS pre/post order examples in Kotlin new file mode 100644 index 0000000..b0d2c60 --- /dev/null +++ b/DFS/BFS pre/post order examples in Kotlin @@ -0,0 +1,139 @@ +Node data structure + +data class Node(val value: T, + var leftNode: Node?, + var rightNode: Node?, + var depth: Int = 0) { + fun link(left: Node?, right: Node?) = this.apply { + linkLeft(left).linkRight(right) + } + + fun linkLeft(left: Node?) = this.apply { leftNode = left } + + fun linkRight(right: Node?) = 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 { + 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 traversalPreOrder(node: Node?, list: MutableList) { + if (node != null) { + list.add(node.value) + traversalPreOrder(node.leftNode, list) + traversalPreOrder(node.rightNode, list) + } +} +in-order traversals +fun traversalInOrder(node: Node?, list: MutableList) { + if (node != null) { + traversalInOrder(node.leftNode, list) + list.add(node.value) + traversalInOrder(node.rightNode, list) + } +} +post-order traversals +fun traversalPostOrder(node: Node?, list: MutableList) { + if (node != null) { + traversalPostOrder(node.leftNode, list) + traversalPostOrder(node.rightNode, list) + list.add(node.value) + } +} +BFS (breadth first search) using a Queue +fun breadthFirstTraversal(root: Node): MutableList> { + val queue = LinkedList>() + val traversalList = mutableListOf>() + + // 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 depthFirstTraversal(root: Node): MutableList> { + val stack = LinkedList>() + val traversalList = mutableListOf>() + + // 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 +}