-
Notifications
You must be signed in to change notification settings - Fork 1
/
preorder_transversal.rb
79 lines (64 loc) · 1.68 KB
/
preorder_transversal.rb
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
require 'pry'
class Stack
attr_reader :elements
def initialize
@elements = []
end
def pop
@elements.shift
end
def push(element)
@elements.unshift(element)
end
def size
@elements.size
end
end
class Node
attr_accessor :value, :left, :right, :name
def initialize(options={})
@value = options[:value]
@name = options[:name]
@left = options[:left] || nil
@right = options[:right] || nil
end
def children?
@left && @right
end
def no_children?
!children?
end
end
def preorder_transversal(tree)
return if tree == nil
puts tree.value
preorder_transversal(tree.left)
preorder_transversal(tree.right)
end
def preorder_transversal_non_recursion(tree)
stack = Stack.new
stack.push(tree)
while stack.size > 0
node = stack.pop
puts node.value
stack.push node.right if node.right
stack.push node.left if node.left
end
end
root = Node.new({:value => 1, :name => 'root'})
child_1 = Node.new({:value => 2, :name => 'child_1'})
child_2 = Node.new({:value => 3, :name => 'child_2'})
grand_child_1 = Node.new({:value => 4, :name => 'grand_child_1'})
grand_child_2 = Node.new({:value => 7, :name => 'grand_child_2'})
grand_child_3 = Node.new({:value => 8, :name => 'grand_child_3'})
grand_grand_child_1 = Node.new({:value => 5, :name => 'grand_grand_child_1'})
grand_grand_child_2 = Node.new({:value => 6, :name => 'grand_grand_child_2'})
grand_child_1.right = grand_grand_child_2
grand_child_1.left = grand_grand_child_1
child_1.left = grand_child_1
child_1.right = grand_child_2
child_2.right = grand_child_3
root.left = child_1
root.right = child_2
preorder_transversal(root)
preorder_transversal_non_recursion(root)