What is a Tree? Tree Terminology More Tree Terminology Tree ADT

What is a Tree?
•
•
•
Tree Terminology
•
•
In computer science, a
tree is an abstract model
of a hierarchical
structure
A tree consists of nodes
with a parent-child
relation
Applications:
•
•
•
– Organization charts
– File systems
– Programming
environments
6/1/2005 10:52 AM
Root: node without parent (A)
•
Internal node: node with at least
one child (A, B, C, F)
External node (a.k.a. leaf ): node
without children (E, I, J, K, G, H, D)
Ancestors of a node: parent,
grandparent, grand-grandparent,
etc.
Depth of a node: number of
ancestors
Height of a tree: maximum depth
of any node (3)
Descendant of a node: child,
grandchild, grand-grandchild, etc.
•
•
COSC 2011
1
6/1/2005 10:52 AM
More Tree Terminology
• We use positions to
abstract nodes
• Generic methods:
–
–
–
–
3
2
integer size()
boolean isEmpty()
objectIterator elements()
positionIterator positions()
6/1/2005 10:52 AM
– boolean isInternal(p)
– boolean isExternal(p)
– boolean isRoot(p)
• Update methods:
– swapElements(p, q)
– object replaceElement(p, o)
• Additional update methods
may be defined by data
structures implementing
the Tree ADT
• Java interface: p. 235
COSC 2011
4
Depth
Basic Algorithms on Trees
• Depth of a node v: number of ancestors of v
• Height of a node v:
if v is a leaf node, height(v) = 0
else height(v) = 1 + maximum height of a child of v
Proposition:
height(T) = maximum depth of a leaf node of T
• Preorder traversal
COSC 2011
$
• Query methods:
– position root()
– position parent(p)
– positionIterator children(p)
•6/1/2005
Postorder
traversal
10:52 AM
#
!
COSC 2011
• Accessor methods:
COSC 2011
"
Tree ADT
• Siblings: children of the same parent
• Ordered tree: the children of each node are ordered
• Let n be number of nodes in a tree.
What is the number of edges?
6/1/2005 10:52 AM
Subtree: tree consisting of
a node and its
descendants
5
• Depth of a node v: number of ancestors of v
• Recursive definition:
if v is the root, depth(v) = 0
else depth(v) = 1 + depth(parent(v))
• Algorithm depth(T, v)
if T.isRoot(v)
return(0)
else return (1 + depth(T, T.parent(v))
• Running time: O(1+dv), dv = depth of node v
assuming isRoot(v) and parent(v) take O(1) time
• Worst case running time: an unbalanced tree
O(n), n = total number of nodes in T
•6/1/2005
In practice,
dv << n
10:52 AM
COSC 2011
6
1
Height
• Recursive definition:
if v is a leaf node, height(v) = 0
else height(v) = 1 + maximum height of a child of v
• Proposition: height(T) = max depth of a leaf node of T
• Algorithm height1(T) {
h=0
for every node v in tree T
if (T.isExternal(v))
h = max(h, depth(T, v));
return h;
}6/1/2005 10:52 AM
COSC 2011
Analysis of height()
We now write an algorithm
using the recursive
definition to see if it’s better.
7
6/1/2005 10:52 AM
COSC 2011
Java Code of height1()
•
A traversal visits the nodes of a
tree in a systematic manner
In a preorder traversal, a node is
visited before its descendants
Application: print a structured
document
•
•
Algorithm preOrder(v)
visit(v)
for each child w of v
preorder (w)
(
%
&
%
"
+
()
%
5
1
()
+%* &
6/1/2005 10:52 AM
An Example
2
3
+)
(%
"
•
+)
/%
&
COSC 2011
In a postorder traversal, a
node is visited after its
descendants
Application: compute space
used by files in a directory and
its subdirectories
5
10
Algorithm postOrder(v)
for each child w of v
postOrder (w)
visit(v)
(26
/
,
7
11
6/1/2005 10:52 AM
4
3
6
(
,( )
/$
COSC 2011
4
+)
+%
- .
,
Postorder Traversal
•
6/1/2005 10:52 AM
5
+)
% ,
/
9
'
0
*
()
(%
COSC 2011
8
Preorder Traversal
! " ! #$&%' )(* ! +&%' ,- ! ./%' (' 01 ! 21&%' 34 3&(' 5
6/1/2005 10:52 AM
Algorithm height2(T, v) {
if (T.isExternal(v))
return 0;
else {
h = 0;
for every child w of T
h = max(h, height2(T, w));
return (h + 1);
}
}
height2() takes O(n) time
Analysis of height1()
• depth(T, v) takes time
O(1+dv), or O(n) in the
worst case
• The number of leaf nodes of
T is bound by n–1, or O(n)
• Running time of height1() is
O(n2) is the worst case
+
,(
)8
($
6
)
+$
1
0
)
9*
(:$
COSC 2011
)
9*
+0$
2
)
9*
+:$
12
2
Applications
Computing Disk Space
• Either preorder traversal or postorder traversal can be
used when the order of computation is not important.
Example: printing the contents of a tree (in any order)
• Preorder traversal is required when we must perform a
computation for each node before performing any
computations for its descendents.
• Postorder traversal is needed when the computation for
a node v requires the computations for v’s children to be
done first.
Example: Given a file system, compute the disk space
used by a directory.
6/1/2005 10:52 AM
COSC 2011
13
6/1/2005 10:52 AM
Example of Preorder Traversal
•
Give an O(n)-time algorithm for computing the depths
of all the nodes of a tree T, where n is the number of
nodes of T. Assume that each node v now has an
additional field depth that stores the depth of v.
•
Simple solution: Traversing the tree (preorder or
postorder), call method depth(v) for each node visited
Running time = ?
•
COSC 2011
Algorithm Depths (v, d) {
v.setDepth(d);
for each child w of v
Depths(w, d+1);
}
• Given a tree T, Depths() is called as follows:
Depths(T.root(), 0);
• Running time = ?
15
Example of Postorder Traversal
•
•
•
Give an O(n)-time algorithm for computing the heights
of all the nodes of a tree T, where n is the number of
nodes of T. Assume that each node v now has an
additional field height that stores the height of v.
Simple solution: Traversing the tree (preorder or
postorder), call method height(v) for each node visited.
Running time = ?
Recall the recursive definition of height:
if v is a leaf node, height(v) = 0
else height(v) = 1 + maximum height of a child of v
6/1/2005 10:52 AM
COSC 2011
14
Computing Depths of All Nodes
Recall the recursive definition of depth:
if v is the root, depth(v) = 0
else depth(v) = 1 + depth(parent(v))
6/1/2005 10:52 AM
COSC 2011
17
6/1/2005 10:52 AM
COSC 2011
16
Computing Heights of All Nodes
Algorithm Heights(v) {
h = 0;
for each child w of v
h = max (h, Heights(w));
• Given a tree T, Heights() is
called as follows:
Heights(T.root());
• Running time = ?
if (isExternal(v))
v.setHeight(0);
else
v.setHeight(1+h);
}
6/1/2005 10:52 AM
COSC 2011
18
3