@@ -4,26 +4,27 @@ object TreeTraversal {
4
4
5
5
class Tree (val rowCount : Int , val childrenCount : Int ) {
6
6
7
- private case class Node (var id : Int ) {
7
+ private case class Node (var id : String ) {
8
8
9
9
var children = ListBuffer [Node ]()
10
10
}
11
11
12
- private val root : Node = Node (1 )
12
+ private val root : Node = Node (" root" )
13
+
13
14
createAllChildren(root, rowCount, childrenCount)
14
15
15
- private def createAllChildren (node : Node , rowCount : Int , childrenCount : Int ): Unit = {
16
+ private def createAllChildren (node : Node , rowCount : Int = 0 , childrenCount : Int = 0 ): Unit = {
16
17
if (rowCount <= 1 ) return
17
18
18
19
0 until childrenCount foreach { i =>
19
- node.children += Node (node.id * 10 + i + 1 )
20
+ node.children += Node (node.id + " - " + i )
20
21
createAllChildren(node.children(i), rowCount - 1 , childrenCount)
21
22
}
22
23
}
23
24
24
25
private def doSomethingWithNode (node : Node ) = Console .println(node.id)
25
26
26
- def dfsRecursive () : Unit = {
27
+ def dfsRecursive : Unit = {
27
28
def dfsRecursive (node : Node ): Unit = {
28
29
doSomethingWithNode(node)
29
30
node.children.foreach(dfsRecursive)
@@ -32,7 +33,7 @@ object TreeTraversal {
32
33
dfsRecursive(root)
33
34
}
34
35
35
- def dfsRecursivePostOrder () : Unit = {
36
+ def dfsRecursivePostOrder : Unit = {
36
37
def dfsRecursivePostOrder (node : Node ): Unit = {
37
38
node.children.foreach(dfsRecursivePostOrder)
38
39
doSomethingWithNode(node)
@@ -41,7 +42,7 @@ object TreeTraversal {
41
42
dfsRecursivePostOrder(root)
42
43
}
43
44
44
- def dfsRecursiveInOrderBinary () : Unit = {
45
+ def dfsRecursiveInOrderBinary : Unit = {
45
46
def processIfChildExists (children : ListBuffer [Node ], index : Int ) =
46
47
if (children.isDefinedAt(index))
47
48
dfsRecursiveInOrderBinary(children(index))
@@ -58,50 +59,48 @@ object TreeTraversal {
58
59
dfsRecursiveInOrderBinary(this .root)
59
60
}
60
61
61
- def dfsStack () : Unit = {
62
+ def dfsStack : Unit = {
62
63
val stack = new ArrayBuffer [Node ]()
63
64
stack += root
64
65
while (stack.nonEmpty) {
65
66
doSomethingWithNode(stack(0 ))
66
- stack ++= stack.remove(0 ).children
67
+ val firstNode = stack.remove(0 )
68
+ stack ++= firstNode.children
67
69
}
68
70
}
69
71
70
- def bfsQueue () : Unit = {
72
+ def bfsQueue : Unit = {
71
73
val queue = new Queue [Node ]()
72
74
queue.enqueue(root)
73
75
while (queue.nonEmpty) {
74
76
doSomethingWithNode(queue.head)
75
- queue ++= queue.dequeue.children
77
+ val firstNode = queue.dequeue()
78
+ queue ++= firstNode.children
76
79
}
77
80
}
78
81
79
82
}
80
83
81
84
def main (args : Array [String ]): Unit = {
82
85
Console .println(" Creating Tree" )
83
- var tree = new Tree (3 , 3 )
86
+ var theTree = new Tree (3 , 3 )
84
87
85
88
Console .println(" Using recursive DFS :" )
86
- tree .dfsRecursive
89
+ theTree .dfsRecursive
87
90
88
91
Console .println(" Using stack-based DFS :" )
89
- tree .dfsStack
92
+ theTree .dfsStack
90
93
91
94
Console .println(" Using queue-based BFS :" )
92
- tree .bfsQueue
95
+ theTree .bfsQueue
93
96
94
97
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
105
104
}
106
105
107
- }
106
+ }
0 commit comments