Trees: Make Money Fast!

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Trees

Make Money Fast!

Stock Ponzi Bank


Fraud Scheme Robbery

© 2010 Goodrich, Tamassia Trees 1


What is a Tree
 In computer science, a
tree is an abstract model Computers”R”Us
of a hierarchical
structure
 A tree consists of nodes Sales Manufacturing R&D
with a parent-child
relation
US International Laptops Desktops
 Applications:
 Organization charts
 File systems Europe Asia Canada
 Programming
environments

© 2010 Goodrich, Tamassia Trees 2


Tree Terminology
 Root: node without parent (A)  Subtree: tree consisting of

 Internal node: node with at least a node and its


one child (A, B, C, F) descendants
 External node (a.k.a. leaf ): node A
without children (E, I, J, K, G, H, D)
 Ancestors of a node: parent,
grandparent, grand-grandparent, B C D
etc.
 Depth of a node: number of
ancestors E F G H
 Height of a tree: maximum depth
of any node (3)
 Descendant of a node: child, I J K
grandchild, grand-grandchild, etc.

© 2010 Goodrich, Tamassia Trees subtree3


Tree ADT
 We use positions to abstract Query methods:
nodes  boolean p.isRoot()
 Generic methods:  boolean p.isExternal()
 integer size() Additional update methods
 boolean empty() may be defined by data
 Accessor methods: structures implementing the
 position root() Tree ADT
 list<position> positions()
 Position-based methods:
 position p.parent()
 list<position> p.children()

© 2010 Goodrich, Tamassia Trees 4


Preorder Traversal
 A traversal visits the nodes of a Algorithm preOrder(v)
tree in a systematic manner
visit(v)
 In a preorder traversal, a node is
visited before its descendants for each child w of v
 Application: print a structured preorder (w)
document
1
Make Money Fast!

2 5 9
1. Motivations 2. Methods References

6 7 8
3 4
2.1 Stock 2.2 Ponzi 2.3 Bank
1.1 Greed 1.2 Avidity
Fraud Scheme Robbery

© 2010 Goodrich, Tamassia Trees 5


Postorder Traversal
 In a postorder traversal, a Algorithm postOrder(v)
node is visited after its
descendants for each child w of v
 Application: compute space postOrder (w)
used by files in a directory and visit(v)
its subdirectories
9
cs16/

8
3 7
todo.txt
homeworks/ programs/
1K

1 2 4 5 6
h1c.doc h1nc.doc DDR.cpp Stocks.cpp Robot.cpp
3K 2K 10K 25K 20K

© 2010 Goodrich, Tamassia Trees 6


Binary Trees
 A binary tree is a tree with the  Applications:
following properties:  arithmetic expressions
 Each internal node has at most two  decision processes
children (exactly two for proper
binary trees)
 searching
 The children of a node are an
A
ordered pair
 We call the children of an internal
node left child and right child
B C
 Alternative recursive definition: a
binary tree is either
 a tree consisting of a single node, or
D E F G
 a tree whose root has an ordered
pair of children, each of which is a
binary tree
H I

© 2010 Goodrich, Tamassia Trees 7


Arithmetic Expression Tree
 Binary tree associated with an arithmetic expression
 internal nodes: operators
 external nodes: operands
 Example: arithmetic expression tree for the
expression (2 (a  1)  (3 b))

 

2  3 b

a 1

© 2010 Goodrich, Tamassia Trees 8


Decision Tree
 Binary tree associated with a decision process
 internal nodes: questions with yes/no answer
 external nodes: decisions
 Example: dining decision

Want a fast meal?


Yes No

How about coffee? On expense account?


Yes No Yes No

Starbucks Spike’s Al Forno Café Paragon


© 2010 Goodrich, Tamassia Trees 9
Properties of Proper Binary Trees
 Notation Properties:
n number of nodes  e  i  1
e number of  n  2e  1
external nodes
 h i
i number of internal
nodes  h (n  1)2

h height  e  2h

 h  log e
2

 h  log2 (n  1)  1

© 2010 Goodrich, Tamassia Trees 10


BinaryTree ADT
 The BinaryTree ADT  Update methods
extends the Tree may be defined by
ADT, i.e., it inherits data structures
all the methods of implementing the
the Tree ADT BinaryTree ADT
 Additional methods:  Proper binary tree:
 position p.left() Each node has
 position p.right() either 0 or 2
children

© 2010 Goodrich, Tamassia Trees 11


Inorder Traversal
 In an inorder traversal a Algorithm inOrder(v)
node is visited after its left
subtree and before its right if  v.isExternal()
subtree inOrder(v.left())
 Application: draw a binary visit(v)
tree
 x(v) = inorder rank of v if  v.isExternal()
 y(v) = depth of v
6 inOrder(v.right())

2 8

1 4 7 9

3 5

© 2010 Goodrich, Tamassia Trees 12


Print Arithmetic Expressions
 Specialization of an inorder Algorithm printExpression(v)
traversal
 print operand or operator
if v.isExternal()
when visiting node print(“(’’)
print “(“ before traversing left
inOrder(v.left())

subtree
 print “)“ after traversing right print(v.element())
subtree
if v.isExternal()
 inOrder(v.right())
  print (“)’’)

2  3 b
((2 (a  1))  (3 b))
a 1
© 2010 Goodrich, Tamassia Trees 13
Evaluate Arithmetic Expressions
 Specialization of a postorder Algorithm evalExpr(v)
traversal if v.isExternal()
 recursive method returning return v.element()
the value of a subtree else
 when visiting an internal x  evalExpr(v.left())
node, combine the values
y  evalExpr(v.right())
of the subtrees
  operator stored at v
 return x  y

 

2  3 2

5 1
© 2010 Goodrich, Tamassia Trees 14
Euler Tour Traversal
 Generic traversal of a binary tree
 Includes a 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)

L  R 
B
2  3 2

5 1

© 2010 Goodrich, Tamassia Trees 15


Linked Structure for Trees
 A node is represented by
an object storing B 
 Element
 Parent node
 Sequence of children
nodes
 Node objects implement A  D F 
the Position ADT

A D F

C E
C  E 

© 2010 Goodrich, Tamassia Trees 16


Linked Structure for Binary Trees
 A node is represented by
an object storing 
 Element B
 Parent node
 Left child node
 Right child node
 Node objects implement  
the Position ADT A D

A D    
C E

C E
© 2010 Goodrich, Tamassia Trees 17
Array-Based Representation of Binary
Trees
 Nodes are stored in an array A 1
A

A B D … G H …
2 3
0 1 2 3 10 11 B D

 Node v is stored at A[rank(v)]


 rank(root) = 1 4 5 6 7
E F C J
 if node is the left child of parent(node),
rank(node) = 2  rank(parent(node))
 if node is the right child of parent(node),
10 11
rank(node) = 2  rank(parent(node))  1 G H

© 2010 Goodrich, Tamassia Trees 18

You might also like