Class: CherryTree::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/ursa/cherry-tree/tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTree

Returns a new instance of Tree.



5
6
7
8
9
# File 'lib/ursa/cherry-tree/tree.rb', line 5

def initialize
  self.current_deep_level = 0
  self.root = Node.new(nil, nil, current_deep_level)
  self.current_parent = root
end

Instance Attribute Details

#current_deep_levelObject

Returns the value of attribute current_deep_level.



3
4
5
# File 'lib/ursa/cherry-tree/tree.rb', line 3

def current_deep_level
  @current_deep_level
end

#current_parentObject

Returns the value of attribute current_parent.



3
4
5
# File 'lib/ursa/cherry-tree/tree.rb', line 3

def current_parent
  @current_parent
end

#rootObject

Returns the value of attribute root.



3
4
5
# File 'lib/ursa/cherry-tree/tree.rb', line 3

def root
  @root
end

Instance Method Details

#make_node(data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ursa/cherry-tree/tree.rb', line 15

def make_node(data)
  node = Node.new(data, current_parent, current_deep_level)

  if current_parent
    current_parent.children << node
  end

  if block_given?
    old_current_parent = self.current_parent

    self.current_deep_level += 1
    self.current_parent = node

    yield

    self.current_parent = old_current_parent
    self.current_deep_level -= 1
  end

  node
end

#to_sObject



11
12
13
# File 'lib/ursa/cherry-tree/tree.rb', line 11

def to_s
  root.to_s if root
end