Example #1
0
    def __init__(self, key, value = None):
        BinaryTreeNode.__init__(self)

        self.key = key
        self.value = value

        if self.value == None:
            self.value = self.key
Example #2
0
 def test_init(self):
     data = 123
     node = BinaryTreeNode(data)
     assert node.data is data
     assert node.left is None
     assert node.right is None
Example #3
0
 def test_is_leaf(self):
     # Create node with no children
     node = BinaryTreeNode(2)
     assert node.is_leaf() is True
     # Attach left child node
     node.left = BinaryTreeNode(1)
     assert node.is_leaf() is False
     # Attach right child node
     node.right = BinaryTreeNode(3)
     assert node.is_leaf() is False
     # Detach left child node
     node.left = None
     assert node.is_leaf() is False
     # Detach right child node
     node.right = None
     assert node.is_leaf() is True
Example #4
0
 def test_height(self):
     # Create node with no children
     node = BinaryTreeNode(4)
     assert node.height() == 0
     # Attach left child node
     node.left = BinaryTreeNode(2)
     assert node.height() == 1
     # Attach right child node
     node.right = BinaryTreeNode(6)
     assert node.height() == 1
     # Attach left-left grandchild node
     node.left.left = BinaryTreeNode(1)
     assert node.height() == 2
     # Attach right-right grandchild node
     node.right.right = BinaryTreeNode(8)
     assert node.height() == 2
     # Attach right-right-left great-grandchild node
     node.right.right.left = BinaryTreeNode(7)
     assert node.height() == 3