ruby-avl 0.0 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +57 -0
- data/lib/ruby-avl/avl_tree.rb +1 -1
- data/lib/ruby-avl/bs_tree.rb +2 -2
- data/lib/ruby-avl/bs_tree_traversal.rb +1 -1
- data/lib/ruby-avl/node.rb +1 -1
- data/lib/ruby-avl/version.rb +2 -2
- data/ruby-avl.gemspec +3 -3
- data/spec/avl_tree_spec.rb +1 -1
- data/spec/bs_tree_spec.rb +3 -3
- data/spec/bs_tree_traversal_spec.rb +1 -1
- data/spec/node_spec.rb +2 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba7ed4b2854e4898a5f144338fcd57bdd6241ecb
|
4
|
+
data.tar.gz: 248d30f972253910cbf89b0b5975e8183684c60f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d084dd9f52378fc4e9f6c4ca07233bd787b406ad20364fb067d509549ec24cfa64b7e5d619f0bc3b3c84a46640a4188612754501ffda7ba3323c88e67e876d11
|
7
|
+
data.tar.gz: 423590f797b7e02a02d7e9323c8c13181becd1e61b8f82daf84c7d1279456621d2b15b060d322378809c42f63166bf1a44ebd4fab7817b871d73ac3f38fc03b7
|
data/README.md
CHANGED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Ruby AVL
|
2
|
+
|
3
|
+
An implementation of the Adelson-Velsky and Landis self-balancing binary search tree written in Ruby.
|
4
|
+
|
5
|
+
***
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
The AVL Tree allows any object to be placed into a Node. However, comparison of the objects needs to be done on an attribute, otherwise the tree won't be able to judge which sub-tree to insert the Node to. This is done easily by implementing the method #compare_to on your object:
|
10
|
+
|
11
|
+
class Person
|
12
|
+
attr_accessor :name
|
13
|
+
|
14
|
+
def compare_to(other_person)
|
15
|
+
this.name <=> other_person.name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
In the above example, a Person object can be added to the tree and it will be balanced on the lexicographical comparison of their name with another Person object's name attribute. This allows you to have more complex trees.
|
20
|
+
|
21
|
+
If you just want to build a tree from simple data types (Fixnum, String, etc.) that can already be compared then you don't need to implement this method.
|
22
|
+
|
23
|
+
***
|
24
|
+
|
25
|
+
To use the AVL Tree is simple, first initialise it:
|
26
|
+
|
27
|
+
avl_tree = AVLTree::AVLTree.new
|
28
|
+
|
29
|
+
Then add the data:
|
30
|
+
|
31
|
+
avl_tree.insert_item(1)
|
32
|
+
avl_tree.insert_item(2)
|
33
|
+
avl_tree.insert_item(3)
|
34
|
+
|
35
|
+
Remove data if necessary:
|
36
|
+
|
37
|
+
avl_tree.remove_item(3)
|
38
|
+
|
39
|
+
***
|
40
|
+
|
41
|
+
To view the tree's structural information: use one (or all) of the traversals. Pass in the root of the AVL Tree as the first parameter.
|
42
|
+
This will return a string representing the nodes in the tree:
|
43
|
+
|
44
|
+
AVLTree::BSTreeTraversal.new.pre_order_string(avl_tree.root)
|
45
|
+
AVLTree::BSTreeTraversal.new.in_order_string(avl_tree.root)
|
46
|
+
AVLTree::BSTreeTraversal.new.post_order_string(avl_tree.root)
|
47
|
+
|
48
|
+
This will return an Array with each element representing a node in the tree:
|
49
|
+
|
50
|
+
AVLTree::BSTreeTraversal.new.pre_order_array(avl_tree.root)
|
51
|
+
AVLTree::BSTreeTraversal.new.in_order_array(avl_tree.root)
|
52
|
+
AVLTree::BSTreeTraversal.new.post_order_array(avl_tree.root)
|
53
|
+
|
54
|
+
An optional parameter can be passed into this function to print out an attribute of the data in the node, instead of the node itself. Obviously, the attribute needs to exist on the object:
|
55
|
+
|
56
|
+
AVLTree::BSTreeTraversal.new.pre_order_string(avl_tree.root, :name)
|
57
|
+
AVLTree::BSTreeTraversal.new.pre_order_array(avl_tree.root, :name)
|
data/lib/ruby-avl/avl_tree.rb
CHANGED
data/lib/ruby-avl/bs_tree.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module AVLTree
|
2
2
|
class BSTree
|
3
3
|
|
4
4
|
attr_reader :root
|
@@ -40,7 +40,7 @@ module BSTree
|
|
40
40
|
# raised to inform the user of duplication.
|
41
41
|
def add_to_tree(item, node)
|
42
42
|
if node.nil?
|
43
|
-
return Node
|
43
|
+
return Node.new(item)
|
44
44
|
elsif compare(item, node.data) < 0
|
45
45
|
node.left = add_to_tree(item, node.left)
|
46
46
|
elsif compare(item, node.data) > 0
|
data/lib/ruby-avl/node.rb
CHANGED
data/lib/ruby-avl/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = '0.
|
1
|
+
module AVLTree
|
2
|
+
VERSION = '0.3'
|
3
3
|
end
|
data/ruby-avl.gemspec
CHANGED
@@ -2,12 +2,12 @@ require File.expand_path('../lib/ruby-avl/version', __FILE__)
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'ruby-avl'
|
5
|
-
s.version =
|
6
|
-
s.date = '2016-
|
5
|
+
s.version = AVLTree::VERSION
|
6
|
+
s.date = '2016-11-03'
|
7
7
|
s.description = s.summary = 'A simple AVL Tree implemented in Ruby'
|
8
8
|
s.authors = ['Daniel Byers']
|
9
9
|
s.email = 'daniel_byers@hotmail.co.uk'
|
10
|
-
s.homepage = '
|
10
|
+
s.homepage = 'https://github.com/daniel-byers/ruby-avl'
|
11
11
|
s.license = 'MIT'
|
12
12
|
|
13
13
|
s.files = `git ls-files`.split("\n")
|
data/spec/avl_tree_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
|
3
3
|
Given.use_natural_assertions
|
4
4
|
|
5
5
|
describe AVLTree::AVLTree do
|
6
|
-
Given(:node) {
|
6
|
+
Given(:node) { AVLTree::Node.new(25) }
|
7
7
|
|
8
8
|
context 'initialize a AVL Tree with a null root' do
|
9
9
|
Then { subject.root == nil }
|
data/spec/bs_tree_spec.rb
CHANGED
@@ -2,15 +2,15 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
|
2
2
|
|
3
3
|
Given.use_natural_assertions
|
4
4
|
|
5
|
-
describe
|
6
|
-
Given(:node) {
|
5
|
+
describe AVLTree::BSTree do
|
6
|
+
Given(:node) { AVLTree::Node.new(1) }
|
7
7
|
|
8
8
|
context 'initialize a BS Tree with a null root' do
|
9
9
|
Then { subject.root == nil }
|
10
10
|
end
|
11
11
|
|
12
12
|
context 'initialize a BS Tree with a root node' do
|
13
|
-
Given(:subject) {
|
13
|
+
Given(:subject) { AVLTree::BSTree.new(node) }
|
14
14
|
Then { subject.root == node }
|
15
15
|
end
|
16
16
|
|
@@ -2,7 +2,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
|
2
2
|
|
3
3
|
Given.use_natural_assertions
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe AVLTree::BSTreeTraversal do
|
6
6
|
# Given returns the last node of the AVL Tree instead of the tree object itself.
|
7
7
|
# In production, avl_tree.root would need to be passed into the traversal methods.
|
8
8
|
Given(:avl_tree) {
|
data/spec/node_spec.rb
CHANGED
@@ -2,9 +2,9 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
|
2
2
|
|
3
3
|
Given.use_natural_assertions
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe AVLTree::Node do
|
6
6
|
context 'should initialize a new node' do
|
7
|
-
Given(:node) {
|
7
|
+
Given(:node) { AVLTree::Node.new('test string') }
|
8
8
|
Then { node.data == 'test string' }
|
9
9
|
And { node.left == nil}
|
10
10
|
And { node.right == nil}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-avl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Byers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -74,7 +74,7 @@ files:
|
|
74
74
|
- spec/bs_tree_traversal_spec.rb
|
75
75
|
- spec/node_spec.rb
|
76
76
|
- spec/spec_helper.rb
|
77
|
-
homepage:
|
77
|
+
homepage: https://github.com/daniel-byers/ruby-avl
|
78
78
|
licenses:
|
79
79
|
- MIT
|
80
80
|
metadata: {}
|
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
94
|
version: '0'
|
95
95
|
requirements: []
|
96
96
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.
|
97
|
+
rubygems_version: 2.5.1
|
98
98
|
signing_key:
|
99
99
|
specification_version: 4
|
100
100
|
summary: A simple AVL Tree implemented in Ruby
|