Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004...
Transcript of Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004...
![Page 1: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/1.jpg)
Trees 1© 2004 Goodrich, Tamassia
Trees
Make Money Fast!
StockFraud
PonziScheme
BankRobbery
![Page 2: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/2.jpg)
Trees 2© 2004 Goodrich, Tamassia
What is a TreeIn computer science, atree is an abstract modelof a hierarchicalstructureA tree consists of nodeswith a parent-childrelationApplications: Organization charts File systems Programming
environments
Computers”R”Us
Sales R&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
![Page 3: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/3.jpg)
Trees 3© 2004 Goodrich, Tamassia
subtree
Tree TerminologyRoot:Internal node:
External node (a.k.a. leaf ):
Ancestors of a node:
Depth of a node:
Height of a tree:
Descendant of a node:
A
B DC
G HE F
I J K
Subtree:Root: node without parent (A)Internal node: node with at leastone child (A, B, C, F)External node (a.k.a. leaf ): nodewithout children (E, I, J, K, G, H, D)Ancestors of a node: parent,grandparent, grand-grandparent,etc.Depth of a node: number ofancestorsHeight of a tree: maximum depthof any node (3)Descendant of a node: child,grandchild, grand-grandchild, etc.
Subtree: tree consisting ofa node and itsdescendants
![Page 4: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/4.jpg)
Trees 4© 2004 Goodrich, Tamassia
Preorder TraversalA traversal visits the nodes of atree in a systematic mannerIn a preorder traversal, a node isvisited before its descendantsApplication: print a structureddocument
Make Money Fast!
1. Motivations References2. Methods
2.1 StockFraud
2.2 PonziScheme1.1 Greed 1.2 Avidity
2.3 BankRobbery
1
2
3
5
4 6 7 8
9
Algorithm preOrder(v)visit(v)for each child w of v
preorder (w)
![Page 5: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/5.jpg)
Trees 5© 2004 Goodrich, Tamassia
Postorder TraversalIn a postorder traversal, anode is visited after itsdescendantsApplication: compute spaceused by files in a directory andits subdirectories
Algorithm postOrder(v)for each child w of v
postOrder (w)visit(v)
cs16/
homeworks/todo.txt
1Kprograms/
DDR.java10K
Stocks.java25K
h1c.doc3K
h1nc.doc2K
Robot.java20K
9
3
1
7
2 4 5 6
8
![Page 6: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/6.jpg)
Trees 6© 2004 Goodrich, Tamassia
Binary TreesA binary tree is a tree with thefollowing properties: Each internal node has at most two
children (exactly two for properbinary trees)
The children of a node are anordered pair
We call the children of an internalnode left child and right childAlternative recursive definition: abinary tree is either a tree consisting of a single node, or a tree whose root has an ordered
pair of children, each of which is abinary tree
Applications: arithmetic expressions decision processes searching
A
B C
F GD E
H I
![Page 7: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/7.jpg)
Trees 7© 2004 Goodrich, Tamassia
Arithmetic Expression TreeBinary tree associated with an arithmetic expression internal nodes: operators external nodes: operands
Example: arithmetic expression tree for theexpression (2 × (a − 1) + (3 × b))
+
××
−2
a 1
3 b
![Page 8: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/8.jpg)
Trees 8© 2004 Goodrich, Tamassia
Decision TreeBinary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions
Example: dining decision
Want a fast meal?
How about coffee? On expense account?
Starbucks Quizno’s Tarpy’s Café Del Monte
Yes No
Yes No Yes No
![Page 9: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/9.jpg)
Trees 9© 2004 Goodrich, Tamassia
Properties of Proper Binary TreesNotationn number of nodese number of
external nodesi number of internal
nodesh height
Properties: h + 1 ≤ n ≤ 2h+1 − 1 1 ≤ e ≤ 2h
h ≤ i ≤ 2h − 1
log2 (n + 1) - 1 ≤ h ≤ (n + 1) / 2
![Page 10: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/10.jpg)
Trees 10© 2004 Goodrich, Tamassia
Inorder TraversalIn an inorder traversal anode is visited after its leftsubtree and before its rightsubtreeApplication: draw a binarytree x(v) = inorder rank of v y(v) = depth of v
Algorithm inOrder(v)if hasLeft (v)
inOrder (left (v))visit(v)if hasRight (v)
inOrder (right (v))
3
1
2
5
6
7 9
8
4
![Page 11: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/11.jpg)
Trees 11© 2004 Goodrich, Tamassia
Print Arithmetic ExpressionsSpecialization of an inordertraversal print operand or operator
when visiting node print “(“ before traversing left
subtree print “)“ after traversing right
subtree
Algorithm printExpression(v)if hasLeft (v)
print(“(’’)inOrder (left(v))
print(v.element ())if hasRight (v)
inOrder (right(v))print (“)’’)
+
××
−2
a 1
3 b((2 × (a − 1)) + (3 × b))
![Page 12: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/12.jpg)
Trees 12© 2004 Goodrich, Tamassia
Evaluate Arithmetic ExpressionsSpecialization of a postordertraversal recursive method returning
the value of a subtree when visiting an internal
node, combine the valuesof 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
![Page 13: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/13.jpg)
Trees 13© 2004 Goodrich, Tamassia
Euler Tour TraversalGeneric traversal of a binary treeIncludes a special cases the preorder, postorder and inorder traversalsWalk around the tree and visit each node three times: on the left (preorder) from below (inorder) on the right (postorder)
+
×
−2
5 1
3 2
LB
R×
![Page 14: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/14.jpg)
Trees 14© 2004 Goodrich, Tamassia
∅
Linked Structure for TreesA node is representedby an object storing Element Parent node Sequence of children
nodes
B
DA
C E
F
B
∅ ∅
A D F
∅
C
∅
E
![Page 15: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/15.jpg)
Trees 15© 2004 Goodrich, Tamassia
Linked Structure for Binary TreesA node is representedby an object storing Element Parent node Left child node Right child node
B
DA
C E
∅ ∅
∅ ∅ ∅ ∅
B
A D
C E
∅
![Page 16: Trees - faculty.nps.edufaculty.nps.edu/kmsquire/cs3901/section2/notes/06a_Trees.pdf · © 2004 Goodrich, Tamassia Trees 2 What is a Tree In computer science, a tree is an abstract](https://reader035.fdocuments.us/reader035/viewer/2022070912/5fb44470ba349928291c38bc/html5/thumbnails/16.jpg)
Trees 16© 2004 Goodrich, Tamassia
Array-Based Representation ofBinary Trees
nodes are stored in an array
…
let rank(node) be defined as follows: rank(root) = 1 if node is the left child of parent(node),
rank(node) = 2*rank(parent(node)) if node is the right child of parent(node),
rank(node) = 2*rank(parent(node))+1
1
2 3
6 74 5
10 11
A
HG
FE
D
C
B
J