0% found this document useful (0 votes)
11 views40 pages

10-Trees

The document is a presentation on trees, part of the textbook 'Data Structures and Algorithms in Java'. It covers the definition, terminology, and properties of trees, including binary trees, and discusses various tree traversal methods such as preorder, postorder, and inorder. Additionally, it provides insights into the representation of trees in Java and their applications in computer science.

Uploaded by

dosabbbaadr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views40 pages

10-Trees

The document is a presentation on trees, part of the textbook 'Data Structures and Algorithms in Java'. It covers the definition, terminology, and properties of trees, including binary trees, and discusses various tree traversal methods such as preorder, postorder, and inorder. Additionally, it provides insights into the representation of trees in Java and their applications in computer science.

Uploaded by

dosabbbaadr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Presentation for use with the textbook Data Structures

and Algorithms in Java, 6th edition, by M. T. Goodrich, R.


Tamassia, and M. H. Goldwasser, Wiley, 2014

Trees
Mamma
l

Do Pi Ca
g g t

© 2014 Goodrich, Tamassia,


Goldwasser Trees 1
What is a Tree
 In computer science,
Computers”R”U
a tree is an abstract s
model of a
hierarchical structure
Sale Manufacturin R&
 A tree consists of s g D
nodes with a parent-
child relation U Internationa Laptop Desktop
 Applications: S l s s
 Organization charts
 File systems Europ Asi Canad
 Programming e a a
environments

© 2014 Goodrich, Tamassia,


Goldwasser Trees 2
Example: Family Tree

© 2010 Goodrich, Tamassia Trees 3


Representing a Portion of
a File System

© 2010 Goodrich, Tamassia Trees 4


Tree Terminology
 If T is nonempty, it has a
special node, called the
root of T, that has no
parent. A
 Each node v of T different
from the root has a B C D
unique parent node w;
every node with parent w
is a child of w. E F G H

I J K

© 2010 Goodrich, Tamassia Trees 5


Tree Terminology
 A tree can be empty,
meaning that it doesn't
have any nodes.
A
 Recursive tree definition:
tree T is either empty or
consists of a node r, B C D
called the root of T, and a
(possibly empty) set of
E F G H
trees whose roots are the
children of r.
I J K

© 2010 Goodrich, Tamassia Trees 6


Tree Terminology
 Descendant of a node:
child, grandchild, grand-
grandchild, etc.
 Ancestors of a node: A
parent, grandparent,
grand-grandparent, etc.
B C D
 Siblings: two nodes
having the same parent.
 Subtree: tree consisting E F G H
of a node and its
descendants. Subtree
I J K
rooted at C

© 2010 Goodrich, Tamassia Trees 7


Tree Terminology
 Depth of a node:
number of ancestors
 Height of a tree:
A depth
maximum depth of any E dg
e (A 0
node (3). ,D )
 An edge of T is a pair B C D depth
of nodes (u,v) s.t. u is 1

the parent of v
E F G H depth
2

I J K depth
3

© 2010 Goodrich, Tamassia Trees 8


Tree Terminology
 A path in T is a sequence
of nodes s.t. any two
consecutive nodes form an
edge. A
 Internal node: node with at
least one child (A, B, C, F)
B C D
 External node (a.k.a. leaf ):
node without children (E, I,
J, K, G, H, D) E F G H

I J K

© 2010 Goodrich, Tamassia Trees 9


Ordered Trees
 A tree is ordered
if there is a
linear ordering A

defined for the 1


child
st
2nd child 3rd child
B C D
children of each
node. 1 2 child
nd
st
1st 2nd child
child child
E F G H

1st 3rd child


childI J K
2nd child

© 2010 Goodrich, Tamassia Trees 10


Tree ADT
 We use positions to Query methods:
abstract nodes  boolean isInternal(p)
 Generic methods:  boolean isExternal(p)
 integer size()  boolean isRoot(p)
 boolean isEmpty()
 Iterator iterator()
 Iterable positions() Additional update
 Accessor methods: methods may be defined
 position root() by data structures
 position parent(p) implementing the Tree
 Iterable children(p) ADT
 Integer numChildren(p)

© 2014 Goodrich, Tamassia,


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

A D F

C E  
© 2014 Goodrich, Tamassia, C E
Goldwasser Trees 12
Java Interface
Methods for a Tree interface:

