Parse Tree - Data Structures - Lecture Notes, Study notes of Data Structures and Algorithms

Some concept of Data Structures are Abstract, Balance Factor, Complete Binary Tree, Dynamically, Storage, Implementation, Sequential Search, Advanced Data Structures, Graph Coloring Two, Insertion Sort. Main points of this lecture are: Parse Tree, Indentify, Node Containing, Items, Children, Root Node, Edge, Parent, Levels, Height

Typology: Study notes

2012/2013

Uploaded on 04/30/2013

jut
jut ๐Ÿ‡ฎ๐Ÿ‡ณ

4.5

(63)

77 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Consider the parse tree for (9 + (5 * 3)) / (8 - 4):
/
+ -
9 *
5 3
8 4
a) Indentify the following items in the above tree:
๎˜
branch from root node to โ€œ3โ€
๎˜
parent of the node containing โ€œ3โ€
๎˜
path from node containing โ€œ+โ€ to node containing โ€œ5โ€
๎˜
children of the node containing โ€œ+โ€
๎˜
subtree whoโ€™s root is node contains โ€œ+โ€
๎˜
root node
๎˜
leaf nodes of the tree
๎˜
edge from node containing โ€œ-โ€ to node containing โ€œ8โ€
๎˜
siblings of the node containing โ€œ*โ€
๎˜
node containing โ€œ*โ€
b) Mark the levels of the tree (level is the number of edges on the path from the root)
c) What is the height (max. level) of the tree?
2. In Python an easy way to implement a tree is as a list of lists where a tree look like:
[ โ€œnode valueโ€, remaining items are subtrees for the node each implemented as a list of lists]
Complete the list-of-lists representation look like for the above parse tree:
[โ€™/โ€™, [โ€™+โ€™, ], [โ€™-โ€™, ] ]
3. Consider a โ€œlinkedโ€ representations of a BinaryTree. For the expression ((4 + 5) * 7), the binary tree would be:
class BinaryTree:
def __init__(self,rootObj):
self.key = rootObj
self.leftChild = None
self.rightChild = None
key
key
key
key
key
leftChild
leftChild
leftChild
leftChild
leftChild
rightChild
rightChild
rightChild
rightChild
rightChild
'*'
'+'
'4'
'7'
'5'
Data Structures Lecture 18 Name:__________________
Lecture 18 Page 1
Docsity.com
pf3

Partial preview of the text

Download Parse Tree - Data Structures - Lecture Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

1. Consider the parse tree for (9 + (5 * 3)) / (8 - 4):

a) Indentify the following items in the above tree:

 parent of the node containing โ€œ3โ€  branch from root node to โ€œ3โ€

 children of the node containing โ€œ+โ€  path from node containing โ€œ+โ€ to node containing โ€œ5โ€

 root node  subtree whoโ€™s root is node contains โ€œ+โ€

 edge from node containing โ€œ-โ€ to node containing โ€œ8โ€  leaf nodes of the tree

 node containing โ€œโ€  siblings of the node containing โ€œโ€

b) Mark the levels of the tree (level is the number of edges on the path from the root)

c) What is the height (max. level) of the tree?

2. In Python an easy way to implement a tree is as a list of lists where a tree look like:

[ โ€œnode valueโ€, remaining items are subtrees for the node each implemented as a list of lists]

Complete the list-of-lists representation look like for the above parse tree:

[โ€™/โ€™, [โ€™+โ€™, ], [โ€™-โ€™, ] ]

3. Consider a โ€œlinkedโ€ representations of a BinaryTree. For the expression ((4 + 5) * 7), the binary tree would be:

class BinaryTree: def init(self,rootObj): self.key = rootObj self.leftChild = None self.rightChild = None

key

key

key

key

key

leftChild

leftChild

leftChild

leftChild

leftChild

rightChild

rightChild

rightChild

rightChild

rightChild

Lecture 18 Page 1

a) Fix the insertLeft and insertRight code:

( (Listing 6.6 and 6.7 are wrong in the text on pp. 242-3)

b) Use these functions to build the BinaryTree for the

expression ((4 + 5) * 7).

Some corresponding external (non-class) functions:

Lecture 18 Page 2

def inorder(tree): if tree != None: inorder(tree.getLeftChild()) print(tree.getRootVal()) inorder(tree.getRightChild())

def printexp(tree): if tree.leftChild: print('(', end=' ') printexp(tree.getLeftChild()) print(tree.getRootVal(), end=' ') if tree.rightChild: printexp(tree.getRightChild()) print(')', end=' ')

def height(tree): if tree == None: return - else: return 1 + max(height(tree.leftChild), height(tree.rightChild))

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 operstree.getRootVal() else: return tree.getRootVal()

import operator class BinaryTree: def init(self,rootObj): self.key = rootObj self.leftChild = None self.rightChild = None

def insertLeft(self,newNode): if self.leftChild == None: self.leftChild=BinaryTree(newNode) else: t = BinaryTree(newNode) t.left = self.leftChild self.leftChild = t

def insertRight(self,newNode): if self.rightChild == None: self.rightChild=BinaryTree(newNode) else: t = BinaryTree(newNode) t.right = self.rightChild self.rightChild = t

def isLeaf(self): return ((not self.leftChild) and (not self.rightChild))

def getRightChild(self): return self.rightChild

def getLeftChild(self): return self.leftChild

def setRootVal(self,obj): self.key = obj

def getRootVal(self,): return self.key

def inorder(self): if self.leftChild: self.leftChild.inorder() print(self.key) if self.rightChild: self.rightChild.inorder()

def postorder(self): if self.leftChild: self.leftChild.postorder() if self.rightChild: self.rightChild.postorder() print(self.key)

def preorder(self): print(self.key) if self.leftChild: self.leftChild.preorder() if self.rightChild: self.rightChild.preorder()

def printexp(self): if self.leftChild: print('(', end=' ') self.leftChild.printexp() print(self.key, end=' ') if self.rightChild: self.rightChild.printexp() print(')', end=' ') def postordereval(self): opers = {'+':operator.add, '-':operator.sub, '*':operator.mul, '/':operator.truediv} res1 = None res2 = None if self.leftChild: res1 = self.leftChild.postordereval() if self.rightChild: res2 = self.rightChild.postordereval() if res1 and res2: return opersself.key else: return self.key