1. Binary Tree
Each node in a binary tree cannot have more than two sons.
1.1 Binary Tree List Implementation
For example, the binary tree list can be used to indicate:
Tree = ['A', # root ['B', # Left subtree ['D', [], [], ['E', [], [], ['C', # right subtree ['F', [], [], []
Implementation:
def BinaryTree(item): return [item,[],[]]def insertLeft(tree,item): leftSubtree=tree.pop(1) if leftSubtree: tree.insert(1,[item,leftSubtree,[]]) else: tree.insert(1,[item,[],[]]) return treedef insertRight(tree,item): rightSubtree=tree.pop(2) if rightSubtree: tree.insert(2,[item,[],rightSubtree]) else: tree.insert(2,[item,[],[]]) return treedef getLeftChild(tree): return tree[1]def getRightChild(tree): return tree[2]
Tree to be implemented:
tree=BinaryTree('a')insertLeft(tree,'b')insertRight(tree,'c')insertRight((getLeftChild(tree)),'d')insertLeft((getRightChild(tree)),'e')insertRight((getRightChild(tree)),'f')
1.2 binary tree class implementation
class BinaryTree(object): def __init__(self,item): self.key=item self.leftChild=None self.rightChild=None def insertLeft(self,item): if self.leftChild==None: self.leftChild=BinaryTree(item) else: t=BinaryTree(item) t.leftChild=self.leftChild self.leftChild=t def insertRight(self,item): if self.rightChild==None: self.rightChild=BinaryTree(item) else: t=BinaryTree(item) t.rightChild=self.rightChild self.rightChild=t
2. Expression Tree
The leaf of the expression tree is the operand, and other nodes are operators.
Expression Tree Representation of an image (7 + 3) * (5-2)
2.1 construct an Expression Tree Based on the infix expression:
Traversal expression:
1. Create an empty tree
2. If '(' is encountered, add a left child for the current Node and use left child as the current Node.
3. In case of a number, assign the value to the current Node and return the parent as the current Node.
4. In case of ('+-*/'), assign a value to the current Node, add a Node as the right child, and use the right child as the current Node.
5. If ')' is encountered, the current Node's parent is returned.
def buildexpressionTree(exp): tree=BinaryTree('') stack=[] stack.append(tree) currentTree=tree for i in exp: if i=='(': currentTree.insertLeft('') stack.append(currentTree) currentTree=currentTree.leftChild elif i not in '+-*/()': currentTree.key=int(i) parent=stack.pop() currentTree=parent elif i in '+-*/': currentTree.key=i currentTree.insertRight('') stack.append(currentTree) currentTree=currentTree.rightChild elif i==')': currentTree=stack.pop() else: raise ValueError return tree3. tree traversal
3.1 preorder travelsal)
Print the root, and then print the left and right subtree recursively.
def preorder(tree,nodelist=None): if nodelist is None: nodelist=[] if tree: nodelist.append(tree.key) preorder(tree.leftChild,nodelist) preorder(tree.rightChild,nodelist) return nodelist
3.2 inorder travelsal)
Print the left subtree recursively, then print the root, and then print the right subtree recursively, corresponding to the infix expression
def inorder(tree): if tree: inorder(tree.leftChild) print tree.key inorder(tree.rightChild)
3.3 Post-sequential traversal (postorder travelsal)
Recursively print the left and right subtree, and then print the root, corresponding to the suffix expression
def postorder(tree): if tree: for key in postorder(tree.leftChild): yield key for key in postorder(tree.rightChild): yield key yield tree.key
3.4 evaluate the Expression Tree
def postordereval(tree): operators={'+':operator.add,'-':operator.sub,'*':operator.mul,'/':operator.truediv} leftvalue=None rightvalue=None if tree: leftvalue=postordereval(tree.leftChild) rightvalue=postordereval(tree.rightChild) if leftvalue and rightvalue: return operators[tree.key](leftvalue,rightvalue) else: return tree.key