© 2014 Goodrich, Tamassia,


Goldwasser Trees 13
Tree Traversal
 A traversal is a systematic way of
visiting the nodes of a tree.
 Common traversals:

1. Preorder: a node is visited before


(prior) its descendants.
2. Postorder: a node is visited after
(post) its descendants.
3. Inorder: a node is visited in between
its descendants (applicable for binary
trees).
© 2010 Goodrich, Tamassia Trees 14
Preorder Traversal
 Runtime requirements?? Algorithm preOrder(v)
 If visit(v) is O(1), then visit(v)
preorder(v) is O(n).
 Application: print a
for each child w of v
structured document preorder (w)

1 Make Money
Fast!

2 5 9
1. 2. Reference
Motivations Methods s
6 7 8
3 4 2.1 2.2 2.3
1.1 1.2
Stock Ponzi Bank
Greed Avidity
Fraud Scheme Robbery
© 2014 Goodrich, Tamassia,
Goldwasser Trees 15
Preorder Traversal
Application: Print a structured
document
Algorithm preOrderPrint(v,indent) Output of

print(newline, indent, v) //visit v first preOrderPrint(root, “”)


//then visit the children Make Money Fast!
for each child w of v 1. Motivations
preorderPrint (w, indent+tab) 1.1 Greed
1.2 Avidity
2. Methods
2.1 Stock Fraud
2.2 Ponzi Scheme
1 Make Money 2.3 Bank Robbery
Fast! References

2 5 9
1. 2. Reference
Motivations Methods s
6 7 8
3 4 2.1 2.2 2.3
1.1 1.2
Stock Ponzi Bank
Greed Avidity
Fraud Scheme Robbery
© 2010 Goodrich, Tamassia Trees 16
Postorder Traversal
 Runtime requirements?? Algorithm postOrder(v)
 If visit(v) is O(1), then for each child w of v
preorder(v) is O(n).
Application: compute postOrder (w)
space used by files in a visit(v)
directory and its
subdirectories 9 cs16
/
8
3 7 todo.tx
homeworks programs
t
/ /
1K
1 2 4 5 6
h1c.do h1nc.do DDR.jav Stocks.jav Robot.jav
c c a a a
3K 2K 10K 25K 20K
© 2014 Goodrich, Tamassia,
Goldwasser Trees 17
Binary Trees
 A binary tree is a tree with
the following properties:
 Each internal node has at
most two children (exactly
A
two for proper binary trees)
 The children of a node are
an ordered pair B C

 We call the children of an


internal node left child and
D E F G

right child left child right


H I child
of E of E
© 2010 Goodrich, Tamassia Trees 18
Binary Trees
 Alternative recursive  Applications:
arithmetic

definition: expressions
 decision processes
a binary tree is either  searching
A
empty or it consists of
a root, together with a
B C
left subtree and a
right subtree, which Right
are both binary trees. D E subtree
F of
G A
Left
subtree of A
H I

© 2010 Goodrich, Tamassia Trees 19


Binary Tree
 A binary tree is proper if each node
has either 0 or 2 children

© 2010 Goodrich, Tamassia Trees 20


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 On expense
coffee? account?
Yes No Yes No

Starbucks Chipotle Gracie’s Café Paragon


© 2014 Goodrich, Tamassia,
Goldwasser Trees 21
Decision Tree

© 2014 Goodrich, Tamassia,


Goldwasser Trees 22
BinaryTree ADT
 The BinaryTree  The above
ADT extends methods return
null when there is
the Tree ADT, no left, right, or
i.e., it inherits sibling of p,
all the methods respectively
of the Tree ADT  Update methods
 Additional may be defined by
data structures
methods: implementing the
 position left(p) BinaryTree ADT
 position
© 2014 Goodrich, Tamassia,
right(p)
Goldwasser Trees 23
Linked Structure for Binary
Trees
 A node is represented
by an object storing 
 Element
 Parent node
 Left child node B
 Right child node
 Node objects
implement the
 
Position ADT
B A D

A D    

C E C E
© 2014 Goodrich, Tamassia,
Goldwasser Trees 24
Arithmetic Expression Tree
 Binary tree associated with an arithmetic
