def test_insert_right(self):
     t1 = BinaryTree("cypress")
     t1.insert_right("right branch")
     self.assertEquals(t1.rc.key, "right branch")
     t1.insert_right("new branch")
     self.assertEquals(t1.rc.key, "new branch")
     self.assertEquals(t1.rc.rc.key, "right branch")
 def test_insert_left(self):
     t1 = BinaryTree("redwood")
     t1.insert_left("left branch")
     self.assertEquals(t1.lc.key, "left branch")
     t1.insert_left("new branch")
     self.assertEquals(t1.lc.key, "new branch")
     self.assertEquals(t1.lc.lc.key, "left branch")
Beispiel #3
0
def buildparsetree(exp):
    plist = exp.split()
    pstack = Stack()
    tree = BinaryTree('')
    pstack.push(tree)
    current_tree = tree

    for item in plist:

        if item == '(':
            current_tree.insert_left('')
            pstack.push(current_tree)
            current_tree = current_tree.get_lc()

        elif item not in ['+', '-', '*', '/', ')']:
            current_tree.set_root(float(item))
            current_tree = pstack.pop()

        elif item in ['+', '-', '*', '/']:
            current_tree.set_root(item)
            current_tree.insert_right('')
            pstack.push(current_tree)
            current_tree = current_tree.get_rc()

        elif item == ')':
            current_tree = pstack.pop()

        else:
            raise ValueError

    return tree
Beispiel #4
0
    def insert(self, o):
        successful = BinaryTree.insert(self, o)
        if not successful:
            return False  # o is already in the tree
        else:
            self.balancePath(o)  # Balance from o to the root if necessary

        return True  # o is inserted
def main():
    bt = BinaryTree()
    createBinaryTree(bt)
    searchBinaryTree(bt)
    deleteBinaryTree(bt)
    avl = AVLTree()
    createAVLTree(avl)
    searchAVLTree(avl)
    deleteAVLTree(avl)
 def test_set_root(self):
     t1 = BinaryTree("cedar")
     t1.key = "oak"
     self.assertEquals(t1.key, "oak")
     t1.set_root("maple")
     self.assertEquals(t1.key, "maple")
     t1.set_root(100)
     self.assertEquals(t1.key, 100)
Beispiel #7
0
    def balancePath(self, o):
        path = BinaryTree.path(self, o)
        for i in range(len(path) - 1, -1, -1):
            A = path[i]
            self.updateHeight(A)
            parentOfA = None if (A == self.root) else path[i - 1]

            if self.balanceFactor(A) == -2:
                if self.balanceFactor(A.left) <= 0:
                    self.balanceLL(A, parentOfA)  # Perform LL rotation
                else:
                    self.balanceLR(A, parentOfA)  # Perform LR rotation
            elif self.balanceFactor(A) == +2:
                if self.balanceFactor(A.right) >= 0:
                    self.balanceRR(A, parentOfA)  # Perform RR rotation
                else:
                    self.balanceRL(A, parentOfA)  # Perform RL rotation
 def test_BinaryTree(self):
     # Check if you can make a new node of different types, or change types
     t1 = BinaryTree("maple")
     self.assertEquals(t1.key, "maple")
     t1.key = "oak"
     self.assertEquals(t1.key, "oak")
     t1.key = 0
     self.assertEquals(t1.key, 0)
     t1.key = [1, 2, 3]
     self.assertEquals(t1.key, [1, 2, 3])
     t2 = BinaryTree(0)
     self.assertEquals(t2.key, 0)
     t3 = BinaryTree([])
     self.assertEquals(t3.key, [])
     # Check that you can set the nodes
     t1.lc = "left leaf"
     t1.rc = "right leaf"
     self.assertEquals(t1.lc, "left leaf")
     self.assertEquals(t1.rc, "right leaf")
Beispiel #9
0
 def __init__(self):
     BinaryTree.__init__(self)
# 07.20.2016
# @totallygloria



from BinaryTreeClass import BinaryTree

def preorder(tree):

    if tree:
        print tree.get_root()
        preorder(tree.get_lc())
        preorder(tree.get_rc())


def postorder(tree):

    if tree is not None:
        postorder(tree.get_lc())
        postorder(tree.get_rc())
        print tree.get_root()


oak = BinaryTree("A")
oak.insert_left("B")
oak.insert_right("C")
oak.lc.insert_left("D")
oak.lc.insert_right("E")

preorder(oak)       # appears to work
postorder(oak)      # appears to work
Beispiel #11
0
# Algorithms and Data Structures: Preorder Traversal of Binary Tree
# 07.20.2016
# @totallygloria

from BinaryTreeClass import BinaryTree


def preorder(tree):

    if tree:
        print tree.get_root()
        preorder(tree.get_lc())
        preorder(tree.get_rc())


def postorder(tree):

    if tree is not None:
        postorder(tree.get_lc())
        postorder(tree.get_rc())
        print tree.get_root()


oak = BinaryTree("A")
oak.insert_left("B")
oak.insert_right("C")
oak.lc.insert_left("D")
oak.lc.insert_right("E")

preorder(oak)  # appears to work
postorder(oak)  # appears to work
 def test_get(self):
     t1 = BinaryTree("pine")
     t1.insert_right("right branch")
     t1.insert_left("left branch")
     self.assertEquals(t1.get_rc(), "right branch")
     self.assertEquals(t1.get_lc(), "left branch")
     self.assertEquals(t1.get_root(), "pine")
     t1.insert_right("right stick")
     t1.insert_left("left stick")
     t1.set_root("hemlock")
     self.assertEquals(t1.get_rc(), "right stick")
     self.assertEquals(t1.get_lc(), "left stick")
     self.assertEquals(t1.get_root(), "hemlock")
""""IMPORITING CLASSES WHICH HAS TO BE USED TO DRIVE THEM"""
from NodeStructure import Node
from BinaryTreeClass import BinaryTree

#<-3->implementing the binary tree
#<-3.1-> connnecting links

#             4
#           /   \
#          2     6
#         / \   /  \
#        1   3  5   7

binaryTree = BinaryTree()
binaryTree.root = Node('4')
binaryTree.root.left = Node('2')
binaryTree.root.right = Node('6')
binaryTree.root.left.left = Node('1')
binaryTree.root.left.right = Node('3')
binaryTree.root.right.right = Node('7')
binaryTree.root.right.left = Node('5')

binaryTree1 = BinaryTree()
binaryTree1.root = Node('4')
binaryTree1.root.left = Node('2')
binaryTree1.root.left.left = Node('1')
binaryTree1.root.left.right = Node('3')
binaryTree1.root.right = Node('6')
binaryTree1.root.right.right = Node('7')
binaryTree1.root.right.left = Node('5')
""" assigning root value of tree in variable"""