SlideShare a Scribd company logo
Chapter 8
Trees
Dr. Muhammad Hanif Durad
Department of Computer and Information Sciences
Pakistan Institute Engineering and Applied Sciences
hanif@pieas.edu.pk
Some slides have bee adapted with thanks from some other lectures
available on Internet. It made my life easier, as life is always
miserable at PIEAS (Sir Muhammad Yusaf Kakakhil )
Dr. Hanif Durad 2
Lecture Outline (1/2)
 Why Trees
 What is a Tree?
 Rooted Trees
 Tree Terminology
 Ordered Trees/N-ary Trees
 Binary Tree
 Representation of Binary Trees
Dr. Hanif Durad 3
Lecture Outline (2/2)
 Traversal in Binary Trees
 Types of Binary Trees
 Arithmetic Expression Trees
 Extended Binary Trees
 Weighted Binary Trees
 Huffman Trees
 Threaded Binary Trees
 General Trees
 Forest
 Forest Traversals
 Application of Binary Trees
8.1 Why Trees
 Linked Lists provide greater flexibility than arrays
but linear access time of linked lists is prohibitive
 Sacks and queues reflect some hierarchy but they are
limited to one dimension
 To overcome these limitation we create trees
 They are simple data structure for which the running time
of most operations (search, insert, delete) is O(log N)?
Example: UNIX Directory
D:Data StructuresCOMP171 Data Structures and Algorithmbst.ppt
Why Trees?
 Trees are very important data structures in computing.
 They are suitable for:
 Hierarchical structure representation, e.g.,
 File directory.
 Organizational structure of an institution.
 Class inheritance tree.
 Problem representation, e.g.,
 Expression tree.
 Game tree.
 Decision tree.
 Efficient algorithmic solutions, e.g.,
 Search trees.
 Efficient priority queues via heaps.
Trees
 A tree is a collection of nodes
 The collection can be empty
 (recursive definition) If not empty, a tree consists of a
distinguished node r (the root), and zero or more nonempty
subtrees T1, T2, ...., Tk, each of whose roots are connected by a
directed edge from r
DS1,P-262
D:Data StructuresCOMP171 Data Structures and Algorithmbst.ppt
8
What is a Tree? (1/2)
• A tree, is a finite set of nodes together with a finite set of directed
edges that define parent-child relationships. Each directed edge
connects a parent to its child. Example:
Nodes={A,B,C,D,E,F,G,H}
Edges={(A,B),(A,E),(B,F),(B,G),(B,H),
(E,C),(E,D)}
• A directed path from node m1 to node mk is a list of nodes m1, m2, . .
. , mk such that each is the parent of the next node in the list. The
length of such a path is k - 1.
• Example: A, E, C is a directed path of length 2.
A
BE
CD F H G
D:Data StructuresICS202Lecture17.ppt
9
What is a Tree? (2/2)
• A tree satisfies the following properties:
1. It has one designated node, called the root, that has no parent.
2. Every node, except the root, has exactly one parent.
3. A node may have zero or more children.
4. There is a unique directed path from the root to each node.
5
2
4 1 6
3
5
2
4 1 6
3
5
2
4
1
6
3
tree Not a tree Not a tree
Rooted Trees
 A rooted tree is a tree where one of the nodes is
designated as the root node. (Only one root in a
tree)
 A rooted tree has a hierarchical structure: the
root on top, followed by the nodes adjacent to it
right below, followed by the nodes adjacent to
those next, and so on.
D:Data StructuresHanif_SearchTreeslecture9.ppt
Example of a Rooted Tree
1
2
5
3
11
12
10
98
7
64
1
23
10
11
98
4 6
5
7
12
Unrooted tree
Tree rooted with root 1
12
8.2 Tree Terminology (1/5)
• Ordered tree: A tree in which the children of each node are linearly
ordered (usually from left to right).
• Ancestor of a node v: Any node, including v itself, on the path from
the root to the node.
• Proper ancestor of a node v: Any node, excluding v, on the path
from the root to the node.
A
CB
ED F G
Ancestors of Gproper ancestors of E
An Ordered Tree
D:Data StructuresICS202Lecture17.ppt
13
Tree Terminology (2/5)
• Descendant of a node v: Any node, including v itself, on any path
from the node to a leaf node (i.e., a node with no children).
• Proper descendant of a node v: Any node, excluding v, on any
path from the node to a leaf node.
• Subtree of a node v: A tree rooted at a child of v.
Descendants of a node C
A
CB
ED F G
Proper descendants
of node B
A
CB
ED F G
subtrees of node A
14
Tree Terminology (3/5)
AA
B
D
H
C
E
F
G
JI
proper ancestors of node H
proper descendants
of node C
subtrees of A
AA
B
D
H
C
E
F
G
JI
parent of node D
child of node D
grandfather of nodes I,J
grandchildren of node C
15
Tree Terminology (4/5)
• Each of node D and B
has degree 1.
• Each of node A and E
has degree 2.
• Leaf: A node with degree 0.
• Internal or interior node: a node with degree greater than 0.
• Siblings: Nodes that have the same parent.
• Size: The number of nodes in a tree.
AA
B
D
H
C
E
F
G
J
I
• node C has degree 3.
• Each of node F,G,H,I,J
has degree 0.
An Ordered Tree with size of 10
Siblings
of E
Siblings of A
16
Tree Terminology (5/5)
• Level (or depth) of a node v: The length of the path from the root to v.
• Height of a node v: The length of the longest path from v to a leaf
node.
– Height of a node v: The length of the longest path from v to a leaf
node.
– The height of a tree is the height of its root mode.
– By definition the height of an empty tree is -1.
AA
B
D
H
C
E F G
JI
k
Level 0
Level 1
Level 2
Level 3
Level 4
• The height of the tree is 4.
• The height of node C is 3.
17
Ordered Trees/
N-ary Trees
• An N-ary tree is an ordered tree that is either:
1. Empty, or
2. It consists of a root node and at most N non-empty N-ary
subtrees.
• It follows that the degree of each node in an N-ary tree is at most N.
• Example of N-ary trees:
5
72
59 2-ary (binary) tree
B
F
J
DD
C G AEB
3-ary (tertiary)tree
18
Binary Tree
A binary tree is a finite set of elements that is either empty or is partitioned
into three disjoint subsets.
- The first subset contains a single element called the root of the tree.
- The other two subsets are themselves binary trees, called the left and right
subtrees of the original tree. A left or right subtree can be empty.
Each element of a tree is called a node of the tree.
B
G
E
D
IH
F
c
A Binary tree
D:Data StructuresHanif_SearchTreesshahidBinaryTree.ppt
19
Binary Tree
Structures that are not binary trees
B
G
E
D
I
H
F
c
A
20
Binary Tree
Structures that are not binary trees
B
G
E
D
F
c
A
21
Binary Tree
Structures that are not binary trees
B
H
E
G
I
F
c
A
D
22
Binary tree
B
G
E
D
IH
F
C
A
Structure that is not a strictly binary tree:
because nodes C and E have one son each.
23
Binary tree
A
B
A
B
A
B C
GE
I
D
H
F
Complete Binary Tree
Skewed Binary Tree
E
C
D
1
2
3
4
5
D:Data StructuresHanif_SearchTreesTrees.ppt, P-22/47
24
Level and depth of a binary Tree
Level of binary tree:
The root of the tree has level 0. And the level of any other node is one
more than the level of its father.
Depth of a binary tree:
The depth of a binary tree is the maximum level of any leaf in the
tree.
B
D
GF
E
C
A Level 0
Level 1
Level 2
Level 3
Depth is 3.
D:Data StructuresHanif_SearchTreesshahidBinaryTree.ppt
8.4 Representation of Binary Trees
Binary Tree Representations
 If a complete binary tree with n nodes (depth =
log n + 1) is represented sequentially, then for
any node with index i, 1<=i<=n, we have:
 parent(i) is at i/2 if i!=1. If i=1, i is at the root and
