red-black-tree 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +0 -1
- data/lib/red-black-tree.rb +21 -15
- data/lib/red_black_tree/node.rb +7 -4
- data/lib/red_black_tree/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 291198bed7dd82b504c47b1c6fa047674c9302a7c529d33409d61797a0677929
|
4
|
+
data.tar.gz: ab8b8d513625de270d0126c0c3f1d5d787260b32f1bd0f13927332e9e2640aab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aed7c4bd38468df481ebf815605f8564bc66ecfb8b7a53ee5babefaad2f68fc0a08994a9e91922f161f628a23d63eff02e57c7418bd7ce80b6db4f95723aa421
|
7
|
+
data.tar.gz: 2ec1c08f09e3ef1ca7bf608032cfb4a28042e57ec357dd40ee1c8951c64e0c7d944f7900db4bf9acc758513070f5533745e4b6a3fa4477567644de73cc62787c
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
data/lib/red-black-tree.rb
CHANGED
@@ -20,6 +20,7 @@ class RedBlackTree
|
|
20
20
|
|
21
21
|
def initialize
|
22
22
|
@size = 0
|
23
|
+
@root = nil
|
23
24
|
@left_most_node = nil
|
24
25
|
end
|
25
26
|
|
@@ -90,6 +91,7 @@ class RedBlackTree
|
|
90
91
|
raise ArgumentError, "Target parent already has #{direction} child" if (child = target_parent[direction]) && child.valid?
|
91
92
|
end
|
92
93
|
|
94
|
+
node.tree = self
|
93
95
|
node.parent = nil
|
94
96
|
node.left = LeafNode.new
|
95
97
|
node.left.parent = node
|
@@ -138,10 +140,16 @@ class RedBlackTree
|
|
138
140
|
# Removes the given node from the tree.
|
139
141
|
#
|
140
142
|
# @param node [RedBlackTree::Node] the node to be removed
|
141
|
-
# @return [RedBlackTree]
|
143
|
+
# @return [RedBlackTree::Node, nil] the removed node
|
142
144
|
def delete! node
|
143
145
|
raise ArgumentError, "cannot delete leaf node" if node.instance_of? LeafNode
|
144
146
|
|
147
|
+
if node.tree.nil?
|
148
|
+
return
|
149
|
+
elsif node.tree != self
|
150
|
+
raise ArgumentError, "node does not belong to this tree"
|
151
|
+
end
|
152
|
+
|
145
153
|
original_node = node
|
146
154
|
|
147
155
|
if node.children_are_valid?
|
@@ -154,13 +162,6 @@ class RedBlackTree
|
|
154
162
|
node.swap_position_with! LeafNode.new
|
155
163
|
|
156
164
|
@root = successor if is_root
|
157
|
-
|
158
|
-
original_node.validate_free!
|
159
|
-
|
160
|
-
decrement_size!
|
161
|
-
update_left_most_node!
|
162
|
-
|
163
|
-
return self
|
164
165
|
elsif node.single_child_is_valid?
|
165
166
|
is_root = is_root? node
|
166
167
|
|
@@ -170,13 +171,6 @@ class RedBlackTree
|
|
170
171
|
node.swap_position_with! LeafNode.new
|
171
172
|
|
172
173
|
@root = valid_child if is_root
|
173
|
-
|
174
|
-
original_node.validate_free!
|
175
|
-
|
176
|
-
decrement_size!
|
177
|
-
update_left_most_node!
|
178
|
-
|
179
|
-
return self
|
180
174
|
elsif node.children_are_leaves?
|
181
175
|
if is_root? node
|
182
176
|
@root = nil
|
@@ -226,11 +220,23 @@ class RedBlackTree
|
|
226
220
|
end
|
227
221
|
end
|
228
222
|
|
223
|
+
original_node.tree = nil
|
229
224
|
original_node.validate_free!
|
230
225
|
|
231
226
|
decrement_size!
|
232
227
|
update_left_most_node!
|
233
228
|
|
229
|
+
original_node
|
230
|
+
end
|
231
|
+
|
232
|
+
# Removes all nodes from the tree.
|
233
|
+
#
|
234
|
+
# @return [RedBlackTree] self
|
235
|
+
def clear!
|
236
|
+
@root = nil
|
237
|
+
@size = 0
|
238
|
+
@left_most_node = nil
|
239
|
+
|
234
240
|
self
|
235
241
|
end
|
236
242
|
|
data/lib/red_black_tree/node.rb
CHANGED
@@ -19,6 +19,9 @@ class RedBlackTree
|
|
19
19
|
# @return [any] the data/value representing the node
|
20
20
|
attr_reader :data
|
21
21
|
|
22
|
+
# @return [RedBlackTree::Node, nil] the tree this node belongs to
|
23
|
+
attr_reader :tree
|
24
|
+
|
22
25
|
# @param data [any] a non-nil data/value representing the node
|
23
26
|
# @return [Node] a new instance of Node
|
24
27
|
def initialize data
|
@@ -51,10 +54,8 @@ class RedBlackTree
|
|
51
54
|
RIGHT = "right"
|
52
55
|
DIRECTIONS = [LEFT, RIGHT].freeze
|
53
56
|
|
54
|
-
attr_writer :data
|
55
|
-
attr_accessor :colour
|
56
|
-
attr_accessor :tree, :parent
|
57
|
-
attr_accessor :left, :right
|
57
|
+
attr_writer :data, :tree
|
58
|
+
attr_accessor :colour, :parent, :left, :right
|
58
59
|
include LeftRightElementReferencers
|
59
60
|
|
60
61
|
def red? = @colour == RED
|
@@ -116,6 +117,8 @@ class RedBlackTree
|
|
116
117
|
end
|
117
118
|
|
118
119
|
def validate_free!
|
120
|
+
raise StructuralError, "Node is still chained to a tree" if @tree
|
121
|
+
|
119
122
|
anchors = []
|
120
123
|
anchors << "parent" if @parent
|
121
124
|
anchors << "left child" if @left && @left.valid?
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: red-black-tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Young
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-01-
|
10
|
+
date: 2025-01-05 00:00:00.000000000 Z
|
11
11
|
dependencies: []
|
12
12
|
email:
|
13
13
|
- djry1999@gmail.com
|