Ejemplo n.º 1
0
def chain_rule(currentNode, currentValue, dStack):
    currentNode.key = "*"
    power = currentNode.getRightChild().key

    innerTree = currentNode.getLeftChild()
    derInnerTree = BinaryTree(innerTree.key)
    derInnerNode = derInnerTree
    iStack = Stack()
    Misc.build_tree(currentNode.getLeftChild(), dStack, derInnerNode, iStack)
    derivative(derInnerTree)

    # building tree based on chain rule that will be inserted into original tree
    chainTree = BinaryTree('*')
    chainNode = chainTree
    chainNode.insertRight(derInnerTree)
    chainNode.insertLeft('*')
    chainNode = chainNode.getLeftChild()
    chainNode.insertLeft(power)
    chainNode.insertRight('^')
    chainNode = chainNode.getRightChild()
    chainNode.insertRight(power - 1)
    chainNode.insertLeft(innerTree)

    # inserting chain tree's first kids into original tree
    currentNode.insertLeft(chainTree.getLeftChild())
    currentNode.insertRight(chainTree.getRightChild())
Ejemplo n.º 2
0
def ParseTree(exp):
    exp = exp.split()
    expTree = BinaryTree('')  # create an empty tree
    parents = Stack()  # create stack to hold the parents
    parents.push(expTree)  # root is kept on bttm of stack

    for item in exp:

        if item == '(':  # next item will be var or int, followed by an operator - we need to move down
            expTree.insertLeft(
                ''
            )  # insert empty node on left which will be a num or int later
            parents.push(expTree)
            expTree = expTree.getLeftChild(
            )  # move down so num or var can be placed

        elif item == ')':
            expTree = parents.pop()

        elif item in OPERATORS:
            expTree.key = item  # operator is placed at current node
            expTree.insertRight('')
            parents.push(expTree)
            expTree = expTree.getRightChild(
            )  # we move right because a symbol or an num must be placed next

        elif item not in SYMBOLS and item not in OPERATORS and item != "(" and item != ")":
            expTree.key = float(
                item
            )  # entering them as float in case anyone inputs float values
            expTree = parents.pop()

        elif item in SYMBOLS:
            expTree.key = item
            expTree = parents.pop()

    return expTree
Ejemplo n.º 3
0
# _*_ coding: utf-8 -*-
# @Time      : 8/27/20 3:19 PM
# @Author    : title_z
# @Filename  : Binarytree.py

from pythonds.trees import BinaryTree

# r = BinaryTree(3)
# BinaryTree.insertLeft(r, 4)
# BinaryTree.insertLeft(r, 5)
# BinaryTree.insertRight(r, 6)
# BinaryTree.insertRight(r, 7)
# l = BinaryTree.getLeftChild(r)
# print(l)

r = BinaryTree('a')
r.insertLeft('b')
r.insertRight('c')
r.getRightChild().setRootVal('hello')
r.getLeftChild().insertRight('d')
print(r)