forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtree.rb
More file actions
152 lines (131 loc) · 3.14 KB
/
tree.rb
File metadata and controls
152 lines (131 loc) · 3.14 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class TreeNode
attr_reader :key, :value
attr_accessor :left, :right
def initialize(key, val)
@key = key
@value = val
@left = nil
@right = nil
end
end
class Tree
attr_reader :root
def initialize
@root = nil
end
# Time Complexity: O(log n) for balanced trees, O(n) for unbalanced trees.
# Space Complexity: O(1)
def add(key, value)
new_node = TreeNode.new(key, value)
if @root == nil
@root = new_node
else
current = @root
add_helper(current, new_node)
end
end
def add_helper(current, new_node)
if current == nil
return current = new_node
elsif new_node.key < current.key
current.left = add_helper(current.left, new_node)
else
current.right = add_helper(current.right, new_node)
end
return current
end
# Time Complexity: O(log n) for balanced trees, O(n) for unbalanced trees.
# Space Complexity: O(1)
def find(key)
return nil if @root == nil
current = @root
find_helper(current, key)
end
def find_helper(current, key)
if current.key == key
return current.value
elsif key < current.key
current.left = find_helper(current.left, key)
else
current.right = find_helper(current.right, key)
end
end
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
array = []
return array if @root == nil
current = @root
return inorder_helper(current, array)
end
def inorder_helper(current, array)
if current == nil
return array
else
inorder_helper(current.left, array)
array.push({:key => current.key, :value => current.value})
inorder_helper(current.right, array)
end
end
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
array = []
return array if @root == nil
current = @root
return preorder_helper(current, array)
end
def preorder_helper(current, array)
if current == nil
return array
else
array.push({:key => current.key, :value => current.value})
preorder_helper(current.left, array)
preorder_helper(current.right, array)
end
end
# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
array = []
return array if @root == nil
current = @root
return postorder_helper(current, array)
end
def postorder_helper(current, array)
if current == nil
return array
else
postorder_helper(current.left, array)
postorder_helper(current.right, array)
array.push({:key => current.key, :value => current.value})
end
end
# Time Complexity: O(n)
# Space Complexity: O(n)
def height
return 0 if @root == nil
current = @root
height_helper(current)
end
def height_helper(current)
return 0 if current == nil
left_side = height_helper(current.left)
right_side = height_helper(current.right)
if left_side < right_side
return (right_side + 1)
else
return (left_side + 1)
end
end
# Optional Method
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
end
# Useful for printing
def to_s
return "#{self.inorder}"
end
end