Class: KTree::KTree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/k-tree/k-tree.rb

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vupper, vlower, depth, &block) ⇒ KTree

Returns a new instance of KTree.

Raises:



69
70
71
72
73
74
75
# File 'lib/k-tree/k-tree.rb', line 69

def initialize(vupper , vlower, depth, &block)
  @tuple = vupper.size
  @depth = depth
  @root = nil
  raise KTreeException.new('tuple mismatch') unless vlower.size == vupper.size
  create_tree(vupper, vlower, &block)
end

Instance Attribute Details

#depthObject (readonly)

Returns the value of attribute depth.



19
20
21
# File 'lib/k-tree/k-tree.rb', line 19

def depth
  @depth
end

#rootObject

Returns the value of attribute root.



20
21
22
# File 'lib/k-tree/k-tree.rb', line 20

def root
  @root
end

#tupleObject (readonly)

Returns the value of attribute tuple.



19
20
21
# File 'lib/k-tree/k-tree.rb', line 19

def tuple
  @tuple
end

Instance Method Details

#create_tree(vupper, vlower, &block) ⇒ Object

The block is passed node itself as parent and the vector midpoints of its children to be.

The block sets is own refob, and returns either true, in which case all the children are created, or it returns false, in which case none of the children will be created, in effect, marking itself as a leaf node.



84
85
86
87
# File 'lib/k-tree/k-tree.rb', line 84

def create_tree(vupper, vlower, &block)
  @root = Node.new(vupper, vlower, @depth)
  @root.create_children &block
end

#each(&block) ⇒ Object

Iterate through all nodes



90
91
92
# File 'lib/k-tree/k-tree.rb', line 90

def each(&block)
  root.each(&block)
end