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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a02458c007e726e16b423af5bec30aabd799260
4
- data.tar.gz: e0e82835afd930aff2858a6083b2357c084d300e
3
+ metadata.gz: ba7ed4b2854e4898a5f144338fcd57bdd6241ecb
4
+ data.tar.gz: 248d30f972253910cbf89b0b5975e8183684c60f
5
5
  SHA512:
6
- metadata.gz: 3fd204050d57a8f9918d2487bec619d82043352f0052f71bc75932db3e869ef2e959b02ec094b8252c14986a69c321b23403f5c99634f0f0ac0f012fb3388f4c
7
- data.tar.gz: d45ac33cc6aa5850cfe309873000c36b81b66f0f6ef94860988e9e8e4b6e650bd5331c8f6593b10fbabbf9ab107b3eddb6fd788c7866873a49b112c338a2ac5e
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)
@@ -1,6 +1,6 @@
1
1
  require_relative 'bs_tree'
2
2
  module AVLTree
3
- class AVLTree < BSTree::BSTree
3
+ class AVLTree < BSTree
4
4
 
5
5
  # Public interfaces implemented in superclass
6
6
 
@@ -1,4 +1,4 @@
1
- module BSTree
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::Node.new(item)
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
@@ -1,4 +1,4 @@
1
- module BSTreeTraversal
1
+ module AVLTree
2
2
  class BSTreeTraversal
3
3
 
4
4
  def pre_order_array(root_node, attribute = nil)
@@ -1,4 +1,4 @@
1
- module Node
1
+ module AVLTree
2
2
  class Node
3
3
  attr_accessor :data, :left, :right
4
4
 
@@ -1,3 +1,3 @@
1
- module AVL
2
- VERSION = '0.0'
1
+ module AVLTree
2
+ VERSION = '0.3'
3
3
  end
@@ -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 = AVL::VERSION
6
- s.date = '2016-04-30'
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 = 'http://rubygems.org/gems/ruby-avl'
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")
@@ -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) { Node::Node.new(25) }
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 }
@@ -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 BSTree::BSTree do
6
- Given(:node) { Node::Node.new(1) }
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) { BSTree::BSTree.new(node) }
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 BSTreeTraversal::BSTreeTraversal do
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) {
@@ -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 Node::Node do
5
+ describe AVLTree::Node do
6
6
  context 'should initialize a new node' do
7
- Given(:node) { Node::Node.new('test string') }
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.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-04-30 00:00:00.000000000 Z
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: http://rubygems.org/gems/ruby-avl
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.4.8
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