def buildParseTree(fpexp): fplist = fpexp.split() pStack = Stack() eTree = BinaryTree('') pStack.push(eTree) for i in fplist: if i == '(': # 如果当前符号是“(”,添加一个新节点作为当前节点的左子节点,并且下降到左子节点 eTree.insertLeft('') pStack.push(eTree) eTree = eTree.getLeftChild() elif i not in ['+', '-', '/', '*', ')']: # 如果当前符号是数字,将当前节点的根值设置为该数字并返回父节点 eTree.setRootVal(int(i)) parent = pStack.pop() eTree = parent elif i in ['+', '-', '/', '*']: eTree.setRootVal(i) eTree.insertRight('') pStack.push(eTree) eTree = eTree.getRightChild() elif i == ')': eTree = pStack.pop() else: raise ValueError return eTree
def build_tree_condition(elements): if len(elements) == 1: return elements[0] tree = BinaryTree('') for i in range(len(elements)): if type(elements[i]) is BinaryTree: if tree.getRootVal() == '': tree.insertLeft(elements[i]) else: tree.insertRight(elements[i]) elif tree.getRootVal() in ['AND', 'OR', 'or', 'and']: new_tree = BinaryTree('') new_tree.setRootVal(elements[i]) new_tree.insertLeft(tree) tree = new_tree else: tree.setRootVal(elements[i]) return tree
def build_parse_tree(fpexp): fplist = fpexp.split() for i in fplist: if i.__contains__('(') and i != '(': index = fplist.index(i) i_l = i.split('(') fplist[index] = i_l[-1] for j in range(len(i_l)-1): fplist.insert(index, '(') if i.__contains__(')') and i != ')': index = fplist.index(i) i_l = i.split(')') fplist[index] = i.split(')')[0] for j in range(len(i_l)-1): fplist.insert(index+1, ')') pstack = Stack() current_tree = BinaryTree('') current_tree.insertLeft('') pstack.push(current_tree) current_tree = current_tree.getLeftChild() for i in fplist: if i == '(': current_tree.insertLeft('') pstack.push(current_tree) current_tree = current_tree.getLeftChild() elif i not in ['and', 'or', ')']: current_tree.setRootVal(i) parent = pstack.pop() current_tree = parent elif i in ['and', 'or']: if current_tree.getRootVal() != "": pstack.push(current_tree) current_tree = BinaryTree('') current_tree.leftChild = pstack.pop() current_tree.setRootVal(i) current_tree.insertRight('') pstack.push(current_tree) current_tree = current_tree.getRightChild() elif i == ')': current_tree = pstack.pop() else: raise ValueError return current_tree
from pythonds.trees.binaryTree import BinaryTree import operator x = BinaryTree('*') x.insertLeft('+') l = x.getLeftChild() l.insertLeft(4) l.insertRight(5) x.insertRight(7) def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sVal + str(tree.getRootVal()) sVal = sVal + printexp(tree.getRightChild()) + ')' return sVal def postordereval(tree): opers = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.truediv } res1 = None res2 = None if tree: res1 = postordereval(tree.getLeftChild())
from pythonds.trees.binaryTree import BinaryTree import operator x = BinaryTree('*') x.insertLeft('+') l = x.getLeftChild() l.insertLeft(4) l.insertRight(5) x.insertRight(7) def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sVal + str(tree.getRootVal()) sVal = sVal + printexp(tree.getRightChild())+')' return sVal def postordereval(tree): opers = {'+':operator.add, '-':operator.sub, '*':operator.mul, '/':operator.truediv} res1 = None res2 = None if tree: res1 = postordereval(tree.getLeftChild()) res2 = postordereval(tree.getRightChild()) if res1 and res2: return opers[tree.getRootVal()](res1,res2) else: return tree.getRootVal()
def build_tree_expression(elements): stack = Stack() tree = BinaryTree('') if len(elements) == 1: tree.setRootVal(elements[0]) return tree stack.push(tree) for i in range(len(elements)): if (elements[i] == '(') or (i == 0): tree.insertLeft('') if elements[i] == '(': stack.push("(") stack.push(tree) tree = tree.getLeftChild() if elements[i] != '(': tree.setRootVal(elements[i]) parent = stack.pop() tree = parent elif elements[i] not in ['+', '-', '*', '/', ')']: tree.setRootVal(elements[i]) parent = stack.pop() tree = parent elif elements[i] in ['+', '-', '*', '/']: sign = elements[i] if tree.getRootVal() in ['+', '-', '*', '/']: if sign in ['+', '-'] or elements[i - 1] == ')': temp = BinaryTree('') parent = stack.pop() if stack.size() == 0: temp.insertLeft(parent) parent = temp tree = parent stack.push(parent) elif parent == "(": temp.insertLeft(tree) parent = stack.pop() parent.insertRight(temp) stack.push(parent) stack.push("(") tree = parent.getRightChild() else: temp.insertLeft(tree) parent.insertRight(temp) stack.push(parent) tree = parent.getRightChild() elif sign in ['*', '/']: rChild = tree.getRightChild() rChild.insertLeft(rChild.getRootVal()) tree = rChild tree.setRootVal(elements[i]) tree.insertRight('') stack.push(tree) tree = tree.getRightChild() elif elements[i] == ')': parent = "" while parent != "(": parent = stack.pop() if parent == "(": tree = stack.pop() stack.push(tree) if i + 1 == len(elements): for j in range(stack.size()): parent = stack.pop() tree = parent return tree
def p_tree_selects(p): '''tree_selects : LBRACKET tree_selects RBRACKET union LBRACKET tree_selects RBRACKET | LBRACKET tree_selects RBRACKET intersect LBRACKET tree_selects RBRACKET | LBRACKET tree_selects RBRACKET join LBRACKET tree_selects RBRACKET join_condition | LBRACKET tree_selects RBRACKET join LBRACKET tree_selects RBRACKET | LBRACKET tree_selects RBRACKET union select | LBRACKET tree_selects RBRACKET intersect select | LBRACKET tree_selects RBRACKET join select join_condition | LBRACKET tree_selects RBRACKET join select | LBRACKET tree_selects RBRACKET join name_table join_condition | LBRACKET tree_selects RBRACKET join name_table | select union LBRACKET tree_selects RBRACKET | select intersect LBRACKET tree_selects RBRACKET | select join LBRACKET tree_selects RBRACKET AS name_table join_condition | select join LBRACKET tree_selects RBRACKET AS name_table | select union select | select intersect select | select join select join_condition | select join select | select join name_table join_condition | select join name_table | select''' tree = BinaryTree('') if len(p) == 2: tree.setRootVal(p[1]) p[0] = tree elif len(p) == 4: tree.insertLeft(p[1]) tree.setRootVal(p[2]) tree.insertRight(p[3]) elif len(p) == 5: tree.insertLeft(p[1]) temp = p[2] temp.join_condition = p[4] tree.setRootVal(temp) tree.insertRight(p[3]) elif len(p) == 6: if p[1] == "(": tree.insertLeft(p[2]) tree.setRootVal(p[4]) tree.insertRight(p[5]) else: tree.insertLeft(p[1]) tree.setRootVal(p[2]) tree.insertRight(p[4]) elif len(p) == 7: tree.insertLeft(p[2]) temp = p[4] temp.join_condition = p[6] tree.setRootVal(temp) tree.insertRight(p[5]) elif len(p) == 8: if p[5] == "(": tree.insertLeft(p[2]) tree.setRootVal(p[4]) tree.insertRight(p[6]) else: tree.insertLeft(p[1]) tree.setRootVal(p[2]) tree.insertRight(PNamedTree(p[4], p[7])) elif len(p) == 9: if p[5] == "(": tree.insertLeft(p[2]) temp = p[4] temp.join_condition = p[8] tree.setRootVal(temp) tree.insertRight(p[6]) else: tree.insertLeft(p[1]) temp = p[2] temp.join_condition = p[8] tree.setRootVal(temp) tree.insertRight(PNamedTree(p[4], p[7])) p[0] = PTreeSelects(tree)
# binary tree or not from pythonds.trees.binaryTree import BinaryTree prev=None def isBinarySearchTree(root): global prev if root: if not isBinarySearchTree(root.leftChild): return False if (prev and prev.key>root.key): return False prev=root if not isBinarySearchTree(root.rightChild): return False return True tree=BinaryTree(4) tree.insertLeft(2) tree.insertRight(5) ls=tree.getLeftChild() ls.insertLeft(1) ls.insertRight(3) print isBinarySearchTree(tree)
if treeNode.getRightChild(): leftNode = treeNode.getLeftChild() rightNode = treeNode.getRightChild() return BinaryExpression(treeNode.key, ExpressionFactory.getExp(leftNode), ExpressionFactory.getExp(rightNode)) elif treeNode.getLeftChild(): leftNode = treeNode.getLeftChild() return UnaryExpression(treeNode.key, ExpressionFactory.getExp(leftNode)) else: return ConstExpression(treeNode.key) if __name__ == "__main__": #strExp="-12*(3+5)+6" rootNode = Tree("+") rootNode.insertRight(6) rootNode.insertLeft("*") treeNode = rootNode.getLeftChild() treeNode.insertLeft("-") tmpNode = treeNode.getLeftChild() tmpNode.insertLeft(12) treeNode.insertRight("+") tmpNode = treeNode.getRightChild() tmpNode.insertLeft(3) tmpNode.insertRight(5) exp = ExpressionFactory.getExp(rootNode) print exp.interpret()
izq = "" if len(parteIzquierda>1): for a in parteIzquierda: for b in a: izq += list(b.keys())[0] else: for a in parteIzquierda: print("a in izq",a) ########print(parteIzquierda, "=>", parteDerecha) #break return [pivote, izq, der] ###print("siguiente...") [pivot, pIzq, pDer] = buscaPivote(regex) print(pivot) print(pIzq) print(pDer) raizArbol = ArbolBinario('.') raizArbol.insertRight('#') """ while pIzq != []: buscaPivote(pIzq) pass """
# binary tree or not from pythonds.trees.binaryTree import BinaryTree prev = None def isBinarySearchTree(root): global prev if root: if not isBinarySearchTree(root.leftChild): return False if (prev and prev.key > root.key): return False prev = root if not isBinarySearchTree(root.rightChild): return False return True tree = BinaryTree(4) tree.insertLeft(2) tree.insertRight(5) ls = tree.getLeftChild() ls.insertLeft(1) ls.insertRight(3) print isBinarySearchTree(tree)
#!/usr/local/python3.5/bin # -*- coding:utf-8 -*- from pythonds.trees.binaryTree import BinaryTree myTree = BinaryTree('A') # print(myTree.getRootVal()) myTree.insertLeft('B') myTree.insertRight('C') # print(myTree.getLeftChild().getRootVal()) # print(myTree.getRightChild().getRootVal()) myTree.getLeftChild().insertLeft('D') myTree.getLeftChild().insertRight('E') # print(myTree.getLeftChild().getLeftChild().getRootVal()) # print(myTree.getLeftChild().getRightChild().getRootVal()) myTree.getRightChild().insertLeft('F') myTree.getRightChild().insertRight('G') # print(myTree.getRightChild().getLeftChild().getRootVal()) # print(myTree.getRightChild().getRightChild().getRootVal()) myTree.getLeftChild().getRightChild().insertLeft('H') myTree.getLeftChild().getRightChild().insertRight('I') # print(myTree.getLeftChild().getRightChild().getLeftChild().getRootVal()) # print(myTree.getLeftChild().getRightChild().getRightChild().getRootVal()) myTree.getRightChild().getRightChild().insertLeft('J') myTree.getRightChild().getRightChild().insertRight('K') # print(myTree.getRightChild().getRightChild().getLeftChild().getRootVal()) # print(myTree.getRightChild().getRightChild().getRightChild().getRootVal()) # 前序遍历
r = BinaryTree('a') print(r.getRootVal()) #a print(r.getLeftChild()) #None r.insertLeft('b') print(r.getLeftChild()) #<__main__.BinaryTree object at 0x0000000005FFC780> print(r.getLeftChild().getRootVal()) #b r.insertRight('c') print(r.getRightChild()) #<__main__.BinaryTree object at 0x0000000005DA2898> print(r.getRightChild().getRootVal()) #c r.getRightChild().setRootVal('hello') print(r.getRightChild().getRootVal()) from pythonds.basic.stack import Stack from pythonds.trees.binaryTree import BinaryTree def buildParseTree(formula): formulalist = formula.split() #['(','3','+','(','4','*','5',')',')'] rootStack = Stack() #用于跟踪父节点 Tree = BinaryTree('') #创建一个二叉树 rootStack.push(Tree) currentTree = Tree for i in formulalist: if i == '(': currentTree.insertLeft('') rootStack.push(currentTree) currentTree = currentTree.getLeftChild() elif i not in ['+', '-', '*', '/', ')']: currentTree.setRootVal(int(i)) parent = rootStack.pop() cerrentTree = parent