Class: Binary_tree
- Inherits:
-
Object
- Object
- Binary_tree
- Includes:
- Enumerable
- Defined in:
- lib/models/binary_tree/binary_tree.rb
Defined Under Namespace
Classes: Node
Constant Summary collapse
- RED =
true
- BLACK =
false
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #add(value) ⇒ Object
- #each(&block) ⇒ Object
- #find(key) ⇒ Object
-
#initialize ⇒ Binary_tree
constructor
A new instance of Binary_tree.
Constructor Details
#initialize ⇒ Binary_tree
Returns a new instance of Binary_tree.
23 24 25 |
# File 'lib/models/binary_tree/binary_tree.rb', line 23 def initialize self.root = nil end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
21 22 23 |
# File 'lib/models/binary_tree/binary_tree.rb', line 21 def root @root end |
Instance Method Details
#add(value) ⇒ Object
27 28 29 30 31 |
# File 'lib/models/binary_tree/binary_tree.rb', line 27 def add(value) new_node = Node.new(value) self.root = insert(self.root, new_node) self.balance(new_node) end |
#each(&block) ⇒ Object
33 34 35 36 |
# File 'lib/models/binary_tree/binary_tree.rb', line 33 def each(&block) iterator = Binary_tree_iterator.new(self.root) iterator.each(&block) end |
#find(key) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/models/binary_tree/binary_tree.rb', line 38 def find(key) result = nil self.each do |value| if value.key == key result = value break end end result end |