ceng2001_week9
ceng2001_week9
ceng2001_week9
Height:
def insertRight(root,newBranch):
t = root[2] # get current right
root[2] = [newBranch, [], t]
return root
5 2
t = Bintree(1)
insertLeft(t,5)
insertRight(t,2)
insertLeft(getRightChild(t),9)
Implement as linked structure in Class
class BinaryTree: def getRightChild(self): return self.rightChild
def __init__(self,rootObj):
self.key = rootObj def getLeftChild(self): return self.leftChild
self.leftChild = None
self.rightChild = None def setRootVal(self,obj): self.key = obj
def insertRight(self,newNode):
oldright = self.rightChild
self.rightChild = BinaryTree(newNode)
self.rightChild.rightChild = oldright
do this
How to do it:
# a
# / \
# b c
# \ / \
# d e f
r = BinaryTree('a')
r.insertLeft('b')
r.getLeftChild().insertRight('d')
r.insertRight('f')
r.insertRight('c')
r.getRightChild().insertLeft('e')
Parse Tree
You can use parse tree for structured
languages like english or math
Figure 2:
Parse Tree for ((7+3)∗(5−2))
doing math expression parse tree:
In the rest of this section we are
going to examine parse trees in
more detail. In particular we will
look at
or as an list
( 3 + ( 4 * 5 ) )
1
( 3 + ( 4 * 5 ) )
^
If the current token is a number, set the root value of the current
node to the number and return to the parent.
2
( 3 + ( 4 * 5 ) )
^
If the current token is in the list ['+','-','/','*'], set the root value of
the current node to the operator represented by the current
token. Add a new node as the right child of the current node and
descend to the right child.
3
( 3 + ( 4 * 5 ) )
^
If the current token is a '(', add a new node as the left child of
the current node, and descend to the left child.
4
( 3 + ( 4 * 5 ) )
^
If the current token is a number, set the root value of the current
node to the number and return to the parent.
6
( 3 + ( 4 * 5 ) )
^
If the current token is a number, set the root value of the current
node to the number and return to the parent.
7
( 3 + ( 4 * 5 ) )
^
If the current token is in the list ['+','-','/','*'], set the root value of
the current node to the operator represented by the current
token. Add a new node as the right child of the current node and
descend to the right child.
8
( 3 + ( 4 * 5 ) )
3 *
4 5
9-10
Check out the code for building Parse tree
evaluate it, recall we can replace
subtrees with the result as shown:
30
Evaluation: how to recurse
• base case, look for both children being a leaf
( leaf is a node that has no children)
For leaf, we just return the value
• For other non leaf nodes, we need to perform
the operator on the two child subtree values
user of operator python module
• Note we are going to use the operator module
to access the methods for add, subtract (sub),
multiply (mul) and divide (truediv), and
because functions can be stored in variables,
we will store the correct function in a
dictionary that maps the token to the correct
operator python function
opers = {
'+':operator.add, '-':operator.sub,
'*':operator.mul, '/':operator.truediv}
Check out the code for evaluation of parse tree
Tree Traversal
Way to visit all the nodes in Tree
• There are three systematic ways to visit all the
nodes of a binary tree:
1. preorder – root > left > right
2. inorder – left > root > right
3. postorder – left > right > root
Remember math expressions
• this is similar to math expressions we already
covered
• prefix, infix, and postfix expressions
• It exactly corresponds to these terms if we are
looking a an expression tree structure
+
1 2
+ 1 2 is a prefix visit nodes +
expression + then 1 then 2
is preorder 1 2
def preorder(self):
print(self.key)
as method
if self.leftChild: # do if not leaf
self.left.preorder() inside
if self.rightChild: # do if not leaf of Tree class
self.right.preorder()
preorder sample
root > left child > right child
numbers show
the order 1
of traversal
visits
2 7
3 4 8
5 6
inorder sample
left child > root > right child
numbers show
the order 6
of traversal
visits
2 7
1 4 8
3 5
postorder sample
left child > right child > root
numbers show
the order 8
of traversal
visits
5 7
1 4 6
2 3
postorder traversal code
def postorder(tree):
if tree != None: # do nothing if from leaf
postorder(tree.getLeftChild())
postorder(tree.getRightChild())
print(tree.getRootVal())
use postorder to eval expression tree