Class: Btree::Tree

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(degree = 2) ⇒ Tree

Creates a BTree of degree 2 by default. Keys Must support being compared using >, < and == methods.



6
7
8
9
10
# File 'lib/btree/tree.rb', line 6

def initialize(degree = 2)
  @degree = degree
  @root = Btree::Node.new(@degree)
  @size = 0
end

Instance Attribute Details

#degreeObject (readonly)

Returns the value of attribute degree.



2
3
4
# File 'lib/btree/tree.rb', line 2

def degree
  @degree
end

#rootObject (readonly)

Returns the value of attribute root.



2
3
4
# File 'lib/btree/tree.rb', line 2

def root
  @root
end

#sizeObject (readonly)

Returns the value of attribute size.



2
3
4
# File 'lib/btree/tree.rb', line 2

def size
  @size
end

Instance Method Details

#dumpObject

puts internal state



29
30
31
# File 'lib/btree/tree.rb', line 29

def dump
  @root.dump
end

#insert(key, value = nil) ⇒ Object Also known as: []=

Insert a key-value pair into the btree



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/btree/tree.rb', line 13

def insert(key, value = nil)
  node = @root
  if node.full?  
    @root = Btree::Node.new(@degree)
    @root.add_child(node)
    @root.split(@root.children.size - 1)
    #puts "After split, root = #{@root.inspect}"
    # split child(@root, 1)
    node = @root
  end
  node.insert(key, value)
  @size += 1
  return self
end

#value_of(key) ⇒ Object Also known as: []

Get value associated with the specified key



34
35
36
# File 'lib/btree/tree.rb', line 34

def value_of(key)
  @root.value_of(key)
end