Chapter 7:Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004) Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery http:// parasol.tamu.edu
Chapter 7:Trees. Make Money Fast!. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004). Bank Robbery. Stock Fraud. Ponzi Scheme. - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Chapter 7:Trees
Nancy AmatoParasol Lab, Dept. CSE, Texas A&M University
Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004)
Make Money Fast!
StockFraud
PonziScheme
BankRobbery
http://parasol.tamu.edu
Trees
Outline and Reading
• Tree Definitions and ADT (§7.1)• Tree Traversal Algorithms for General Trees
(preorder and postorder) (§7.2)• BinaryTrees (§7.3)
• Traversals of Binary Trees (preorder, inorder, postorder, and Euler Tour) (§7.3.6)
• Template method pattern (§7.3.7)
• Data structures for trees (§7.1.4 and §7.3.4)• C++ implementation (§7.1.3 and §7.3.2)
2
Trees
What is a Tree
• In computer science, a tree is an abstract model of a hierarchical structure
• A tree consists of nodes with a parent-child relation
• Binary tree associated with an arithmetic expression• internal nodes: operators• leaves: operands
• Example: arithmetic expression tree for the expression (2 (a 1) (3 b))
2
a 1
3 b
13
Trees
Decision Tree
• Binary tree associated with a decision process• internal nodes: questions with yes/no answer• leaves: decisions
• Example: dining decision
Want a fast meal?
How about coffee? On expense account?
Starbucks Spike’s Al Forno Café Paragon
Yes No
Yes No Yes No
14
Trees
Properties of Binary Trees
• Notationn number of nodesl number of leavesi number of internal nodesh height
• Properties:• l i 1• n 2l 1• h i• h (n 1)2• l 2h
• h log2 l
• h log2 (n 1) 1
15
Trees
BinaryTree ADT
• The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT
• Additional methods:• position leftChild(p)• position rightChild(p)• position sibling(p)
• Update methods may be defined by data structures implementing the BinaryTree ADT
16
Trees
A Linked Structure for Binary Trees• A node is represented by
an object storing• Element• Parent node• Left child node• Right child node
• Node objects implement the Position ADT
B
DA
C E
B
A D
C E
17
Trees
Inorder Traversal
• In an inorder traversal a node is visited after its left subtree and before its right subtree
• Application: draw a binary tree
• x(v) = inorder rank of v• y(v) = depth of v
Algorithm inOrder(v)if isInternal(v)
inOrder(leftChild(v))visit(v)if isInternal(v)
inOrder(rightChild(v))
3
1
2
5
6
7 9
8
4
18
Trees
Exercise: Inorder Traversal
• In an inorder traversal a node is visited after its left subtree and before its right subtree
• List the nodes of this tree in inorder traversal order.
A
B C
G HE F
I J
Algorithm inOrder(v)if isInternal(v)
inOrder(leftChild(v))visit(v)if isInternal(v)
inOrder(rightChild(v))
19
Trees
Exercise: Preorder & InOrder Traversal
• Draw a (single) binary tree T, such that• Each internal node of T stores a single character• A preorder traversal of T yields EXAMFUN• An inorder traversal of T yields MAFXUEN
20
Trees
Print Arithmetic Expressions• Specialization of an inorder
traversal• print operand or operator when
visiting node• print “(“ before traversing left
subtree• print “)“ after traversing right
subtree
Algorithm printExpression(v)if isInternal(v)
print(“(’’)printExpression(leftChild(v))
print(v.element())if isInternal(v)
printExpression(rightChild(v))print (“)’’)
2
a 1
3 b((2 (a 1)) (3 b))
21
Trees
Evaluate Arithmetic Expressions• Specialization of a postorder
traversal• recursive method returning
the value of a subtree• when visiting an internal
node, combine the values of the subtrees
Algorithm evalExpr(v)if isExternal(v)
return v.element()else
x evalExpr(leftChild(v))
y evalExpr(rightChild(v))
operator stored at vreturn x y
2
5 1
3 2
22
Trees
Exercise: Arithmetic Expressions
• Draw an expression tree that has • Four leaves, storing the values 1, 5, 6, and 7• 3 internal nodes, storing operations +, -, *, /
(operators can be used more than once, but each internal node stores only one)
• The value of the root is 21
23
Trees
Euler Tour Traversal
• Generic traversal of a binary tree
• Includes as special cases the preorder, postorder and inorder traversals
• Walk around the tree and visit each node three times:• on the left (preorder)• from below (inorder)• on the right (postorder)