has no parent.
 left_child(i) ia at 2i if 2i<=n. If 2i>n, then i has no
left child.
 right_child(i) ia at 2i+1 if 2i +1 <=n. If 2i +1 >n,
then i has no right child.
D:Data StructuresHanif_SearchTrees chapter5.ppt, P-16/88
27
Sequential Representation
A
B
--
C
--
--
--
D
--
.
E
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
.
[16]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
A
B
C
D
E
F
G
H
I
A
B
E
C
D
A
B C
GE
I
D
H
F
(1) waste space
(2) insertion/deletion
problem
Linked List Representation
A
B
C
D E
B
A D
C
Tree structure using Doubly Linked List
Root
D:Data StructuresHanif_SearchTrees unit8.ppt
8.5 Types of Binary Trees
30
Binary Tree
8.6 Strictly binary trees:
If every nonleaf node in a binary tree has nonempty left and
right subtrees, the tree is called a strictly binary tree.
A strictly binary tree with n leaves always contains 2n -1 nodes.
B
D
GF
E
C
A
31
8.7 A complete binary tree
Complete binary tree of depth d is the strictly binary tree all of
whose leaves are at level d.
A complete binary tree of depth d is the binary tree of depth d
that contains exactly 2 l nodes at each level between 0 and d.
The total number of nodes = the sum of the number of nodes at
each level between 0 and d.
= 2 d + 1 - 1
C
A
ONM
F G
KJ
IH
ED
B
L
Level 0
Level 1
Level 2
Level 3
32
8.8 Almost complete binary tree (1/4)
A binary tree of depth d is an almost complete binary tree if:
- 1. A node at level less than d -1 has two sons
- 2. For any node in the tree with a right descendant at level d, node must have
a left son and every left descendant of node is either a leaf at level d or has
two sons.
G
ED
B
A
C
F
The strictly binary tree
is not almost complete,
since it contains leaves
at levels 1, 2, and 3.
Violates condition 1
Level 0
Level 1
Level 2
Level 3
33
Almost complete binary tree (2/4)
A binary tree of depth d is an almost complete binary tree if:
- 1. A node at level less than d -1 has two sons
- 2. For any node in the tree with a right descendant at level d, node must have
a left son and every left descendant of node is either a leaf at level d or has
two sons.
The strictly binary tree is not
almost complete, since A
has a right descendant at
level 3 (J) but also has a left
descendant that is a leaf at
level 2 (E)
Violates condition 2
Satisfies the condition 1,
since every leaf node is
either at level 2 or at level
3.
I
ED
B
A
G
H
C
F
KJ
Level 0
Level 1
Level 2
Level 3
d-1=2
34
Almost complete binary tree (3/4)
A binary tree of depth d is an almost complete binary tree if:
- 1. A node at level less than d -1 has two sons
- 2. For any node in the tree with a right descendant at level d, node must have
a left son and every left descendant of node is either a leaf at level d or has
two sons.
The binary tree is almost complete, Satisfies the condition1, since every
leaf node is either at level 2 or at level 3.
Satisfies the condition 2
I
ED
B
A
G
H
C
F
1
2
3
4 5 6 7
8 9
Level 0
Level 1
Level 2
Level 3
d=3
35
Almost complete binary tree (4/4)
A binary tree of depth d is an almost complete binary tree if:
- 1. A node at level less than d -1 has two sons
- 2. For any node in the tree with a right descendant at level d, node must have
a left son and every left descendant of node is either a leaf at level d or has
two sons.
The binary tree
is almost complete,
Satisfies the condition1, since every
leaf node is either at level 2 or at
level 3.
Satisfies the condition 2
However, the binary tree is not
strictly binary tree, since node E has
a left son but not a right son
I
ED
B
A
G
H
C
F
1
2
3
4 5 6 7
8 9
F 10
Bound on number of nodes in
Almost complete binary tree
 An almost complete binary tree of depth d is
intermediate between the complete binary
tree of depth d-1 that contains 2d-1 nodes
and complete binary tree of depth d , which
contains 2d+1-1 nodes
DS1,P-275
8.9 Traversal in Binary Trees
Tree Traversal
 The process of systematically visiting all the
nodes in a tree and performing some
computation at each node in the tree is called a
tree traversal.
 There are two methods in which to traverse a
tree:
1. Depth-First Traversal.
2. Breadth-First Traversal.
D:Data StructuresICS202Lecture19.ppt
Depth-First Traversal
Each NodeName
Visit the node
Visit the left subtree, if any.
Visit the right subtree, if any.
Preorder
(N-L-R)
Visit the left subtree, if any.
Visit the node
Visit the right subtree, if any.
Inorder
(L-N-R)
Visit the left subtree, if any.
Visit the right subtree, if any.
Visit the node
Postorder
(L-R-N)
40
8.9 Traversing a binary tree
Three methods:
- 1. preorder
- 2. inorder
- 3 postorder
Preorder
1. Visit the root
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
93
4
14
18
15
20
7 16
175
Inorder
1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse the right subtree in inorder
Postorder
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
D:Data StructuresHanif_SearchTreesshahidBinaryTree.ppt
41
Traversing a binary tree
Preorder
1. Visit the root
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
preorder: ABDGCEHIF
Inorder
1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse the right subtree in inorder
inorder: DGBAHEICF
Postorder
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
postorder: GDBHIEFCA
D
B
A
F
C
E
G
IH
42
Traversing a binary tree
Preorder
1. Visit the root
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
preorder: ABCEIFJDGHKL
Inorder
1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse the right subtree in inorder
inorder: EICFJBGDKHLA
Postorder
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
postorder: IEJFCGKLHDBA
C
B
A
L
H
D
K
G
E
I
F
J
Breadth-First Traversal
H
D
B
A C E G I K M O
N
L
JF
OMKIGECANJFBLDH
D:Data StructuresICS202Lecture19.ppt
8.12 Arithmetic Expression Trees
(1/2)
 Binary Tree associated with Arithmetic expression is