expression
 internal nodes: operators
 external nodes: operands
 left subtree ≡ left operand
 right subtree ≡ right operand
 Example: arithmetic expression tree for
+ the
expression (2  (a - 1) + (3  b))
 

2 - 3 b

© 2014 Goodrich, Tamassia,


a 1
Goldwasser Trees 25
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 – a 1 * 3 b
 Preorder traversal:
+

 

 2 a 1 - * 3 b * +
Postorder traversal:
2 - 3 b

 2 * a – 1 + 3 * b
 Inorder traversal:
a 1
© 2010 Goodrich, Tamassia Trees 26
Useful Facts
 Let T be a binary tree.
 For every k ≥ 0, there are
no more than 2k nodes in
level k.
A level 0:
 Let T be a binary tree n20
with λ levels.
level 1: n21
 Then T has no more than B C
2λ – 1 nodes.
 Let T be a binary tree level 2:
E F G H n22
with N nodes.
 Then the number of levels
is at least log (N + 1). level 3:
I K
n23

27
Properties of Proper Binary
Trees

 #leaves = #internal nodes + 1


Properties:

 #nodes = 2 * #leaves - 1

 height  (#nodes - 1)/2

 #leaves  2height  height  log #leaves


2
 h  log2 (#nodes + 1) - 1

© 2010 Goodrich, Tamassia Trees 28


Properties of Proper Binary
Trees
 Notation Properties:
n number of  e = i + 1
nodes  n = 2e - 1
e number of
 h  i
external nodes
i number of  h  (n - 1)/2
internal nodes  e  2h
h height
 h  log e
2
 h  log2 (n + 1) - 1

© 2014 Goodrich, Tamassia,


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

2 8

1 4 7 9

3 5

© 2014 Goodrich, Tamassia,


Goldwasser Trees 30
Print Arithmetic Expressions
 Specialization of an inorder Algorithm printExpression(v)
traversal
 print operand or operator
if left (v) ≠ null
when visiting node print(“(’’)
print “(“ before traversing
printExpression(left(v))

left subtree
 print “)“ after traversing print(v.element ())
right subtree
if right(v) ≠ null
+ printExpression(right(v))
print (“)’’)
 

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

 

2 - 3 2

5 1
© 2014 Goodrich, Tamassia,
Goldwasser Trees 32
© 2014 Goodrich, Tamassia,
Goldwasser Trees 33
© 2014 Goodrich, Tamassia,
Goldwasser Trees 34
© 2014 Goodrich, Tamassia,
Goldwasser Trees 35
© 2014 Goodrich, Tamassia,
Goldwasser Trees 36
Structure Tree
Implementation
Operation Time
size, isEmpty
O(1)
iterator, positions
O(n)
replace
O(1)
root, parent, children, left, right
O(1)
isInternal, isExternal, isRoot
O(1)

© 2010 Goodrich, Tamassia Trees 37


Array-Based Representation of
Binary Trees
 Nodes are stored in an array A 0
A
A B D F C J G H

0 1 2 3 4 5 6 7 8 9 10

1 2
B D

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


 rank(root) = 0 4 5 6
 if node is the left child of parent(node), F C J

rank(node) = 2  rank(parent(node)) + 1
 if node is the right child of parent(node),
9 10
rank(node) = 2  rank(parent(node)) + 2
G H

© 2010 Goodrich, Tamassia Trees 38


Array vs. Linked
Representation
Array Linked Structure
 Representation
Restricted to binary trees.  Can be used for
 Parent and children generic trees.
 Each position has to
implicitly represented
  Lower memory explicitly store 3
requirements per position references:
 Memory requirements  parent, left child, right

determined by height of child


 Data structure grows
tree (exponential).
 Considerable waste in case as needed – no
of sparse trees wasted space.
 There are efficient
applications (e.g. heap)
Trees 39
Euler Tour Traversal
Algorithm EulerTour(T, v)
perform action for visiting node v on the left
if T.left (v) then
EulerTour(T, T.left)
perform action for visiting node v from below
if T.right (v) then
EulerTour(T, T.right)
perform action for visiting node v from right

Includes a special cases the preorder, postorder and inorder


traversals:
• For preorder  no visiting from below and on the right
• For postorder  no action performed when visiting on the left
and from below
• For inorder  no action performed when visiting on the right
and Trees
on the left 41

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy