1.
Presented By:
Susheel Thakur
M.Tech(C.S)
Roll No- 324
2.
Have you ever thought how does the OS
manages or files?
Why do we have a hierarchical file system?
How do files get saved and deleted under
hierarchical directories ?
SOLUTION: A hierarchical data structure
called Trees.
11/13/13 06:38 AM
Data Structures and Algorithms
2
3.
Tree
Nodes
Each node can have 0 or more children
A node can have at most one parent
Binary tree
Tree with 0–2 children per node
11/13/13 06:38 AM
Tree
Data Structures and Algorithms
Binary Tree
3
4.
Terminology
Root ⇒ no parent
Leaf ⇒ no child
Interior ⇒ non-leaf
Height ⇒maximum level of a node
Level ⇒ Length of path from root to node plus 1
Root node
Interior nodes
Height
Leaf nodes
11/13/13 06:38 AM
Data Structures and Algorithms
4
5.
General Tree – organization chart format
Parenthetical Listing
11/13/13 06:38 AM
Data Structures and Algorithms
5
6.
11/13/13 06:38 AM
Data Structures and Algorithms
6
7.
Parenthetical Listing – the algebraic expression,
where each open parenthesis indicates the start
of a new level and each closing parenthesis
completes the current level and moves up one
level in the tree.
11/13/13 06:38 AM
Data Structures and Algorithms
7
8.
A (B (C D) E F (G H I) )
11/13/13 06:38 AM
Data Structures and Algorithms
8
9.
List Representation
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first, followed by a list of sub-trees
data
11/13/13 06:38 AM
link 1
link 2
...
Data Structures and Algorithms
link n
9
10.
Every tree node:
object – useful information
children – pointers to its children nodes
O
O
O
O
O
11/13/13 06:38 AM
Data Structures and Algorithms
10
11.
A binary tree is a tree in which no node can
have more than two subtrees; the maximum
outdegree for a node is two.
In other words, a node can have zero, one, or
two subtrees.
These subtrees are designated as the left
subtree and the right subtree.
11/13/13 06:38 AM
Data Structures and Algorithms
11
12.
11/13/13 06:38 AM
Data Structures and Algorithms
12
13.
A null tree
is a tree
with no
nodes
11/13/13 06:38 AM
Data Structures and Algorithms
13
14.
The height of binary trees can be
mathematically predicted
Given that we need to store N nodes in a
binary tree, the maximum height is
H max = N
A tree with a maximum height is rare. It occurs when all of the nodes in the
entire tree have only one successor.
Data Structures and Algorithms
14
15.
The minimum height of a binary tree is
determined as follows:
H min = [ log 2 N ] + 1
For instance, if there are three nodes to be stored in the binary tree (N=3)
then Hmin=2.
Data Structures and Algorithms
15
16.
Given a height of the binary tree, H, the
minimum number of nodes in the tree is
given as follows:
N min = H
Data Structures and Algorithms
16
17.
Given a height of the binary tree, H, the
maximum number of nodes in the tree is
given as follows:
N max = 2 − 1
H
Data Structures and Algorithms
17
18.
A complete tree has the maximum number of
entries for its height. The maximum number is
reached when the last level is full.
A tree is considered nearly complete if it has
the minimum height for its nodes and all nodes
in the last level are found on the left
11/13/13 06:38 AM
Data Structures and Algorithms
18
19.
11/13/13 06:38 AM
Data Structures and Algorithms
19
20.
11/13/13 06:38 AM
Data Structures and Algorithms
20
21.
Sequential
Representation
A
B
C
D
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
.
[16]
(1) waste space
(2) insertion/deletion
problem
A
B
-C
---D
-.
E
A
B
C
D
E
F
G
H
I
A
B
D
E
11/13/13 06:38 AM
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
H
I
Data Structures and Algorithms
C
E
F
G
21
22.
Linked Representation
typedef struct tnode *ptnode;
typedef struct tnode {
int data;
ptnode left, right;
};
data
left
data
right
left
11/13/13 06:38 AM
Data Structures and Algorithms
right
22
23.
11/13/13 06:38 AM
Data Structures and Algorithms
23
24.
A binary tree traversal requires that each node
of the tree be processed once and only once in a
predetermined sequence.
11/13/13 06:38 AM
Data Structures and Algorithms
24
25.
11/13/13 06:38 AM
Data Structures and Algorithms
25
26.
11/13/13 06:38 AM
Data Structures and Algorithms
26
27.
11/13/13 06:38 AM
Data Structures and Algorithms
27
28.
11/13/13 06:38 AM
Data Structures and Algorithms
28
29.
11/13/13 06:38 AM
Data Structures and Algorithms
29
30.
Preorder Traversal (recursive version)
void preorder(tnode ptr)
/* preorder tree traversal */
{
if (ptr) {
printf(“%d”, ptr->data);
preorder(ptr->left);
predorder(ptr->right);
}
}
11/13/13 06:38 AM
Data Structures and Algorithms
30
31.
11/13/13 06:38 AM
Data Structures and Algorithms
31
32.
11/13/13 06:38 AM
Data Structures and Algorithms
32
33.
Inorder Traversal (recursive version)
void inorder(tnode ptr)
/* inorder tree traversal */
{
if (ptr) {
inorder(ptr->left);
printf(“%d”, ptr->data);
indorder(ptr->right);
}
}
11/13/13 06:38 AM
Data Structures and Algorithms
33
34.
11/13/13 06:38 AM
Data Structures and Algorithms
34
35.
11/13/13 06:38 AM
Data Structures and Algorithms
35
36.
Postorder Traversal (recursive version)
void postorder(tnode ptr)
/* postorder tree traversal */
{
if (ptr) {
postorder(ptr->left);
postdorder(ptr->right);
printf(“%d”, ptr->data);
}
}
11/13/13 06:38 AM
Data Structures and Algorithms
36
37.
Using Pre order and Post order Traversal
Using Post order and In order Traversal
11/13/13 06:38 AM
Data Structures and Algorithms
37
38.
1.
2.
3.
4.
Scan the preorder traversal from left to right.
For each scanned node locate its position in
inorder traversal. Let the scanned node be X.
The nodes preceding X in inorder form its left
subtree and nodes succeeding it form right
subtree.
Repeat step1 for each symbol in the preorder.
11/13/13 06:38 AM
Data Structures and Algorithms
38
39.
Preorder:
ABDHECFG
Inorder:
DHBEAFCG
A
B
D
C
E
F
G
H
11/13/13 06:38 AM
Data Structures and Algorithms
39
40.
1.
2.
3.
Scan the post order traversal from right to left.
For each node scanned locate its position in
order traversal. Let the scanned node be X.
The node preceding X in inorder, form its left
subtree and nodes succeeding it form right
subtree.
11/13/13 06:38 AM
Data Structures and Algorithms
40
41.
Postorder:
HDIEBJFKLGCA
Inorder:
HDBIEAFJCKGL
A
B
D
H
11/13/13 06:38 AM
C
E
I
F
G
J
Data Structures and Algorithms
K
L
41
42.
A variable is an expression.
If x and y are expressions, then ¬x, x∧y,
x∨y are expressions.
Parentheses can be used to alter the normal
order of evaluation (¬ > ∧ > ∨).
Example: x1 ∨ (x2 ∧ ¬x3)
11/13/13 06:38 AM
Data Structures and Algorithms
42
Views
Actions
Embeds 0
Report content