-
Notifications
You must be signed in to change notification settings - Fork 1
/
transverse_tree_iteratively.rb
58 lines (46 loc) · 1.11 KB
/
transverse_tree_iteratively.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
require 'pry'
def in_order_transverse(node)
stack = []
current = node
while stack.any? || !current.nil?
while current != nil
stack.push(current)
current = current.left
end
current = stack.pop
puts current.value
current = current.right
end
end
Node = Struct.new(:left, :right, :value)
node4 = Node.new(nil, nil, 4)
node5 = Node.new(nil, nil, 5)
node2 = Node.new(node4, node5, 2)
node3 = Node.new(nil, nil, 3)
node1 = Node.new(node2, node3, 1)
# in_order_transverse(node1)
def pre_order_transverse(node)
stack = []
stack.push(node)
while stack.any?
element = stack.pop
puts element.value
stack.push(element.right) if element.right
stack.push(element.left) if element.left
end
end
# pre_order_transverse(node1)
# To do
def post_order_transverse(node)
stack_one = []
stack_two = []
stack_one.push(node)
while stack_one.any?
element = stack_one.pop
stack_two.push(element)
stack_one.push(element.left) if element.left
stack_one.push(element.right) if element.right
end
stack_two.map(&:value).reverse
end
pp post_order_transverse(node1)