@@ -4,26 +4,27 @@ object TreeTraversal {
44
55 class Tree (val rowCount : Int , val childrenCount : Int ) {
66
7- private case class Node (var id : Int ) {
7+ private case class Node (var id : String ) {
88
99 var children = ListBuffer [Node ]()
1010 }
1111
12- private val root : Node = Node (1 )
12+ private val root : Node = Node (" root" )
13+
1314 createAllChildren(root, rowCount, childrenCount)
1415
15- private def createAllChildren (node : Node , rowCount : Int , childrenCount : Int ): Unit = {
16+ private def createAllChildren (node : Node , rowCount : Int = 0 , childrenCount : Int = 0 ): Unit = {
1617 if (rowCount <= 1 ) return
1718
1819 0 until childrenCount foreach { i =>
19- node.children += Node (node.id * 10 + i + 1 )
20+ node.children += Node (node.id + " - " + i )
2021 createAllChildren(node.children(i), rowCount - 1 , childrenCount)
2122 }
2223 }
2324
2425 private def doSomethingWithNode (node : Node ) = Console .println(node.id)
2526
26- def dfsRecursive () : Unit = {
27+ def dfsRecursive : Unit = {
2728 def dfsRecursive (node : Node ): Unit = {
2829 doSomethingWithNode(node)
2930 node.children.foreach(dfsRecursive)
@@ -32,7 +33,7 @@ object TreeTraversal {
3233 dfsRecursive(root)
3334 }
3435
35- def dfsRecursivePostOrder () : Unit = {
36+ def dfsRecursivePostOrder : Unit = {
3637 def dfsRecursivePostOrder (node : Node ): Unit = {
3738 node.children.foreach(dfsRecursivePostOrder)
3839 doSomethingWithNode(node)
@@ -41,7 +42,7 @@ object TreeTraversal {
4142 dfsRecursivePostOrder(root)
4243 }
4344
44- def dfsRecursiveInOrderBinary () : Unit = {
45+ def dfsRecursiveInOrderBinary : Unit = {
4546 def processIfChildExists (children : ListBuffer [Node ], index : Int ) =
4647 if (children.isDefinedAt(index))
4748 dfsRecursiveInOrderBinary(children(index))
@@ -58,50 +59,48 @@ object TreeTraversal {
5859 dfsRecursiveInOrderBinary(this .root)
5960 }
6061
61- def dfsStack () : Unit = {
62+ def dfsStack : Unit = {
6263 val stack = new ArrayBuffer [Node ]()
6364 stack += root
6465 while (stack.nonEmpty) {
6566 doSomethingWithNode(stack(0 ))
66- stack ++= stack.remove(0 ).children
67+ val firstNode = stack.remove(0 )
68+ stack ++= firstNode.children
6769 }
6870 }
6971
70- def bfsQueue () : Unit = {
72+ def bfsQueue : Unit = {
7173 val queue = new Queue [Node ]()
7274 queue.enqueue(root)
7375 while (queue.nonEmpty) {
7476 doSomethingWithNode(queue.head)
75- queue ++= queue.dequeue.children
77+ val firstNode = queue.dequeue()
78+ queue ++= firstNode.children
7679 }
7780 }
7881
7982 }
8083
8184 def main (args : Array [String ]): Unit = {
8285 Console .println(" Creating Tree" )
83- var tree = new Tree (3 , 3 )
86+ var theTree = new Tree (3 , 3 )
8487
8588 Console .println(" Using recursive DFS :" )
86- tree .dfsRecursive
89+ theTree .dfsRecursive
8790
8891 Console .println(" Using stack-based DFS :" )
89- tree .dfsStack
92+ theTree .dfsStack
9093
9194 Console .println(" Using queue-based BFS :" )
92- tree .bfsQueue
95+ theTree .bfsQueue
9396
9497 Console .println(" Using post-order recursive DFS :" )
95- tree.dfsRecursivePostOrder
96-
97- // Uncommenting the following 2 lines will result in an exception thrown because at least one Node of the Tree
98- // has more than 2 children and therefor a DFSRecursiveInorderBinary doesn't work.
99- Console .println(" Using in-order binary recursive DFS : (fail)" )
100- tree.dfsRecursiveInOrderBinary
101-
102- tree = new Tree (3 , 2 )
103- Console .println(" Using in-order binary recursive DFS : (succeed)" )
104- tree.dfsRecursiveInOrderBinary
98+ theTree.dfsRecursivePostOrder
99+
100+ // Create a binary tree to test inOrder traversal
101+ theTree = new Tree (3 , 2 )
102+ Console .println(" Using in-order binary recursive DFS :" )
103+ theTree.dfsRecursiveInOrderBinary
105104 }
106105
107- }
106+ }
0 commit comments