-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.rb
201 lines (179 loc) · 4.42 KB
/
BST.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# A node class for the bst
class BSTNode
include Comparable
attr_accessor :left, :right, :item
def initialize item
@item = item
end
def <=> other
return @item <=> other.item
end
end
##
# A typical binary search tree class. Will work using standard comparison operators (<, >, ==)
class BST
attr_accessor :root, :size
##
# The constructor. Set size to 0 and root to null
def initialize
@root = nil
@size = 0
end
##
# Adds the item to the bst
def add item
@root = addHelper item, @root
end
##
# Returns true if the item has been added to the tree, false otherwise. Checks using the == operator
def contains? item
return containsHelper item, @root
end
##
# Removes the value from the tree. If applicable, replaces with the successor
def remove item
@root = removeHelper item, @root
end
##
# Emptys the tree
def clear!
@root = nil
@size = 0
end
##
# Returns a preorder traversal of the tree
def preorder
ret = Array.new
preorderHelper @root, ret
return ret
end
##
# Returns a preorder traversal of the tree
def inorder
ret = Array.new
inorderHelper @root, ret
return ret
end
##
# Returns a postorder traversal of the tree
def postorder
ret = Array.new
postorderHelper @root, ret
return ret
end
private
# Recurses through the bst for adding
def addHelper item, root
if !root
@size = @size + 1
return BSTNode.new item
else
if item < root.item
root.left = addHelper item, root.left
return root
elsif item > root.item
root.right = addHelper item, root.right
return root
else
return root
end
end
end
# Recurses through the bst for contains
def containsHelper item, root
if !root
return false
else
if item < root.item
return containsHelper item, root.left
elsif item > root.item
return containsHelper item, root.right
else
return true
end
end
end
# Recurses through the tree for remove
def removeHelper item, root
if !root
return nil
else
if item < root.item
root.left = removeHelper item, root.left
return root
elsif item > root.item
root.right = removeHelper item, root.right
return root
else
#removing leaf
if !root.left && !root.right
@size -= 1
return nil
#no left child
elsif !root.left
@size -= 1
return root.right
#no right child
elsif !root.right
@size -= 1
return root.left
#
else
predecessor = getLargest root.left
removeHelper predecessor, root
root.item = predecessor
return root
end
end
end
end
#Gets the largest item in the subtree
def getLargest root
if !root.right
return root.item
else
return getLargest root.right
end
end
def preorderHelper root, arr
if root
arr.push root.item
preorderHelper root.left, arr
preorderHelper root.right, arr
end
end
def inorderHelper root, arr
if root
preorderHelper root.left, arr
arr.push root.item
preorderHelper root.right, arr
end
end
def postorderHelper root, arr
if root
preorderHelper root.left, arr
preorderHelper root.right, arr
arr.push root.item
end
end
end
bst = BST.new
bst.add 10
bst.add 15
bst.add 5
bst.add 3
bst.add 7
bst.add 12
bst.add 20
# puts bst.size
# bst.remove 10
# puts bst.size
# puts bst.root.item
# puts bst.preorder
bst.clear!
puts bst.size
puts bst.root
# puts bst.contains 10
# puts bst.contains 5
# puts bst.contains 15
# puts bst.contains 143