binary_search_tree 2.0 → 2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +32 -0
- data/README +76 -0
- data/Rakefile +1 -0
- data/binary_search_tree.gemspec +20 -0
- data/lib/binary_search_tree.rb +4 -522
- data/lib/binary_search_tree/binary_node.rb +42 -0
- data/lib/binary_search_tree/binary_search_tree.rb +493 -0
- data/lib/{binary_search_tree_hash.rb → binary_search_tree/binary_search_tree_hash.rb} +7 -7
- data/lib/binary_search_tree/version.rb +3 -0
- data/spec/binary_node_spec.rb +84 -0
- data/spec/binary_search_tree_spec.rb +107 -0
- metadata +31 -5
@@ -16,7 +16,7 @@ class BinarySearchTreeHash
|
|
16
16
|
|
17
17
|
def [] key
|
18
18
|
node = @bst.find key
|
19
|
-
node.
|
19
|
+
node.nil?? nil : node.value
|
20
20
|
end
|
21
21
|
|
22
22
|
def []=(key, value)
|
@@ -40,15 +40,15 @@ class BinarySearchTreeHash
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def values
|
43
|
-
map{ |node| node.
|
43
|
+
map{ |node| node.last }
|
44
44
|
end
|
45
45
|
|
46
46
|
def to_a
|
47
|
-
map{ |node| [node.first, node.
|
47
|
+
map{ |node| [node.first, node.last] }
|
48
48
|
end
|
49
49
|
|
50
50
|
def to_s
|
51
|
-
'{' + map{ |node| "#{node.first} => #{node.
|
51
|
+
'{' + map{ |node| "#{node.first} => #{node.last}" }.join(', ') + '}'
|
52
52
|
end
|
53
53
|
|
54
54
|
def min
|
@@ -113,7 +113,7 @@ class BinarySearchTreeHash
|
|
113
113
|
alias :length :size
|
114
114
|
|
115
115
|
def has_key? key
|
116
|
-
|
116
|
+
!!@bst.find(key)
|
117
117
|
end
|
118
118
|
|
119
119
|
def include? key
|
@@ -129,7 +129,7 @@ class BinarySearchTreeHash
|
|
129
129
|
end
|
130
130
|
|
131
131
|
def has_value? value
|
132
|
-
|
132
|
+
!!@bst.find_value(value)
|
133
133
|
end
|
134
134
|
|
135
135
|
def value? value
|
@@ -147,7 +147,7 @@ class BinarySearchTreeHash
|
|
147
147
|
|
148
148
|
def shift
|
149
149
|
deleted_node = @bst.remove_min
|
150
|
-
deleted_node.
|
150
|
+
deleted_node.nil?? nil : [deleted_node.key, deleted_node.value]
|
151
151
|
end
|
152
152
|
|
153
153
|
def invert
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'binary_search_tree'
|
3
|
+
|
4
|
+
describe BinaryNode do
|
5
|
+
subject(:node) { BinaryNode.new(5, "five", nil) }
|
6
|
+
# its(:key) { should be (5) }
|
7
|
+
# its(:value) { should eql ("five") }
|
8
|
+
|
9
|
+
describe "#is_leaf?" do
|
10
|
+
it "returns true when height is 0" do
|
11
|
+
expect(node.is_leaf?).to be true
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns false when height is not 0" do
|
15
|
+
node.height = 1
|
16
|
+
expect(node.is_leaf?).to be false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#max_children_height" do
|
21
|
+
|
22
|
+
before(:each) do
|
23
|
+
@left = double('left_child', :present? => true, :height => 3)
|
24
|
+
@right = double('right_child', :present? => true, :height => 2)
|
25
|
+
node.left, node.right = @left, @right
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when both children are present and the left has a greater height" do
|
29
|
+
it "returns the value of the left child's height" do
|
30
|
+
expect(node.max_children_height).to eql(3)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "when both children are present and right has a greater height" do
|
35
|
+
it "returns the value of the right child's height" do
|
36
|
+
@right.stub(:height).and_return(4)
|
37
|
+
expect(node.max_children_height).to eql(4)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when only the left child is present" do
|
42
|
+
it "returns the value of the left child's height" do
|
43
|
+
node.right = nil
|
44
|
+
|
45
|
+
expect(node.max_children_height).to eql(3)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "when only the right child is present" do
|
50
|
+
it "returns the value of the right child's height" do
|
51
|
+
node.left = nil
|
52
|
+
expect(node.max_children_height).to eql(2)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#balance_factor" do
|
58
|
+
before(:each) do
|
59
|
+
@left = double('left_child', :present? => true, :height => 3)
|
60
|
+
@right = double('right_child', :present? => true, :height => 2)
|
61
|
+
node.left, node.right = @left, @right
|
62
|
+
end
|
63
|
+
|
64
|
+
it "returns the correct height when the left is greater" do
|
65
|
+
expect(node.balance_factor).to eql(1)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "returns the correct height when the right is greater" do
|
69
|
+
@right.stub(:height).and_return(5)
|
70
|
+
expect(node.balance_factor).to eql(-2)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "returns the correct height when both are zero" do
|
74
|
+
@right.stub(:height).and_return(0)
|
75
|
+
@left.stub(:height).and_return(0)
|
76
|
+
expect(node.balance_factor).to eql(0)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
end
|
84
|
+
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'binary_search_tree'
|
3
|
+
|
4
|
+
describe BinarySearchTree do
|
5
|
+
subject(:tree) { BinarySearchTree.new() }
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
tree.insert(9, "nine")
|
9
|
+
tree.insert(5, "five")
|
10
|
+
tree.insert(10, "ten")
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#insert" do
|
14
|
+
it "remains balanced after insertion requiring an lrc rebalance" do
|
15
|
+
tree.insert(1, "one")
|
16
|
+
tree.insert(3, "three")
|
17
|
+
expect(tree.root.max_children_height).to eql(1)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "remains balanced after insertion requiring an llc rebalance" do
|
21
|
+
tree.insert(1, "one")
|
22
|
+
tree.insert(0.5, "half")
|
23
|
+
expect(tree.root.max_children_height).to eql(1)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "remains balanced after insertion requiring an rrc rebalance" do
|
27
|
+
tree.insert(11, "eleven")
|
28
|
+
tree.insert(12, "twelve")
|
29
|
+
expect(tree.root.max_children_height).to eql(1)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "remains balanced after insertion requiring an rlc rebalance" do
|
33
|
+
tree.insert(15, "X")
|
34
|
+
tree.insert(14, "X")
|
35
|
+
expect(tree.root.max_children_height).to eql(1)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#find" do
|
40
|
+
it "finds a value when a value is present" do
|
41
|
+
expect(tree.find(9).value).to eql("nine")
|
42
|
+
end
|
43
|
+
|
44
|
+
it "does not find a value that is not present" do
|
45
|
+
expect(tree.find(18)).to eql(nil)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#min" do
|
50
|
+
it "finds the minimum value" do
|
51
|
+
expect(tree.min.key).to eql(5)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "finds the minimum value after insert" do
|
55
|
+
tree.insert(0.5, "half")
|
56
|
+
expect(tree.min.key).to eql(0.5)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#max" do
|
61
|
+
it "returns the maximum value" do
|
62
|
+
expect(tree.max.key).to eql(10)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#remove_min" do
|
67
|
+
it "updates the minimum value after removing" do
|
68
|
+
expect(tree.min.key).to eql(5)
|
69
|
+
tree.remove_min
|
70
|
+
expect(tree.min.key).to eql(9)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#remove" do
|
75
|
+
it "updates the number of nodes in the tree" do
|
76
|
+
tree.remove(5)
|
77
|
+
expect(tree.min.key).to eql(9)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "doesn't corrupt tree" do
|
81
|
+
puts "about to remove 10"
|
82
|
+
tree.remove 10
|
83
|
+
expect(tree.balanced?).to eql(true)
|
84
|
+
expect(tree.nodes.size).to eql(tree.size)
|
85
|
+
|
86
|
+
puts "about to remove 9"
|
87
|
+
tree.remove 9
|
88
|
+
expect(tree.balanced?).to eql(true)
|
89
|
+
expect(tree.nodes.size).to eql(tree.size)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "#nodes" do
|
94
|
+
it "returns an array of nodes" do
|
95
|
+
expect(tree.nodes).to be_an(Array)
|
96
|
+
expect(tree.nodes[0]).to be_a(BinaryNode)
|
97
|
+
end
|
98
|
+
|
99
|
+
it "contains all the nodes that have been inserted" do
|
100
|
+
tree.nodes.count.should eql(3)
|
101
|
+
tree.insert(11, "eleven")
|
102
|
+
tree.nodes.count.should eql(4)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
metadata
CHANGED
@@ -1,23 +1,48 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: binary_search_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '2.
|
4
|
+
version: '2.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Misha Conway
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
11
|
+
date: 2016-01-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
description:
|
14
28
|
email: MishaAConway@gmail.com
|
15
29
|
executables: []
|
16
30
|
extensions: []
|
17
31
|
extra_rdoc_files: []
|
18
32
|
files:
|
33
|
+
- ".gitignore"
|
34
|
+
- Gemfile
|
35
|
+
- Gemfile.lock
|
36
|
+
- README
|
37
|
+
- Rakefile
|
38
|
+
- binary_search_tree.gemspec
|
19
39
|
- lib/binary_search_tree.rb
|
20
|
-
- lib/
|
40
|
+
- lib/binary_search_tree/binary_node.rb
|
41
|
+
- lib/binary_search_tree/binary_search_tree.rb
|
42
|
+
- lib/binary_search_tree/binary_search_tree_hash.rb
|
43
|
+
- lib/binary_search_tree/version.rb
|
44
|
+
- spec/binary_node_spec.rb
|
45
|
+
- spec/binary_search_tree_spec.rb
|
21
46
|
homepage:
|
22
47
|
licenses:
|
23
48
|
- MIT
|
@@ -38,9 +63,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
63
|
version: '0'
|
39
64
|
requirements: []
|
40
65
|
rubyforge_project: nowarning
|
41
|
-
rubygems_version: 2.
|
66
|
+
rubygems_version: 2.4.5
|
42
67
|
signing_key:
|
43
68
|
specification_version: 4
|
44
69
|
summary: A self balancing avl binary search tree class. Also includes BinarySearchTreeHash
|
45
70
|
which is a hash like class that internally uses binary search tree.
|
46
71
|
test_files: []
|
72
|
+
has_rdoc:
|