Class: Btree::Tree
- Inherits:
-
Object
- Object
- Btree::Tree
- Defined in:
- lib/btree/tree.rb
Instance Attribute Summary collapse
-
#degree ⇒ Object
readonly
Returns the value of attribute degree.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#dump ⇒ Object
puts internal state.
-
#initialize(degree = 2) ⇒ Tree
constructor
Creates a BTree of degree 2 by default.
-
#insert(key, value = nil) ⇒ Object
(also: #[]=)
Insert a key-value pair into the btree.
-
#value_of(key) ⇒ Object
(also: #[])
Get value associated with the specified key.
Constructor Details
Instance Attribute Details
#degree ⇒ Object (readonly)
Returns the value of attribute degree.
2 3 4 |
# File 'lib/btree/tree.rb', line 2 def degree @degree end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
2 3 4 |
# File 'lib/btree/tree.rb', line 2 def root @root end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
2 3 4 |
# File 'lib/btree/tree.rb', line 2 def size @size end |
Instance Method Details
#dump ⇒ Object
puts internal state
31 32 33 |
# File 'lib/btree/tree.rb', line 31 def dump @root.dump end |
#insert(key, value = nil) ⇒ Object Also known as: []=
Insert a key-value pair into the btree
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/btree/tree.rb', line 14 def insert(key, value = nil) node = @root if node.full? @root = Btree::Node.new(@degree) @root.leaf = false @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
36 37 38 |
# File 'lib/btree/tree.rb', line 36 def value_of(key) @root.value_of(key) end |