called Arithmetic Expression Tree
 All the internal nodes are operators
 All the external nodes are operands
 When Arithmetic Expression tree is traversed in in-
order, the output is In-fix expression.
 When Arithmetic Expression tree is traversed in post-
order, We will obtain Post-fix expression.
Example: Expression Tree for (2 * (a − 1) + (3 * b))
D:Data StructuresHanif_SearchTrees unit8.ppt
Arithmetic Expression Trees
(2/2)
+
* *
2 - 3 b
a 1
In-Order Traversal of Expression Tree
Results In-fix Expression.
2 * ( a – 1 ) + ( 3 * b )
Post-Order Traversal of Expression Tree
Results Post-fix Expression.
2 a 1 – * 3 b * +
To convert In-fix Expression to Post-fix expression, First construct
an expression tree using infix expression and then do tree traversal
in post order.
8.13 Extended Binary Trees
 Path length
 Path length of a node in a tree is defined as the number of
edges that has to be traversed from the node to the root node.
 External node
 The node with zero children is called an external node (or in
other words, an external node is nothing but a leaf node).
 Internal node
 A node with one or more children is called an internal node.
D:Data StructuresHanif_SearchTrees FCS_07_HuffmanTrees.ppt
Extended Binary Trees
External node
 The node with zero children is
called an external node (or in other
words, an external node is nothing
but a leaf node).
N
N
N N
N
N
N N
N
L
L L
L L
L L
L
L L
1
2
3
4
5
6
7
8
9 10
1
2 3
4 5
6 7
8 9
Internal node
 A node with one or more
children is called an internal
node.
Extended Binary Trees
Path length
 Path length of a node in a tree is
defined as the number of edges that
has to be traversed from the node to
the root node
N
N
N N
N
N
N N
N
L
L L
L L
L L
L
L L
1
2
3
4
5
6
7
8
9 10
1
2 3
4 5
6 7
8 9
Path length of N1 = 0
Path length of N9 = 4
Path length of L5 = 4
Path length of L8 = 5
Extended Binary Trees
External path length
 External path length (let it be denoted
as E) of a binary tree is defined as the
sum of all path lengths, summed over
each path from the root node of the
tree to an external node
N
N
N N
N
N
N N
N
L
L L
L L
L L
L
L L
1
2
3
4
5
6
7
8
9 10
1
2 3
4 5
6 7
8 9


EN
i
iPE
1
Pi denotes the path length of
the i-th external node
L1 L2 L3 L4 L5 L6 L7 L8 L9 L10
E = 2 + 3 + 3 + 4 + 4 + 3 + 3 + 5 + 5 + 4 = 36
Extended Binary Trees
Internal path length
 Internal path length (let it be denoted as
I) of a binary tree can be defined
analogously as the sum of path lengths
of all internal nodes in the tree
N
N
N N
N
N
N N
N
L
L L
L L
L L
L
L L
1
2
3
4
5
6
7
8
9 10
1
2 3
4 5
6 7
8 9
Pi denotes the path length of
the i-th internal node



IN
i
iPI
1
N1 N2 N3 N4 N5 N6 N7 N8 N9
I = 0 + 1 + 2 + 1 + 2 + 3 + 2 + 3 + 4 = 18
Extended Binary Trees
 Properties
 In a binary tree with n internal nodes, if I denotes the
