Documente Academic
Documente Profesional
Documente Cultură
Trees
branches
root
1
9/12/2017
root
leaves
branches
nodes
What is a Tree
• A tree is a finite
nonempty set of
elements. Computers”R”Us
• It is an abstract model
of a hierarchical
structure. Sales Manufacturing R&D
• Consists of nodes with
a parent-child
relation. US International Laptops Desktops
• Applications:
– Organization charts
Europe Asia Canada
– File systems
– Programming
environments
2
9/12/2017
Tree Terminology
• Root: node without parent (A) Subtree: tree consisting of a
• Siblings: nodes share the same parent node and its descendants
• Internal node: node with at least one child
(A, B, C, F)
• External node (leaf ): node without children
(E, I, J, K, G, H, D) A
• Ancestors of a node: parent, grandparent,
grand-grandparent, etc.
• Descendant of a node: child, grandchild,
grand-grandchild, etc. B C D
• Depth of a node: number of ancestors
• Height of a tree: maximum depth of any
node (3)
• Degree of a node: the number of its E F G H
children
• Degree of a tree: the maximum degree of
its node.
I J K
5
subtree
Tree Properties
Property Value
A Number of nodes
Height
C Root Node
B
Leaves
Interior nodes
Ancestors of H
D E F Descendants of B
Siblings of E
Right subtree of A
Degree of this tree
G
H I
3
9/12/2017
Tree Properties
Property Value
A Number of nodes : 9
Height : 4
C Root Node : A
B
Leaves : 5
Interior nodes : 4
Ancestors of H : G, E,B, A
D E F Descendants of B : E, G, H, I
Siblings of E : D, F
Right subtree of A : C
Degree of this tree : 3
G
H I
List Representation
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first, followed by a list of links to sub-trees
4
9/12/2017
Trees
Data
A Tree Representation
• A node is represented by an
object storing ∅
– Element
– Parent node B
– Sequence of children nodes
∅ ∅
A D F
B
A D F
∅ ∅
C E
C E
10
5
9/12/2017
Data
Left Right
Child Sibling A
B C D
E F G H I
J K L
11
12
6
9/12/2017
Tree Traversal
• Two main methods:
– Preorder
– Postorder
• Recursive definition
• Preorder:
– visit the root
– traverse in preorder the children (subtrees)
• Postorder
– traverse in postorder the children (subtrees)
– visit the root
13
Preorder Traversal
• 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
1
Become Rich
2 5 9
1. Motivations 2. Methods 3. Success Stories
3 4 6 7 8
1.1 Enjoy 1.2 Help 2.1 Get a 2.2 Start a 2.3 Acquired
Life Poor Friends CS PhD Web Site by Google
14
7
9/12/2017
Postorder Traversal
• In a postorder traversal, a node is visited after its
descendants
• Application: compute space used by files in a directory and
its subdirectories
9
cs16/
8
3 7
todo.txt
homeworks/ programs/
1K
1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
15
Binary Trees
16
8
9/12/2017
Binary Tree
• A binary tree is a tree with the Applications:
following properties: arithmetic expressions
– Each internal node has at most decision processes
two children (degree of two)
searching
– The children of a node are an
ordered pair
A 1 A
A
B B 2
B C
C
3 D E F G
D
4 H I
E 5
18
9
9/12/2017
A A
B B
19
× ×
2 − 3 b
a 1
20
10
9/12/2017
Decision Tree
• Binary tree associated with a decision process
– internal nodes: questions with yes/no answer
– external nodes: decisions
• Example: dining decision
Yes No
Yes No Yes No
Starbucks Spike’s Al Forno Café Paragon
21
22
11
9/12/2017
ABCDEFG
23
Preorder: FBADCEGIH
24
12
9/12/2017
Preorder: PFBHGSRYTWZ
25
26
13
9/12/2017
Inorder: ABCDEFGHI
27
Inorder: BFGHPRSTWYZ
28
14
9/12/2017
29
Postorder: ACEDBHIGF
30
15
9/12/2017
Postorder: BGHFRWTZYSP
31
× ×
a 1
32
16
9/12/2017
33
34
17
9/12/2017
∅ ∅
B A D
A D ∅ ∅ ∅ ∅
C E
C E
35
36
18
9/12/2017
Algorithm RPREORDER(T)
Input: A binary tree whose root node address is given by a pointer variable T
Output: Preorder traversal of a tree
[Process the root node]
if T != NULL then
write DATA(T)
else
return false {Empty Tree}
[Process the left subtree]
if LPTR(T) != NULL then
RPREORDER(LPTR(T))
[Process the right subtree]
if RPTR(T) != NULL then
RPREORDER(RPTR(T))
[Finished]
return
38
19
9/12/2017
40
20
9/12/2017
41
21
9/12/2017
Algorithm RINORDER(T)
Input: A binary tree whose root node address is given by a pointer variable T
Output: Inorder traversal of a tree
43
22