@@ -14,20 +14,18 @@ AVLTreeNode_or_null{T} = Union{AVLTreeNode{T}, Nothing}
14
14
15
15
mutable struct AVLTree{T}
16
16
root:: AVLTreeNode_or_null{T}
17
+ count:: Int
17
18
18
- AVLTree {T} () where T = new {T} (nothing )
19
+ AVLTree {T} () where T = new {T} (nothing , 0 )
19
20
end
20
21
21
22
AVLTree () = AVLTree {Any} ()
22
23
23
- function get_height (node:: Union{AVLTreeNode, Nothing} )
24
- if node == nothing
25
- return 0
26
- else
27
- return node. height
28
- end
29
- end
24
+ Base. length (tree:: AVLTree ) = tree. count
25
+
26
+ get_height (node:: Union{AVLTreeNode, Nothing} ) = (node == nothing ) ? 0 : node. height
30
27
28
+ # balance is the difference of height between leftChild and rightChild of a node.
31
29
function get_balance (node:: Union{AVLTreeNode, Nothing} )
32
30
if node == nothing
33
31
return 0
@@ -36,8 +34,13 @@ function get_balance(node::Union{AVLTreeNode, Nothing})
36
34
end
37
35
end
38
36
39
- fix_height (node:: Union{AVLTreeNode, Nothing} ) = 1 + max (get_height (node. leftChild), get_height (node. rightChild))
37
+ fix_height (node:: AVLTreeNode ) = 1 + max (get_height (node. leftChild), get_height (node. rightChild))
38
+
39
+ """
40
+ left_rotate!(node_x::RBTreeNode)
40
41
42
+ Performs a left-rotation on `node_x`, updates height of the nodes, and returns the rotated node.
43
+ """
41
44
function left_rotate (z:: AVLTreeNode )
42
45
y = z. rightChild
43
46
α = y. leftChild
@@ -48,6 +51,11 @@ function left_rotate(z::AVLTreeNode)
48
51
return y
49
52
end
50
53
54
+ """
55
+ right_rotate!(node_x::RBTreeNode)
56
+
57
+ Performs a right-rotation on `node_x`, updates height of the nodes, and returns the rotated node.
58
+ """
51
59
function right_rotate (z:: AVLTreeNode )
52
60
y = z. leftChild
53
61
α = y. rightChild
@@ -58,17 +66,30 @@ function right_rotate(z::AVLTreeNode)
58
66
return y
59
67
end
60
68
61
- function get_minimum_node (node:: Union{AVLTreeNode, Nothing} )
69
+ """
70
+ minimum_node(tree::RBTree, node::RBTreeNode)
71
+
72
+ Returns the RBTreeNode with minimum value in subtree of `node`.
73
+ """
74
+ function minimum_node (node:: Union{AVLTreeNode, Nothing} )
62
75
while node != nothing && node. leftChild != nothing
63
76
node = node. leftChild
64
77
end
65
78
return node
66
79
end
67
80
81
+ """
82
+ search_node(tree, key)
83
+
84
+ Returns the last visited node, while traversing through in binary-search-tree fashion looking for `key`.
85
+ """
86
+ search_node (tree, key)
87
+
68
88
function search_node (tree:: AVLTree{K} , d:: K ) where K
69
89
prev = nothing
70
90
node = tree. root
71
91
while node != nothing && node. data != nothing && node. data != d
92
+
72
93
prev = node
73
94
if d < node. data
74
95
node = node. leftChild
@@ -80,16 +101,30 @@ function search_node(tree::AVLTree{K}, d::K) where K
80
101
return (node == nothing ) ? prev : node
81
102
end
82
103
83
- function search_key (tree:: AVLTree{K} , d:: K ) where K
104
+ """
105
+ haskey(tree, key)
106
+
107
+ Returns true if `key` is present in the `tree`, else returns false.
108
+ """
109
+ haskey (tree, key)
110
+
111
+ function haskey (tree:: AVLTree{K} , d:: K ) where K
84
112
(tree. root == nothing ) && return false
85
113
node = search_node (tree, d)
86
114
return (node. data == d)
87
115
end
88
116
117
+ """
118
+ insert!(tree, key)
119
+
120
+ Inserts `key` in the `tree` if it is not present.
121
+ """
122
+ insert! (tree, key)
123
+
89
124
function Base. insert! (tree:: AVLTree{K} , d:: K ) where K
90
125
91
126
function insert_node (node:: Union{AVLTreeNode, Nothing} , key)
92
- if node == nothing || node . data == nothing
127
+ if node == nothing
93
128
return AVLTreeNode {K} (key)
94
129
elseif key < node. data
95
130
node. leftChild = insert_node (node. leftChild, key)
@@ -103,7 +138,7 @@ function Base.insert!(tree::AVLTree{K}, d::K) where K
103
138
if balance > 1
104
139
if key < node. leftChild. data
105
140
return right_rotate (node)
106
- else
141
+
107
142
node. leftChild = left_rotate (node. leftChild)
108
143
return right_rotate (node)
109
144
end
@@ -113,20 +148,31 @@ function Base.insert!(tree::AVLTree{K}, d::K) where K
113
148
if key > node. rightChild. data
114
149
return left_rotate (node)
115
150
else
116
- node. rightChild = right_rotate (node)
151
+ node. rightChild = right_rotate (node. rightChild )
117
152
return left_rotate (node)
118
153
end
119
154
end
120
155
121
156
return node
122
157
end
123
158
124
- search_key (tree, d) && return tree
159
+ haskey (tree, d) && return tree
125
160
126
161
tree. root = insert_node (tree. root, d)
162
+ tree. count += 1
127
163
return tree
128
164
end
129
165
166
+ """
167
+ push!(tree, key)
168
+
169
+ Inserts `key` in the `tree` if it is not present.
170
+ """
171
+ function Base. push! (tree:: AVLTree{K} , key0) where K
172
+ key = convert (K, key0)
173
+ insert! (tree, key)
174
+ end
175
+
130
176
function Base. delete! (tree:: AVLTree{K} , d:: K ) where K
131
177
132
178
function delete_node (node:: Union{AVLTreeNode, Nothing} , key)
@@ -146,7 +192,7 @@ function Base.delete!(tree::AVLTree{K}, d::K) where K
146
192
node = nothing
147
193
return temp
148
194
else
149
- temp = get_minimum_node (node. rightChild)
195
+ temp = minimum_node (node. rightChild)
150
196
node. data = temp. data
151
197
node. rightChild = delete_node (node. rightChild, temp. data)
152
198
end
@@ -180,8 +226,11 @@ function Base.delete!(tree::AVLTree{K}, d::K) where K
180
226
return node
181
227
end
182
228
183
- ! search_key (tree, d) && throw (KeyError (d))
229
+ # if the key is not in the tree, do nothing and return the tree
230
+ ! haskey (tree, d) && return tree
184
231
232
+ # if the key is present, delete it from the tree
185
233
tree. root = delete_node (tree. root, d)
234
+ tree. count -= 1
186
235
return tree
187
- end
236
+ end
0 commit comments