Class: Moon::Tree

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/moon/packages/std/tree.rb

Overview

An Implementation of Tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, value = nil) ⇒ Tree


14
15
16
17
18
# File 'lib/moon/packages/std/tree.rb', line 14

def initialize(parent = nil, value = nil)
  @parent = parent
  @value = value
  @children = []
end

Instance Attribute Details

#parentTree


8
9
10
# File 'lib/moon/packages/std/tree.rb', line 8

def parent
  @parent
end

#valueObject


11
12
13
# File 'lib/moon/packages/std/tree.rb', line 11

def value
  @value
end

Instance Method Details

#add(value) ⇒ Object

Adds a new Tree with its value set to value


37
38
39
# File 'lib/moon/packages/std/tree.rb', line 37

def add(value)
  add_tree Tree.new(self, value)
end

#add_tree(tree) ⇒ Object

Adds a Tree to the list of children


30
31
32
# File 'lib/moon/packages/std/tree.rb', line 30

def add_tree(tree)
  @children.push(tree)
end

#delete(value) ⇒ Void

Deletes a child tree by checking its value.


116
117
118
# File 'lib/moon/packages/std/tree.rb', line 116

def delete(value)
  reject_tree { |tree| tree.value == value }
end

#delete_tree(tree) ⇒ Tree

Deletes a child tree and returns it


108
109
110
# File 'lib/moon/packages/std/tree.rb', line 108

def delete_tree(tree)
  @children.delete(tree)
end

#each {|child| ... } ⇒ Object

Yields each child tree object

Yield Parameters:


23
24
25
# File 'lib/moon/packages/std/tree.rb', line 23

def each(&block)
  @children.each(&block)
end

#find {|tree| ... } ⇒ Object?

Same as find_tree, will return the value of the found tree or nil.

Yield Parameters:


161
162
163
# File 'lib/moon/packages/std/tree.rb', line 161

def find(&block)
  (obj = find_tree(&block)) ? obj.value : nil
end

#find_tree {|tree| ... } ⇒ Tree?

Looks in the tree and its children for anything that evaluates the block to true.

Yield Parameters:


147
148
149
150
151
152
153
154
155
# File 'lib/moon/packages/std/tree.rb', line 147

def find_tree(&block)
  return to_enum(:find_tree) unless block_given?
  return self if yield self
  each do |tree|
    return tree if yield tree
    tree.find_tree(&block)
  end
  nil
end

#get(index) ⇒ Object?

Gets a child's value at index.


75
76
77
# File 'lib/moon/packages/std/tree.rb', line 75

def get(index)
  (t = get_tree(index)) && t.value
end

#get_tree(index) ⇒ Tree?

Gets a child at index


67
68
69
# File 'lib/moon/packages/std/tree.rb', line 67

def get_tree(index)
  @children[index]
end

#insert(index, value) ⇒ Void

Inserts a new Tree into the target tree with its value set to value


93
94
95
# File 'lib/moon/packages/std/tree.rb', line 93

def insert(index, value)
  insert_tree index, Tree.new(self, value)
end

#insert_tree(index, tree) ⇒ Void

Inserts a tree into the target tree.


84
85
86
# File 'lib/moon/packages/std/tree.rb', line 84

def insert_tree(index, tree)
  @children.insert index, tree
end

#reject_tree {|tree| ... } ⇒ Object

Rejects a tree from the children.

Yield Parameters:


100
101
102
# File 'lib/moon/packages/std/tree.rb', line 100

def reject_tree(&block)
  @children.reject!(&block)
end

#remove_tree(index) ⇒ Tree

Removes a tree from the children given an index


124
125
126
# File 'lib/moon/packages/std/tree.rb', line 124

def remove_tree(index)
  @children.delete_at(index)
end

#set(index, value) ⇒ Tree

Sets a child's value at index to value, if the tree doesn't exist, it is added and its value set.


55
56
57
58
59
60
61
# File 'lib/moon/packages/std/tree.rb', line 55

def set(index, value)
  if tree = @children[index]
    tree.tap { |t| t.value = value }
  else
    @children[index] = Tree.new(self, value)
  end
end

#set_tree(index, tree) ⇒ Object

Sets a child at index to the provided tree


45
46
47
# File 'lib/moon/packages/std/tree.rb', line 45

def set_tree(index, tree)
  @children[index] = tree
end

#sizeInteger

The size of the tree, excluding the size of its children.


131
132
133
# File 'lib/moon/packages/std/tree.rb', line 131

def size
  @children.size
end

#total_sizeInteger

The total size of the tree, including its children's total_size


138
139
140
# File 'lib/moon/packages/std/tree.rb', line 138

def total_size
  size + @children.inject(0) { |r, tree| r + tree.total_size }
end