internal path length, then external path length, E = I + 2n.
2
)1(
)1(210max


nn
nI


max
0
min 238241210
l
i
i
iI
Extended Binary Trees
Binary tree with I
max minBinary tree with I
Weighted Binary Trees
 A binary tree is called weighted binary tree if a weight is
assigned to each external node.
W W
W W
W W W
1 2
3
4
5 6 7
=2 =5
=1 =7 =6
=3 =4
Weighted Binary Trees
Weighted path length
 The external path length with the
weights are called external
weighted path length (or, simply
weighted path length).
where Wi is a weight of an
external node ni and its path
length is Li , n is the total number
of external nodes
P W L
i
n
i i=
=1

W W
W W
W W W
1 2
3
4
5 6 7
=2 =5
=1 =7 =6
=3 =4
P = W1L1+ W2L2 + W3L3 + W4L4 + W5L5 + W6L6 + W7L7
= 2×2 + 5×2 + 3×4 + 4×4 + 1×3 + 7×3 + 6×3
= 84
Minimum Weighted Binary Trees
 The objective is to construct a weighted binary tree for a
given set of weights of the external nodes, so that the
weighted path length is minimum
2 3 5 9
2
9
5 3
5
9
2 3
(a) T
(b) T (c) T
1
2 3
P(T1) = 38 P(T2) = 44 P(T3) = 34
8.14 Huffman Tree
 A weighted binary tree according to Huffman’s algorithm
 Minimum weighted binary tree
 Huffman’s algorithm
 Suppose, we have to construct a minimum weighted binary tree (Huffman tree)
with n weights W1, W2, . . ., Wn.
 Sort the weights in ascending order.
 Obtain a sub-tree with two minimum weights as the weights of external nodes.
 Include the weighted path length of the sub-tree so obtained into the list of weights.
 Repeat the procedure until the list contains single weight.
Huffman’s Algorithm: Example
 Input
External node: A B C D E F
Weight: 2 5 3 4 1 7
4 5 6* 7
9*
4 5
D B
(c)
1 2 3 4 5 7
3*
1 2
E A
(a)
3* 3 4 5 7
E A
C
6*
3* 3
(b)
1 2
Huffman’s Algorithm: Example
E A
C
F
13*
6*
3*
7
1 2
3
(d)
22*
9* 13*
4 5 6* 7
3*
1 2
E A
C
3
D B F
(e)
6* 7 9* 9* 13*
P = 22 + 9 + 13 + 6 + 3 = 53
Huffman’s Algorithm: Example
22*
9* 13*
4 5 6* 7
3*
1 2
E A
C
3
D B F
9* 13*
P = 22 + 9 + 13 + 6 + 3 = 53
22
8
3 5 7
14
7
1 2 3 4
E A
B F
C D
P = 22 + 8 + 14 + 3 + 7 = 54
A weighted binary tree but not Huffman tree
Huffman tree according to Huffman’s algorithm
Huffman’s Algorithm: Example
 Huffman tree is not necessarily unique for a given set
weights
22
3
7 4
9
3
EA
B
F D
13
6
12
5
C
(b)
22
3
7 4
9
3
E A
BF D
13
6
1 2
5
C
(a)
Application of Huffman Tree:
Huffman Coding
 To obtain an optimal set of codes for symbols S1,
S2, ..., Sn which constitute messages. Each code is
a binary string (combinations of 0’s and 1’s)
which will be used for transmission of messages
 Fixed length coding
 Variable length coding
 Fixed length coding
S1 0000
S2 0001
S3 0010
S4 0011
S5 0100
S6 0101
S7 0110
S8 0111
S9 1000
S10 1001
S11 1010
Application of Huffman Tree:
Huffman Coding
 Variable length coding
S1 1/4 00
S2 3/16 10
S3 1/8 111
S4 1/8 010
S5 1/16 1101
S6 1/16 01110
S7 1/16 01111
S8 1/16 0110
S9 1/32 11001
S10 1/64 110000
S11 1/64 110001
Application of Huffman Tree:
Huffman Coding
Application of Huffman Tree:
Huffman Coding
16
8
4
4 4 1 1
2
4
8
12
S
S
S
S S S S
S
S
S
S
1
2
3
4
5
6 7
8
9
10 11
Huffman tree
S1 16
S2 12
S3 8
S4 8
S5 4
S6 4
S7 4
S8 4
S9 2
S10 1
S11 1
S1 16 00
S2 12 10
S3 8 111
S4 8 010
S5 4 1101
S6 4 01110
S7 4 01111
S8 4 0110
S9 2 11001
S10 1 110000
S11 1 110001
S
S
S
S S S S
S
1
2
4
6 7
8
10 11
Huffman coding
S9
S5
S3
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
11
1
1
1
Application of Huffman Tree:
Huffman Coding
Huffman Coding
Advantages
 The Huffman coding has the minimum average length
 It does not require any end-of-character delimiter although the
binary codes are of variable length codes. This is because of its
unique prefix property
8.15 Threaded Binary Trees (1/2)
 Two many null pointers in current representation
of binary trees
n: number of nodes
number of non-null links: n-1
total links: 2n
null links: 2n-(n-1)=n+1
 Replace these null pointers with some useful
“threads”.
D:Data StructuresHanif_SearchTrees chapter5.ppt
Threaded Binary Trees (2/2)
If ptr->left_child is null,
replace it with a pointer to the node that would be
visited before ptr in an inorder traversal
If ptr->right_child is null,
replace it with a pointer to the node that would be
visited after ptr in an inorder traversal
A Threaded Binary Tree
A
B C
GE
I
D
H
F
root
dangling
dangling
inorder traversal:
H, D, I, B, E, A, F, C, G
Data Structures for Threaded BT
TRUE   FALSE
left_thread left_child data right_child right_thread
FALSE: childTRUE: thread
Memory Representation of A Threaded BT
f f--
f fA
f fCf fB
t tE t tF t tGf fD
t tIt tH
root
Next Node in Threaded BT
threaded_pointer insucc(threaded_pointer tree)
{
threaded_pointer temp;
temp = tree->right_child;
if (!tree->right_thread)
while (!temp->left_thread)
temp = temp->left_child;
return temp;
}
Inorder Traversal of Threaded BT
void tinorder(threaded_pointer tree)
{
/* traverse the threaded binary tree inorder
*/
threaded_pointer temp = tree;
for (;;) {
temp = insucc(temp);
if (temp==tree) break;
printf(“%3c”, temp->data);
}
}
O(n)
Inserting Nodes into Threaded BTs
 Insert child as the right child of node parent
 change parent->right_thread to FALSE
 set child->left_thread and child-
>right_thread to TRUE
 set child->left_child to point to parent
 set child->right_child to parent->right_child
 change parent->right_child to point to child
Examples
Insert a node D as a right child of B.
root
parent
A
B
C D
child
root
parent
A
B
C D child
empty
(1)
(2)
(3)
*Figure 5.24: Insertion of child as a right child of parent in a threaded binary tree (p.217)
nonempty
(1)
(3)
(4)
(2)
77
8.19 General Trees
• A general tree is an ordered tree whose set of nodes cannot be
empty.
• In a general tree, there is no limit to the number of children that a
node can have.
• Representing a general tree by linked lists:
– Each node has a linked list of the subtrees of that node.
D:Data StructuresICS202 Lecture17.ppt
78
General Trees (Contd.)
• An example of a general tree:
D
E
F H
G
J
K L
M
K
Forest
 A forest is a set of n >= 0 disjoint trees
A E G
B C D F H I G
H
I
A
B
C
D
F
E
Forest
D:Data StructuresHanif_SearchTrees chapter5.ppt p-73/88
Transform a forest into a binary tree
 T1, T2, …, Tn: a forest of trees
B(T1, T2, …, Tn): a binary tree
corresponding to this forest
 algorithm
(1) empty, if n = 0
(2) has root equal to root(T1)
has left subtree equal to B(T11,T12,…,T1m)
has right subtree equal to B(T2,T3,…,Tn)
Forest Traversals
 Preorder
 If F is empty, then return
 Visit the root of the first tree of F
 Taverse the subtrees of the first tree in tree preorder
 Traverse the remaining trees of F in preorder
 Inorder
 If F is empty, then return
 Traverse the subtrees of the first tree in tree inorder
 Visit the root of the first tree
 Traverse the remaining trees of F is indorer
82
D
H
A
B
F G
CE
I
J
inorder: EFBGCHIJDA
preorder: ABEFCGDHIJ
A
B C D
E F
G H I J
B
E
F
C
G
D
H
I
J
preorder
83
Application of Binary Trees
• Binary trees have many important uses. Two examples are:
1. Binary decision trees.
• Internal nodes are conditions. Leaf nodes denote decisions.
• Expression Tree
Condition1
Condition2
Condition3
decision1
decision2
decision3
decision4
false
false
false
True
True
True
+
a *
d-
b c
D:Data StructuresICS202Lecture17.ppt

More Related Content

What's hot

Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 
Representation of binary tree in memory
Representation of binary tree in memoryRepresentation of binary tree in memory
Representation of binary tree in memory
Rohini Shinde
 

What's hot (20)

Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Binary tree
Binary treeBinary tree
Binary tree
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Trees
Trees Trees
Trees
 
Discrete Mathematics Tree
Discrete Mathematics  TreeDiscrete Mathematics  Tree
Discrete Mathematics Tree
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Data Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfData Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdf
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Representation of binary tree in memory
Representation of binary tree in memoryRepresentation of binary tree in memory
Representation of binary tree in memory
 
Binary tree
Binary tree Binary tree
Binary tree
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
FORESTS
FORESTSFORESTS
FORESTS
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Trees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsTrees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and Algorithms
 

Similar to Chapter 8 ds

Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
Aakash deep Singhal
 

Similar to Chapter 8 ds (20)

binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxLecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
 
Lec6
Lec6Lec6
Lec6
 
Lecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptxLecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptx
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
DSA IV Unit.pptx
DSA IV Unit.pptxDSA IV Unit.pptx
DSA IV Unit.pptx
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Tree terminology and introduction to binary tree
Tree terminology and introduction to binary treeTree terminology and introduction to binary tree
Tree terminology and introduction to binary tree
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
AD3251-Data Structures Design-Notes-Tree.pdf
AD3251-Data Structures  Design-Notes-Tree.pdfAD3251-Data Structures  Design-Notes-Tree.pdf
AD3251-Data Structures Design-Notes-Tree.pdf
 
Data structure using c module 2
Data structure using c module 2Data structure using c module 2
Data structure using c module 2
 
7.tree
7.tree7.tree
7.tree
 
Trees
TreesTrees
Trees
 
heap sort
 heap sort heap sort
heap sort
 
Tree
TreeTree
Tree
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 

More from Hanif Durad

More from Hanif Durad (20)

Chapter 26 aoa
Chapter 26 aoaChapter 26 aoa
Chapter 26 aoa
 
Chapter 25 aoa
Chapter 25 aoaChapter 25 aoa
Chapter 25 aoa
 
Chapter 24 aoa
Chapter 24 aoaChapter 24 aoa
Chapter 24 aoa
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
 
Chapter 10 ds
Chapter 10 dsChapter 10 ds
Chapter 10 ds
 
Chapter 9 ds
Chapter 9 dsChapter 9 ds
Chapter 9 ds
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Chapter 3 ds
Chapter 3 dsChapter 3 ds
Chapter 3 ds
 
Chapter 2 ds
Chapter 2 dsChapter 2 ds
Chapter 2 ds
 
Chapter 5 pc
Chapter 5 pcChapter 5 pc
Chapter 5 pc
 
Chapter 4 pc
Chapter 4 pcChapter 4 pc
Chapter 4 pc
 
Chapter 3 pc
Chapter 3 pcChapter 3 pc
Chapter 3 pc
 
Chapter 2 pc
Chapter 2 pcChapter 2 pc
Chapter 2 pc
 
Chapter 1 pc
Chapter 1 pcChapter 1 pc
Chapter 1 pc
 
Chapter 6 pc
Chapter 6 pcChapter 6 pc
Chapter 6 pc
 

Recently uploaded

Recently uploaded (20)

How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security Services
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 

Chapter 8 ds

  • 1. Chapter 8 Trees Dr. Muhammad Hanif Durad Department of Computer and Information Sciences Pakistan Institute Engineering and Applied Sciences hanif@pieas.edu.pk Some slides have bee adapted with thanks from some other lectures available on Internet. It made my life easier, as life is always miserable at PIEAS (Sir Muhammad Yusaf Kakakhil )
  • 2. Dr. Hanif Durad 2 Lecture Outline (1/2)  Why Trees  What is a Tree?  Rooted Trees  Tree Terminology  Ordered Trees/N-ary Trees  Binary Tree  Representation of Binary Trees
  • 3. Dr. Hanif Durad 3 Lecture Outline (2/2)  Traversal in Binary Trees  Types of Binary Trees  Arithmetic Expression Trees  Extended Binary Trees  Weighted Binary Trees  Huffman Trees  Threaded Binary Trees  General Trees  Forest  Forest Traversals  Application of Binary Trees
  • 4. 8.1 Why Trees  Linked Lists provide greater flexibility than arrays but linear access time of linked lists is prohibitive  Sacks and queues reflect some hierarchy but they are limited to one dimension  To overcome these limitation we create trees  They are simple data structure for which the running time of most operations (search, insert, delete) is O(log N)?
  • 5. Example: UNIX Directory D:Data StructuresCOMP171 Data Structures and Algorithmbst.ppt
  • 6. Why Trees?  Trees are very important data structures in computing.  They are suitable for:  Hierarchical structure representation, e.g.,  File directory.  Organizational structure of an institution.  Class inheritance tree.  Problem representation, e.g.,  Expression tree.  Game tree.  Decision tree.  Efficient algorithmic solutions, e.g.,  Search trees.  Efficient priority queues via heaps.
  • 7. Trees  A tree is a collection of nodes  The collection can be empty  (recursive definition) If not empty, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk, each of whose roots are connected by a directed edge from r DS1,P-262 D:Data StructuresCOMP171 Data Structures and Algorithmbst.ppt
  • 8. 8 What is a Tree? (1/2) • A tree, is a finite set of nodes together with a finite set of directed edges that define parent-child relationships. Each directed edge connects a parent to its child. Example: Nodes={A,B,C,D,E,F,G,H} Edges={(A,B),(A,E),(B,F),(B,G),(B,H), (E,C),(E,D)} • A directed path from node m1 to node mk is a list of nodes m1, m2, . . . , mk such that each is the parent of the next node in the list. The length of such a path is k - 1. • Example: A, E, C is a directed path of length 2. A BE CD F H G D:Data StructuresICS202Lecture17.ppt
  • 9. 9 What is a Tree? (2/2) • A tree satisfies the following properties: 1. It has one designated node, called the root, that has no parent. 2. Every node, except the root, has exactly one parent. 3. A node may have zero or more children. 4. There is a unique directed path from the root to each node. 5 2 4 1 6 3 5 2 4 1 6 3 5 2 4 1 6 3 tree Not a tree Not a tree
  • 10. Rooted Trees  A rooted tree is a tree where one of the nodes is designated as the root node. (Only one root in a tree)  A rooted tree has a hierarchical structure: the root on top, followed by the nodes adjacent to it right below, followed by the nodes adjacent to those next, and so on. D:Data StructuresHanif_SearchTreeslecture9.ppt
  • 11. Example of a Rooted Tree 1 2 5 3 11 12 10 98 7 64 1 23 10 11 98 4 6 5 7 12 Unrooted tree Tree rooted with root 1
  • 12. 12 8.2 Tree Terminology (1/5) • Ordered tree: A tree in which the children of each node are linearly ordered (usually from left to right). • Ancestor of a node v: Any node, including v itself, on the path from the root to the node. • Proper ancestor of a node v: Any node, excluding v, on the path from the root to the node. A CB ED F G Ancestors of Gproper ancestors of E An Ordered Tree D:Data StructuresICS202Lecture17.ppt
  • 13. 13 Tree Terminology (2/5) • Descendant of a node v: Any node, including v itself, on any path from the node to a leaf node (i.e., a node with no children). • Proper descendant of a node v: Any node, excluding v, on any path from the node to a leaf node. • Subtree of a node v: A tree rooted at a child of v. Descendants of a node C A CB ED F G Proper descendants of node B A CB ED F G subtrees of node A
  • 14. 14 Tree Terminology (3/5) AA B D H C E F G JI proper ancestors of node H proper descendants of node C subtrees of A AA B D H C E F G JI parent of node D child of node D grandfather of nodes I,J grandchildren of node C
  • 15. 15 Tree Terminology (4/5) • Each of node D and B has degree 1. • Each of node A and E has degree 2. • Leaf: A node with degree 0. • Internal or interior node: a node with degree greater than 0. • Siblings: Nodes that have the same parent. • Size: The number of nodes in a tree. AA B D H C E F G J I • node C has degree 3. • Each of node F,G,H,I,J has degree 0. An Ordered Tree with size of 10 Siblings of E Siblings of A
  • 16. 16 Tree Terminology (5/5) • Level (or depth) of a node v: The length of the path from the root to v. • Height of a node v: The length of the longest path from v to a leaf node. – Height of a node v: The length of the longest path from v to a leaf node. – The height of a tree is the height of its root mode. – By definition the height of an empty tree is -1. AA B D H C E F G JI k Level 0 Level 1 Level 2 Level 3 Level 4 • The height of the tree is 4. • The height of node C is 3.
  • 17. 17 Ordered Trees/ N-ary Trees • An N-ary tree is an ordered tree that is either: 1. Empty, or 2. It consists of a root node and at most N non-empty N-ary subtrees. • It follows that the degree of each node in an N-ary tree is at most N. • Example of N-ary trees: 5 72 59 2-ary (binary) tree B F J DD C G AEB 3-ary (tertiary)tree
  • 18. 18 Binary Tree A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. - The first subset contains a single element called the root of the tree. - The other two subsets are themselves binary trees, called the left and right subtrees of the original tree. A left or right subtree can be empty. Each element of a tree is called a node of the tree. B G E D IH F c A Binary tree D:Data StructuresHanif_SearchTreesshahidBinaryTree.ppt
  • 19. 19 Binary Tree Structures that are not binary trees B G E D I H F c A
  • 20. 20 Binary Tree Structures that are not binary trees B G E D F c A
  • 21. 21 Binary Tree Structures that are not binary trees B H E G I F c A D
  • 22. 22 Binary tree B G E D IH F C A Structure that is not a strictly binary tree: because nodes C and E have one son each.
  • 23. 23 Binary tree A B A B A B C GE I D H F Complete Binary Tree Skewed Binary Tree E C D 1 2 3 4 5 D:Data StructuresHanif_SearchTreesTrees.ppt, P-22/47
  • 24. 24 Level and depth of a binary Tree Level of binary tree: The root of the tree has level 0. And the level of any other node is one more than the level of its father. Depth of a binary tree: The depth of a binary tree is the maximum level of any leaf in the tree. B D GF E C A Level 0 Level 1 Level 2 Level 3 Depth is 3. D:Data StructuresHanif_SearchTreesshahidBinaryTree.ppt
  • 25. 8.4 Representation of Binary Trees
  • 26. Binary Tree Representations  If a complete binary tree with n nodes (depth = log n + 1) is represented sequentially, then for any node with index i, 1<=i<=n, we have:  parent(i) is at i/2 if i!=1. If i=1, i is at the root and has no parent.  left_child(i) ia at 2i if 2i<=n. If 2i>n, then i has no left child.  right_child(i) ia at 2i+1 if 2i +1 <=n. If 2i +1 >n, then i has no right child. D:Data StructuresHanif_SearchTrees chapter5.ppt, P-16/88
  • 28. Linked List Representation A B C D E B A D C Tree structure using Doubly Linked List Root D:Data StructuresHanif_SearchTrees unit8.ppt
  • 29. 8.5 Types of Binary Trees
  • 30. 30 Binary Tree 8.6 Strictly binary trees: If every nonleaf node in a binary tree has nonempty left and right subtrees, the tree is called a strictly binary tree. A strictly binary tree with n leaves always contains 2n -1 nodes. B D GF E C A
  • 31. 31 8.7 A complete binary tree Complete binary tree of depth d is the strictly binary tree all of whose leaves are at level d. A complete binary tree of depth d is the binary tree of depth d that contains exactly 2 l nodes at each level between 0 and d. The total number of nodes = the sum of the number of nodes at each level between 0 and d. = 2 d + 1 - 1 C A ONM F G KJ IH ED B L Level 0 Level 1 Level 2 Level 3
  • 32. 32 8.8 Almost complete binary tree (1/4) A binary tree of depth d is an almost complete binary tree if: - 1. A node at level less than d -1 has two sons - 2. For any node in the tree with a right descendant at level d, node must have a left son and every left descendant of node is either a leaf at level d or has two sons. G ED B A C F The strictly binary tree is not almost complete, since it contains leaves at levels 1, 2, and 3. Violates condition 1 Level 0 Level 1 Level 2 Level 3
  • 33. 33 Almost complete binary tree (2/4) A binary tree of depth d is an almost complete binary tree if: - 1. A node at level less than d -1 has two sons - 2. For any node in the tree with a right descendant at level d, node must have a left son and every left descendant of node is either a leaf at level d or has two sons. The strictly binary tree is not almost complete, since A has a right descendant at level 3 (J) but also has a left descendant that is a leaf at level 2 (E) Violates condition 2 Satisfies the condition 1, since every leaf node is either at level 2 or at level 3. I ED B A G H C F KJ Level 0 Level 1 Level 2 Level 3 d-1=2
  • 34. 34 Almost complete binary tree (3/4) A binary tree of depth d is an almost complete binary tree if: - 1. A node at level less than d -1 has two sons - 2. For any node in the tree with a right descendant at level d, node must have a left son and every left descendant of node is either a leaf at level d or has two sons. The binary tree is almost complete, Satisfies the condition1, since every leaf node is either at level 2 or at level 3. Satisfies the condition 2 I ED B A G H C F 1 2 3 4 5 6 7 8 9 Level 0 Level 1 Level 2 Level 3 d=3
  • 35. 35 Almost complete binary tree (4/4) A binary tree of depth d is an almost complete binary tree if: - 1. A node at level less than d -1 has two sons - 2. For any node in the tree with a right descendant at level d, node must have a left son and every left descendant of node is either a leaf at level d or has two sons. The binary tree is almost complete, Satisfies the condition1, since every leaf node is either at level 2 or at level 3. Satisfies the condition 2 However, the binary tree is not strictly binary tree, since node E has a left son but not a right son I ED B A G H C F 1 2 3 4 5 6 7 8 9 F 10
  • 36. Bound on number of nodes in Almost complete binary tree  An almost complete binary tree of depth d is intermediate between the complete binary tree of depth d-1 that contains 2d-1 nodes and complete binary tree of depth d , which contains 2d+1-1 nodes DS1,P-275
  • 37. 8.9 Traversal in Binary Trees
  • 38. Tree Traversal  The process of systematically visiting all the nodes in a tree and performing some computation at each node in the tree is called a tree traversal.  There are two methods in which to traverse a tree: 1. Depth-First Traversal. 2. Breadth-First Traversal. D:Data StructuresICS202Lecture19.ppt
  • 39. Depth-First Traversal Each NodeName Visit the node Visit the left subtree, if any. Visit the right subtree, if any. Preorder (N-L-R) Visit the left subtree, if any. Visit the node Visit the right subtree, if any. Inorder (L-N-R) Visit the left subtree, if any. Visit the right subtree, if any. Visit the node Postorder (L-R-N)
  • 40. 40 8.9 Traversing a binary tree Three methods: - 1. preorder - 2. inorder - 3 postorder Preorder 1. Visit the root 2. Traverse the left subtree in preorder 3. Traverse the right subtree in preorder 93 4 14 18 15 20 7 16 175 Inorder 1. Traverse the left subtree in inorder 2. Visit the root 3. Traverse the right subtree in inorder Postorder 1. Traverse the left subtree in postorder 2. Traverse the right subtree in postorder 3. Visit the root D:Data StructuresHanif_SearchTreesshahidBinaryTree.ppt
  • 41. 41 Traversing a binary tree Preorder 1. Visit the root 2. Traverse the left subtree in preorder 3. Traverse the right subtree in preorder preorder: ABDGCEHIF Inorder 1. Traverse the left subtree in inorder 2. Visit the root 3. Traverse the right subtree in inorder inorder: DGBAHEICF Postorder 1. Traverse the left subtree in postorder 2. Traverse the right subtree in postorder 3. Visit the root postorder: GDBHIEFCA D B A F C E G IH
  • 42. 42 Traversing a binary tree Preorder 1. Visit the root 2. Traverse the left subtree in preorder 3. Traverse the right subtree in preorder preorder: ABCEIFJDGHKL Inorder 1. Traverse the left subtree in inorder 2. Visit the root 3. Traverse the right subtree in inorder inorder: EICFJBGDKHLA Postorder 1. Traverse the left subtree in postorder 2. Traverse the right subtree in postorder 3. Visit the root postorder: IEJFCGKLHDBA C B A L H D K G E I F J
  • 43. Breadth-First Traversal H D B A C E G I K M O N L JF OMKIGECANJFBLDH D:Data StructuresICS202Lecture19.ppt
  • 44. 8.12 Arithmetic Expression Trees (1/2)  Binary Tree associated with Arithmetic expression is called Arithmetic Expression Tree  All the internal nodes are operators  All the external nodes are operands  When Arithmetic Expression tree is traversed in in- order, the output is In-fix expression.  When Arithmetic Expression tree is traversed in post- order, We will obtain Post-fix expression. Example: Expression Tree for (2 * (a − 1) + (3 * b)) D:Data StructuresHanif_SearchTrees unit8.ppt
  • 45. Arithmetic Expression Trees (2/2) + * * 2 - 3 b a 1 In-Order Traversal of Expression Tree Results In-fix Expression. 2 * ( a – 1 ) + ( 3 * b ) Post-Order Traversal of Expression Tree Results Post-fix Expression. 2 a 1 – * 3 b * + To convert In-fix Expression to Post-fix expression, First construct an expression tree using infix expression and then do tree traversal in post order.
  • 46. 8.13 Extended Binary Trees  Path length  Path length of a node in a tree is defined as the number of edges that has to be traversed from the node to the root node.  External node  The node with zero children is called an external node (or in other words, an external node is nothing but a leaf node).  Internal node  A node with one or more children is called an internal node. D:Data StructuresHanif_SearchTrees FCS_07_HuffmanTrees.ppt
  • 47. Extended Binary Trees External node  The node with zero children is called an external node (or in other words, an external node is nothing but a leaf node). N N N N N N N N N L L L L L L L L L L 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 Internal node  A node with one or more children is called an internal node.
  • 48. Extended Binary Trees Path length  Path length of a node in a tree is defined as the number of edges that has to be traversed from the node to the root node N N N N N N N N N L L L L L L L L L L 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 Path length of N1 = 0 Path length of N9 = 4 Path length of L5 = 4 Path length of L8 = 5
  • 49. Extended Binary Trees External path length  External path length (let it be denoted as E) of a binary tree is defined as the sum of all path lengths, summed over each path from the root node of the tree to an external node N N N N N N N N N L L L L L L L L L L 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9   EN i iPE 1 Pi denotes the path length of the i-th external node L1 L2 L3 L4 L5 L6 L7 L8 L9 L10 E = 2 + 3 + 3 + 4 + 4 + 3 + 3 + 5 + 5 + 4 = 36
  • 50. Extended Binary Trees Internal path length  Internal path length (let it be denoted as I) of a binary tree can be defined analogously as the sum of path lengths of all internal nodes in the tree N N N N N N N N N L L L L L L L L L L 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 Pi denotes the path length of the i-th internal node    IN i iPI 1 N1 N2 N3 N4 N5 N6 N7 N8 N9 I = 0 + 1 + 2 + 1 + 2 + 3 + 2 + 3 + 4 = 18
  • 51. Extended Binary Trees  Properties  In a binary tree with n internal nodes, if I denotes the internal path length, then external path length, E = I + 2n. 2 )1( )1(210max   nn nI   max 0 min 238241210 l i i iI
  • 52. Extended Binary Trees Binary tree with I max minBinary tree with I
  • 53. Weighted Binary Trees  A binary tree is called weighted binary tree if a weight is assigned to each external node. W W W W W W W 1 2 3 4 5 6 7 =2 =5 =1 =7 =6 =3 =4
  • 54. Weighted Binary Trees Weighted path length  The external path length with the weights are called external weighted path length (or, simply weighted path length). where Wi is a weight of an external node ni and its path length is Li , n is the total number of external nodes P W L i n i i= =1  W W W W W W W 1 2 3 4 5 6 7 =2 =5 =1 =7 =6 =3 =4 P = W1L1+ W2L2 + W3L3 + W4L4 + W5L5 + W6L6 + W7L7 = 2×2 + 5×2 + 3×4 + 4×4 + 1×3 + 7×3 + 6×3 = 84
  • 55. Minimum Weighted Binary Trees  The objective is to construct a weighted binary tree for a given set of weights of the external nodes, so that the weighted path length is minimum 2 3 5 9 2 9 5 3 5 9 2 3 (a) T (b) T (c) T 1 2 3 P(T1) = 38 P(T2) = 44 P(T3) = 34
  • 56. 8.14 Huffman Tree  A weighted binary tree according to Huffman’s algorithm  Minimum weighted binary tree  Huffman’s algorithm  Suppose, we have to construct a minimum weighted binary tree (Huffman tree) with n weights W1, W2, . . ., Wn.  Sort the weights in ascending order.  Obtain a sub-tree with two minimum weights as the weights of external nodes.  Include the weighted path length of the sub-tree so obtained into the list of weights.  Repeat the procedure until the list contains single weight.
  • 57. Huffman’s Algorithm: Example  Input External node: A B C D E F Weight: 2 5 3 4 1 7 4 5 6* 7 9* 4 5 D B (c) 1 2 3 4 5 7 3* 1 2 E A (a) 3* 3 4 5 7 E A C 6* 3* 3 (b) 1 2
  • 58. Huffman’s Algorithm: Example E A C F 13* 6* 3* 7 1 2 3 (d) 22* 9* 13* 4 5 6* 7 3* 1 2 E A C 3 D B F (e) 6* 7 9* 9* 13* P = 22 + 9 + 13 + 6 + 3 = 53
  • 59. Huffman’s Algorithm: Example 22* 9* 13* 4 5 6* 7 3* 1 2 E A C 3 D B F 9* 13* P = 22 + 9 + 13 + 6 + 3 = 53 22 8 3 5 7 14 7 1 2 3 4 E A B F C D P = 22 + 8 + 14 + 3 + 7 = 54 A weighted binary tree but not Huffman tree Huffman tree according to Huffman’s algorithm
  • 60. Huffman’s Algorithm: Example  Huffman tree is not necessarily unique for a given set weights 22 3 7 4 9 3 EA B F D 13 6 12 5 C (b) 22 3 7 4 9 3 E A BF D 13 6 1 2 5 C (a)
  • 61. Application of Huffman Tree: Huffman Coding  To obtain an optimal set of codes for symbols S1, S2, ..., Sn which constitute messages. Each code is a binary string (combinations of 0’s and 1’s) which will be used for transmission of messages  Fixed length coding  Variable length coding
  • 62.  Fixed length coding S1 0000 S2 0001 S3 0010 S4 0011 S5 0100 S6 0101 S7 0110 S8 0111 S9 1000 S10 1001 S11 1010 Application of Huffman Tree: Huffman Coding
  • 63.  Variable length coding S1 1/4 00 S2 3/16 10 S3 1/8 111 S4 1/8 010 S5 1/16 1101 S6 1/16 01110 S7 1/16 01111 S8 1/16 0110 S9 1/32 11001 S10 1/64 110000 S11 1/64 110001 Application of Huffman Tree: Huffman Coding
  • 64. Application of Huffman Tree: Huffman Coding 16 8 4 4 4 1 1 2 4 8 12 S S S S S S S S S S S 1 2 3 4 5 6 7 8 9 10 11 Huffman tree S1 16 S2 12 S3 8 S4 8 S5 4 S6 4 S7 4 S8 4 S9 2 S10 1 S11 1
  • 65. S1 16 00 S2 12 10 S3 8 111 S4 8 010 S5 4 1101 S6 4 01110 S7 4 01111 S8 4 0110 S9 2 11001 S10 1 110000 S11 1 110001 S S S S S S S S 1 2 4 6 7 8 10 11 Huffman coding S9 S5 S3 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 11 1 1 1 Application of Huffman Tree: Huffman Coding
  • 66. Huffman Coding Advantages  The Huffman coding has the minimum average length  It does not require any end-of-character delimiter although the binary codes are of variable length codes. This is because of its unique prefix property
  • 67. 8.15 Threaded Binary Trees (1/2)  Two many null pointers in current representation of binary trees n: number of nodes number of non-null links: n-1 total links: 2n null links: 2n-(n-1)=n+1  Replace these null pointers with some useful “threads”. D:Data StructuresHanif_SearchTrees chapter5.ppt
  • 68. Threaded Binary Trees (2/2) If ptr->left_child is null, replace it with a pointer to the node that would be visited before ptr in an inorder traversal If ptr->right_child is null, replace it with a pointer to the node that would be visited after ptr in an inorder traversal
  • 69. A Threaded Binary Tree A B C GE I D H F root dangling dangling inorder traversal: H, D, I, B, E, A, F, C, G
  • 70. Data Structures for Threaded BT TRUE   FALSE left_thread left_child data right_child right_thread FALSE: childTRUE: thread
  • 71. Memory Representation of A Threaded BT f f-- f fA f fCf fB t tE t tF t tGf fD t tIt tH root
  • 72. Next Node in Threaded BT threaded_pointer insucc(threaded_pointer tree) { threaded_pointer temp; temp = tree->right_child; if (!tree->right_thread) while (!temp->left_thread) temp = temp->left_child; return temp; }
  • 73. Inorder Traversal of Threaded BT void tinorder(threaded_pointer tree) { /* traverse the threaded binary tree inorder */ threaded_pointer temp = tree; for (;;) { temp = insucc(temp); if (temp==tree) break; printf(“%3c”, temp->data); } } O(n)
  • 74. Inserting Nodes into Threaded BTs  Insert child as the right child of node parent  change parent->right_thread to FALSE  set child->left_thread and child- >right_thread to TRUE  set child->left_child to point to parent  set child->right_child to parent->right_child  change parent->right_child to point to child
  • 75. Examples Insert a node D as a right child of B. root parent A B C D child root parent A B C D child empty (1) (2) (3)
  • 76. *Figure 5.24: Insertion of child as a right child of parent in a threaded binary tree (p.217) nonempty (1) (3) (4) (2)
  • 77. 77 8.19 General Trees • A general tree is an ordered tree whose set of nodes cannot be empty. • In a general tree, there is no limit to the number of children that a node can have. • Representing a general tree by linked lists: – Each node has a linked list of the subtrees of that node. D:Data StructuresICS202 Lecture17.ppt
  • 78. 78 General Trees (Contd.) • An example of a general tree: D E F H G J K L M K
  • 79. Forest  A forest is a set of n >= 0 disjoint trees A E G B C D F H I G H I A B C D F E Forest D:Data StructuresHanif_SearchTrees chapter5.ppt p-73/88
  • 80. Transform a forest into a binary tree  T1, T2, …, Tn: a forest of trees B(T1, T2, …, Tn): a binary tree corresponding to this forest  algorithm (1) empty, if n = 0 (2) has root equal to root(T1) has left subtree equal to B(T11,T12,…,T1m) has right subtree equal to B(T2,T3,…,Tn)
  • 81. Forest Traversals  Preorder  If F is empty, then return  Visit the root of the first tree of F  Taverse the subtrees of the first tree in tree preorder  Traverse the remaining trees of F in preorder  Inorder  If F is empty, then return  Traverse the subtrees of the first tree in tree inorder  Visit the root of the first tree  Traverse the remaining trees of F is indorer
  • 82. 82 D H A B F G CE I J inorder: EFBGCHIJDA preorder: ABEFCGDHIJ A B C D E F G H I J B E F C G D H I J preorder
  • 83. 83 Application of Binary Trees • Binary trees have many important uses. Two examples are: 1. Binary decision trees. • Internal nodes are conditions. Leaf nodes denote decisions. • Expression Tree Condition1 Condition2 Condition3 decision1 decision2 decision3 decision4 false false false True True True + a * d- b c D:Data StructuresICS202Lecture17.ppt