Class: Tree::BinaryTreeNode

Description

Provides a Binary tree implementation. This tree node allows only two child nodes (left and right childs). It also provides direct access to the left and right children, including assignment to the same.

Public Instance methods


add (child)

Adds the specified child node to the receiver node. The child node‘s parent is set to be the receiver. The child nodes are added in the order of addition, i.e., the first child added becomes the left node, and the second child will be the second node. If only one child is present, then this will be the left child.

    # File lib/tree/binarytree.rb, line 59
59:     def add(child)
60:       raise "Already has two child nodes" if @children.size == 2
61: 
62:       super(child)
63:     end

isLeftChild? ()

Returns true if this is the left child of its parent. Always returns false if this is the root node.

    # File lib/tree/binarytree.rb, line 92
92:     def isLeftChild?
93:       return nil if isRoot?
94:       self == parent.leftChild
95:     end

isRightChild? ()

Returns true if this is the right child of its parent. Always returns false if this is the root node.

     # File lib/tree/binarytree.rb, line 99
 99:     def isRightChild?
100:       return nil if isRoot?
101:       self == parent.rightChild
102:     end

leftChild ()

Returns the left child node. Note that left Child == first Child

    # File lib/tree/binarytree.rb, line 67
67:     def leftChild
68:       children.first
69:     end

leftChild= (child)

Sets the left child. If a previous child existed, it is replaced.

    # File lib/tree/binarytree.rb, line 79
79:     def leftChild=(child)
80:       @children[0] = child
81:       @childrenHash[child.name] = child if child # Assign the name mapping
82:     end

rightChild ()

Returns the right child node. Note that right child == last child unless there is only one child. Returns nil if the right child does not exist.

    # File lib/tree/binarytree.rb, line 74
74:     def rightChild
75:       children[1]
76:     end

rightChild= (child)

Sets the right child. If a previous child existed, it is replaced.

    # File lib/tree/binarytree.rb, line 85
85:     def rightChild=(child)
86:       @children[1] = child
87:       @childrenHash[child.name] = child if child # Assign the name mapping
88:     end

swap_children ()

Swaps the left and right children with each other

     # File lib/tree/binarytree.rb, line 105
105:     def swap_children
106:       tempChild = leftChild
107:       self.leftChild= rightChild
108:       self.rightChild= tempChild
109:     end