Class: Db::Core::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/root/tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#datasObject

Returns the value of attribute datas.



5
6
7
# File 'lib/root/tree.rb', line 5

def datas
  @datas
end

#next_nodeObject

Returns the value of attribute next_node.



5
6
7
# File 'lib/root/tree.rb', line 5

def next_node
  @next_node
end

#parent_nodeObject

Returns the value of attribute parent_node.



5
6
7
# File 'lib/root/tree.rb', line 5

def parent_node
  @parent_node
end

#pre_nodeObject

Returns the value of attribute pre_node.



5
6
7
# File 'lib/root/tree.rb', line 5

def pre_node
  @pre_node
end

#structureObject

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

#keysObject



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_valueObject



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