Ejemplo n.º 1
0
def create_tree():
    # create the binary tree
    r = BinaryTree('a')
    r.insertLeft(BinaryTree('b'))
    r.insertRight(BinaryTree('e'))

    r.getLeftChild.insertLeft(BinaryTree('e'))

    print(r.getRootVal())
    print(r.getLeftChild().getLeftChild())
Ejemplo n.º 2
0
def create_tree():
    # create the binary tree
    r = BinaryTree('book')
    r.insertLeft('chapter1')
    r.insertRight('chapter2')

    r.getLeftChild().insertLeft('section1.1')
    r.getLeftChild().insertRight('section1.2')

    r.getRightChild().insertLeft('section2.1')
    r.getRightChild().insertRight('section2.2')

    # print(r.getRootVal())
    # print(r.getLeftChild().getRootVal())
    # print(r.getLeftChild().getLeftChild().getRootVal())

    return r
Ejemplo n.º 3
0
import sys
sys.path.append("../")

from tree.binary_tree import BinaryTree
"""
we will create a binary tree looks like,
         a
      /      \
     b        e
   /   \    /   \
   c   d   f     g

"""

# create the binary tree
r = BinaryTree('a')

b = BinaryTree('b')
b.insertLeft('c')
b.insertRight('d')

e = BinaryTree('e')
e.insertLeft('f')
e.insertRight('g')

r.insertLeft(b)
r.insertRight(e)

print(r.getRootVal())
print(r.getLeftChild())
Ejemplo n.º 4
0
def create_tree():
   # create the binary tree
   r = BinaryTree('a')

   b = BinaryTree('b')
   b.insertLeft('c')
   b.insertRight('d')

   e = BinaryTree('e')
   e.insertLeft('f')
   e.insertRight('g')

   r.insertLeft(b)
   r.insertRight(e)

   print(r.getRootVal())
   print(r.getLeftChild().getLeftChild().getRootVal())
Ejemplo n.º 5
0
        if node.r_child:
            self.__post_order(node.r_child, path)
        path.append(node.data)
        return path

    def depth_first_search(self, node, mode):
        traversed = SinglyLinkedList()
        if node is None:
            return
        if mode is TreeTraverse.PRE_ORDER:
            self.__pre_order(node, traversed)
        elif mode is TreeTraverse.IN_ORDER:
            self.__in_order(node, traversed)
        elif mode is TreeTraverse.POST_ORDER:
            self.__post_order(node, traversed)
        return traversed


b = BinaryTree()
b.insert(10)
b.insert(6)
b.insert(15)
b.insert(3)
b.insert(8)
b.insert(20)
traverse = TreeTraverse()
print(traverse.breadth_first_search(b.root))
print(traverse.depth_first_search(b.root, TreeTraverse.PRE_ORDER))
print(traverse.depth_first_search(b.root, TreeTraverse.IN_ORDER))
print(traverse.depth_first_search(b.root, TreeTraverse.POST_ORDER))
def binary_tree():
    node = create_tree(sorted([15, 10, 8, 12, 20, 16, 25]))
    binary_tree = BinaryTree(node)
    return binary_tree
Ejemplo n.º 7
0
        prev = None
        while node is not None:
            prev = node
            if data < node.data:
                node = node.lchild
            elif data > node.data:
                node = node.rchild
            else:
                return node

        return prev


if __name__ == "__main__":
    bst = BST()
    BinaryTree.print_tree(bst.root)
    print

    data_tuple = (7, 3, 11, 1, 5, 9, 8)
    for data in data_tuple:
        bst.add(data)
        BinaryTree.print_tree(bst.root)
        print

    for data in data_tuple:
        assert bst.find_recursive(bst.root, data) is not None
        assert bst.find_non_recursive(data) is not None

    assert bst.find_non_recursive(10000) is None

    for data in data_tuple: