forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtree.rb
156 lines (139 loc) · 3.3 KB
/
tree.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
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
153
154
155
156
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)
# Space Complexity: [O(1)-if it's a loop] - why do I wanna say O(n)?? help me.
# Try with a while loop
def add(key, value = nil)
if @root.nil?
@root = TreeNode.new(key, value)
else
add_helper(@root, key, value)
end
end
def add_helper(current, key, value)
# if current = return TreeNode.new
if key < current.key
# if current.left is nil place the new node
# otherwise make current.left the new current and run through the same checks
if current.left.nil?
current.left = TreeNode.new(key, value)
return
else
add_helper(current.left, key, value)
end
else
if current.right.nil?
current.right = TreeNode.new(key, value)
return
else
add_helper(current.right, key, value)
end
end
end
# Time Complexity: O(log n)
# Space Complexity: O(1)
def find(key)
return find_helper(@root, key)
end
def find_helper(current, key)
if current.nil?
return nil
end
if current.key == key
return current.value
end
if key > current.key
return find_helper(current.right, key)
else
return find_helper(current.left, key)
end
end
# Time Complexity: O(n)
# Space Complexity: O(n)
# left-root-right
def inorder
# current = @root
inorder_helper(@root, [])
end
def inorder_helper(current, values) # Values is an array
if current.nil?
return values
end
inorder_helper(current.left, values)
values.push({:key=>current.key, :value=>current.value})
inorder_helper(current.right, values)
return values
end
# Time Complexity: O(n)
# Space Complexity: O(n)
# root-left-right
def preorder
preorder_helper(@root, [])
end
def preorder_helper(current, values)
if current.nil?
return values
end
values.push({:key=>current.key, :value=>current.value})
preorder_helper(current.left, values)
preorder_helper(current.right, values)
return values
end
# Time Complexity: O(n)
# Space Complexity: O(n)
# left-right-root
def postorder
postorder_helper(@root, [])
end
def postorder_helper(current, values)
if current.nil?
return values
end
postorder_helper(current.left, values)
postorder_helper(current.right, values)
values.push({:key=>current.key, :value=>current.value})
return values
end
# Time Complexity: O(n)^2
# Space Complexity: O(n)
def height
# raise NotImplementedError
height_helper(@root)
end
def height_helper(current)
if current.nil?
return 0
end
left_height = height_helper(current.left)
right_height = height_helper(current.right)
if left_height > right_height
return left_height + 1
else
return right_height + 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