Transcript tree
Trees
A non-linear implementation for
collection classes
Linear structures
Arrays and linked lists are linear:
Obvious first and last elements
Obvious order for traversal
Non-linear structures
Other organizations are possible,
(e.g., file organization on disk is a tree)
c:
\drivers
\documents and settings
\adobe
\program files
\canon
May be more efficient
No obvious first and last elements
No obvious order for traversal
\google
Tree as graph
Connected graph with n
nodes and n-1 edges
-> no cycles
Rooted tree – any root
-directed edges
-one node is root
-all nodes accessible from
root
-root has indegree 0
-leaf nodes have
outdegree 0
-branch nodes have in
and out edges
Relative terminology
Relative to a node
•Parent node
•Ancestors
•Child node
•Descendents
•Sibling node
Measuring location by path length
Depth (from root)
2
Height (above
deepest descendent)
2
Recursive definition of list
Empty list (0 nodes)
OR
Head node and 0 or 1 non-empty sublist
Recursive definition of list
Empty list (0 nodes)
OR
Head node and 0 or 1 non-empty sublist
Recursive definition of tree
•Empty tree (0
nodes)
OR
•Root node and 0 or
more non-empty
subtrees (child node
and its descendents)
Recursive definition of tree
Tree application – file system
Directory structure
is tree rooted at /
(Windows explorer)
A Unix directory
Data Structures & Problem Solving using JAVA/2E
Mark Allen Weiss
© 2002 Addison Wesley
The Unix directory with file counts
Task: count total files
(in parentheses at each node)
Data Structures & Problem Solving using JAVA/2E
Mark Allen Weiss
© 2002 Addison Wesley
The Unix directory with file counts
Task: count total files
(in parentheses at each node)
recursively
Data Structures & Problem Solving using JAVA/2E
Mark Allen Weiss
© 2002 Addison Wesley
Number of files in subtrees
Data Structures & Problem Solving using JAVA/2E
Mark Allen Weiss
© 2002 Addison Wesley
Number of files in subtrees
52
41
17
11
8
12
3
Data Structures & Problem Solving using JAVA/2E
4
Mark Allen Weiss
© 2002 Addison Wesley
Traversal: how to organize activity
on all nodes in a collection?
•
List – process sequentially by
1. iteration
2. recursion
Traversal: how to organize activity
on all nodes in a collection?
•
Tree – no obvious order to process
?
Traversal of trees
Problem: how to visit all
nodes; what order to
visit:
Many traversals for
different purposes:
•Breadth first
•Depth first
•Preorder, postorder
(recursive)
•…more
Preorder Traversal
Recursive definition:
Visit root before children
1. Visit root
2. Visit subtrees
(left to right)
Preorder Traversal
Recursive definition:
Visit root before children
1
2
1. Visit root
2. Visit subtrees
(left to right)
4
3
5
6
7
8
9
11
10
Recursive definition of tree
Postorder Traversal
Recursive definition:
Visit root after children
1. Visit subtrees
(left to right)
2. Visit root
Postorder Traversal
11
Recursive definition:
Visit root after children
2
1. Visit subtrees
(left to right)
2. Visit root
3
1
10
4
8
6
5
9
7
Traversal examples
W
R
I
T
S
F
D
O
C
A
B
X
U
P
E
Implementing trees
• List
– Node – data &
reference
• Tree
– Node – data &
references
class ListNode
{int data;
ListNode next;
}
class TreeNode
{int data;
TreeNode
child1,
child2,
child3;
}
Implementing trees
Problem of
representing a node:
How many edges to
allow for? e.g.,
Class TreeNode
{
int data;
TreeNode
child1,
child2,
child3;
}
Implementing trees
Problem of representing
a node:
Three solutions:
1. allow lots of child
edge references
2. restrict tree structure
to two child edges
3. use first child/next
sibling representation
First child/next sibling
Class TreeNode
{
int data;
TreeNode child,
sibling;
}
Problem:
Makes path to most
nodes longer
Binary Trees
result –
for many purposes,
two references are sufficient
class BNode
binary trees with
{
-left child
int data;
BNode left,
-right child
right;
}