-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathtree-traversal.cr
116 lines (95 loc) · 2.2 KB
/
tree-traversal.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class Node
property id, children
def initialize(@id : Int32, @children : Array(Node))
end
end
def dfs_recursive(node)
print node.id
node.children.each{ |child| dfs_recursive child }
end
def dfs_recursive_postorder(node)
node.children.each{ |child| dfs_recursive_postorder child }
print node.id
end
def dfs_recursive_inorder_btree(node)
case node.children.size
when 2
dfs_recursive_inorder_btree node.children[0]
print node.id
dfs_recursive_inorder_btree node.children[1]
when 1
dfs_recursive_inorder_btree node.children[0]
print node.id
when 0
print node.id
else
print "Not a binary tree!"
end
end
def dfs_stack(node)
stack = [node]
until stack.empty?
temp = stack.pop
print temp.id
temp.children.each{ |child| stack.push child }
end
end
def bfs_queue(node)
queue = Deque.new [node]
until queue.empty?
temp = queue.shift
print temp.id
temp.children.each{ |child| queue.push child }
end
end
def create_tree(levels, num_childs)
children = [] of Node
unless levels == 0
num_childs.times{children.push create_tree levels-1, num_childs }
end
Node.new(levels, children)
end
def print_tree(node, depth = [] of String)
puts "(#{node.id})"
depth.push " "
len = node.children.size - 1
(0 .. len).each do |i|
depth.each{|c| print c}
unless i == len
print "├"
depth.push "│"
print_tree node.children[i], depth
depth.pop
else
print "└"
depth.push " "
print_tree node.children[i], depth
depth.pop
end
end
depth.pop
end
def main
puts "Creating Tree"
root = create_tree levels: 2, num_childs: 3
print_tree root
puts "Using recursive DFS:"
dfs_recursive root
puts
puts "Using recursive DFS with post-order traversal:"
dfs_recursive_postorder root
puts
puts "Using stack-based DFS:"
dfs_stack root
puts
puts "Using queue-based BFS:"
bfs_queue root
puts
puts "Creating binary tree to test in-order traversal"
root_bin = create_tree levels: 3, num_childs: 2
print_tree root_bin
puts "Using In-order DFS:"
dfs_recursive_inorder_btree root_bin
puts
end
main