Class: Db::Core::Tree
- Inherits:
-
Object
- Object
- Db::Core::Tree
- Defined in:
- lib/root/tree.rb
Instance Attribute Summary collapse
-
#datas ⇒ Object
Returns the value of attribute datas.
-
#next_node ⇒ Object
Returns the value of attribute next_node.
-
#parent_node ⇒ Object
Returns the value of attribute parent_node.
-
#pre_node ⇒ Object
Returns the value of attribute pre_node.
-
#structure ⇒ Object
Returns the value of attribute structure.
Instance Method Summary collapse
-
#initialize(structure = 3) ⇒ Tree
constructor
A new instance of Tree.
- #insert(key, value) ⇒ Object
- #keys ⇒ Object
- #max_key_value ⇒ Object
- #search(key, operator = DB::Core::Common::Comparison::EQ) ⇒ Object
Constructor Details
#initialize(structure = 3) ⇒ Tree
Returns a new instance of Tree.
7 8 9 10 11 12 13 14 |
# File 'lib/root/tree.rb', line 7 def initialize(structure=3) @structure = structure @datas = [] @parent_node = nil @pre_node = nil @leaf_node = nil @next_node = nil end |
Instance Attribute Details
#datas ⇒ Object
Returns the value of attribute datas.
5 6 7 |
# File 'lib/root/tree.rb', line 5 def datas @datas end |
#next_node ⇒ Object
Returns the value of attribute next_node.
5 6 7 |
# File 'lib/root/tree.rb', line 5 def next_node @next_node end |
#parent_node ⇒ Object
Returns the value of attribute parent_node.
5 6 7 |
# File 'lib/root/tree.rb', line 5 def parent_node @parent_node end |
#pre_node ⇒ Object
Returns the value of attribute pre_node.
5 6 7 |
# File 'lib/root/tree.rb', line 5 def pre_node @pre_node end |
#structure ⇒ Object
Returns the value of attribute structure.
5 6 7 |
# File 'lib/root/tree.rb', line 5 def structure @structure end |
Instance Method Details
#insert(key, value) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/root/tree.rb', line 26 def insert(key, value) inserting_element = key _datas = [] << inserting_element << value @datas << _datas sort_keys divide if @datas.length > @structure @datas end |
#keys ⇒ Object
16 17 18 19 20 |
# File 'lib/root/tree.rb', line 16 def keys id = [] @datas.each {|k| id << k[0]} id end |
#max_key_value ⇒ Object
22 23 24 |
# File 'lib/root/tree.rb', line 22 def max_key_value @datas[-1][0] end |
#search(key, operator = DB::Core::Common::Comparison::EQ) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/root/tree.rb', line 35 def search(key, operator=DB::Core::Common::Comparison::EQ) match=[] case operator when DB::Core::Common::Comparison::EQ @datas.each do |key_node| if key == key_node[0] match << key_node[1] break; end end return match[0] when DB::Core::Common::Comparison::GT ## Find nodes that are greater than key @datas.each do |key_node| if key_node[0] > key match << key_node[1] end end if max_key_value > key match += next_node.search(key, operator) unless next_node.nil? return match when DB::Core::Common::Comparison::GTE @datas.each do |key_node| if key_node[0] >= key match << key_node[1] end end if max_key_value >= key match += next_node.search(key, operator) unless next_node.nil? return match when DB::Core::Common::Comparison::LT @datas.each do |key_node| if key_node[0] < key match << key_node[1] end end pre_match = [] pre_match = pre_node.search(key, operator) unless pre_node.nil? match = pre_match + match return match when DB::Core::Common::Comparison::LTE @datas.each do |key_node| if key_node[0] <= key match << key_node[1] end end pre_match = [] pre_match = pre_node.search(key, operator) unless pre_node.nil? match = pre_match + match return match end end |