def parseTree(expression):
    tokens = expression.split()
    tree_holder = Stack()
    main_tree = BinaryTree('')
    tree_holder.push(main_tree)
    currentTree = main_tree
    for val in tokens:
        if val == '(':
            currentTree.add_left('')
            tree_holder.push(currentTree)
            currentTree = currentTree.get_left()

        elif val not in '+-*/':
            currentTree.set_root_val(val)
            parent = tree_holder.pop()
            currentTree = parent

        elif val in '+-*/':
            currentTree.set_root_val(val)
            currentTree.add_right('')
            tree_holder.push(currentTree)
            currentTree = currentTree.get_right()

        elif val == ')':
            currentTree = tree_holder.pop()
        else:
            raise ValueError

    return main_tree
Example #2
0
def buildParseTree(fpexp):
	exp_list = fpexp.split() # split the expression in a list wth the delimateur being space
	stack = Stack()
	current_node = BinaryTree('')

	tokens = ['+', '-', '/', '*']

	#stack.push(current_node)
	
	for char in exp:

		if char == '(':

			current_node.insertLeft('')
			S.push(current_node) # push parent into the stack
			current_node = current_node.leftChild

		elif char.isdigit():

			current_node.key = int(char)

			current_node = S.pop()

		elif char in tokens:
			current_node.insertRight('')
			current_node.key = char
			S.push(current_node)
			current_node = current_node.rightChild

		elif char == ')':
			current_node = S.pop()

		else:
			raise ValueError
Example #3
0
def intersection(llist_1, llist_2):

    #edge cases
    if llist_1.size() == 0 or llist_2.size() == 0:
        return LinkedList()

    # Insert elements of llist_1 into bt1
    bt1 = BinaryTree()
    node = llist_1.head
    while node:
        bt1.insert(node.value)
        node = node.next

    # Insert elements of llist_2 into bt2
    bt2 = BinaryTree()
    node = llist_2.head
    while node:
        bt2.insert(node.value)
        tail2 = node
        node = node.next

    #link head of llist1 to the tail of llist2
    tail2.next = llist_1.head
    node = llist_2.head
    bt = BinaryTree()
    while node:
        if bt1.search(node.value) and bt2.search(node.value):
            bt.insert(node.value)
        node = node.next

    flatten(bt.get_root())

    # Empty btree into llist and return
    llist = LinkedList()
    node = bt.get_root()
    while node:
        llist.append(node.value)
        node = node.right
    return llist
Example #4
0
def union(llist_1, llist_2):
    # Insert elements of llist_1 into linked llist_1
    bt1 = BinaryTree()
    node = llist_1.head
    while node:
        bt1.insert(node.value)
        node = node.next

    # Fist turn elements of llist_2 into the same btree
    node = llist_2.head
    while node:
        bt1.insert(node.value)
        node = node.next

    # Flatten the binary tree
    flatten(bt1.get_root())

    # Empty btree into llist and return
    llist = LinkedList()
    node = bt1.get_root()
    while node:
        llist.append(node.value)
        node = node.right
    return llist
 def __init__(self, _name):
     self.name = _name
     self.groups = []
     self.users = []
     self.users_b = BinaryTree()
from data_structures import BinaryTree

tree = BinaryTree('')


def preorder(tree):
    if tree:
        print(tree.get_root())
        preorder(tree.get_left())
        preorder(tree.get_right())