C-Programming-Class 14
C-Programming-Class 14
Trees
1
Session Objectives
2
Session Topics
• Introduction to trees
• Parts of trees
• Binary trees
• In-order Transversal
• Post-order Transversal
• Threaded Binary Tree
3
Definition of a Tree
A directed tree is an acyclic graph, which has only one
node with indegree 0 (root) and all other nodes have
indegree 1.
Indegree the number of edges incident on a node .
Outdegree the number of edges leaving a node.
4
Parts of a Tree
5
Parts of a Tree
nodes
6
Parts of a Tree
parent
node
7
Parts of a Tree
parent child
node nodes
8
Parts of a Tree
parent child
node nodes
9
Parts of a Tree
root node
10
Parts of a Tree
A node that has an outdegree of 0, is
leaf called a Leaf node or Terminal node.
nodes
11
Parts of a Tree
sub-tree
12
Parts of a Tree
sub-tree
13
Parts of a Tree
sub-tree
14
Parts of a Tree
sub-tree
15
Level & Height of a Tree
Level It is number of edges in the path from the root node
Ex: Level of 25 is 2, Level of 10 is 0
Height It is one more than the maximum level in the tree.
Ex: Height of tree shown below is 3.
10
20 15
30 25
16
Binary Tree
A tree in which outdegree of each node is less than or equal to
two is called a Binary tree.
If the outdegree of every node is either 0 or 2, it is called Strictly
Binary Tree.
17
Storage representation of a node
In a linked allocation technique, a node in a tree has three
fields.
infowhich contains the actual information
llinkwhich contains address of the left subtree
rlinkwhich contains address of the right subtree
A node can be represented as a structure as follows
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
18
Various operations performed on Trees
19
Traversals
Traversal is the most common operation that can be
performed on trees.
There are three types of traversals:-
• Preorder
• Inorder
• Postorder
Preorder Traversal
Recursive definition of Preorder traversal
• Process the root Node[N]
• Traverse the Left subtree in preorder[L]
• Traverse the Right subtree in preorder[R]
20
Example: Preorder
A B D G H C E I F
B C
D E F
G H I
21
Function for preorder traversal
22
Inorder Traversal
23
Example: Inorder
G D H B A E I C F
A
B C
D E F
G H I
24
Function for inorder traversal
25
Postorder Traversal
26
Example:Postorder
G H D B I E F C A
B C
D E F
G H I
27
Function for postorder traversal
28
Insertion
29
Insert
Example: 57
43
31 64
20 40 56 89
28 33 47 59
30
Insert
Example: 57
43
31 64
20 40 56 89
28 33 47 59
57
31
Binary Search Tree
32
Binary Search Tree
45
35 64
25 40 56 89
28 32 47 59
33
Insertion
140
100
50 200
25 90 150 300
80 140 180
34
Function to insert an item
NODE insert(int item, NODE root){
NODE temp,cur,prev;
temp = get_node(); /*Obtain a new node*/
temp->info = item;
temp->llink = NULL;
temp->rlink = NULL;
if(root == NULL)
return temp;
prev = NULL; /*find the appropriate position*/
cur = root;
while(cur != NULL)
{
prev = cur;/*Obtain parent and left or right child*/
cur = (item < cur->info) ? Cur->llink :cur->rlink;
}
if(item < prev->info) /*new node is less than parent */
prev->llink = temp; /*Insert towards left */
else
prev->rlink = temp; /*Insert towards right */
return root; /*Return the root*/
}
35
Function to search for an item
NODE search(int item,NODE root)
{
/* Search for the item */
while(root!=NULL && item!=root->info)
{
root=(item<root->info)?
root->llink:root->rlink;
}
return root;
}
36
Deletion
37
Deletion
Two cases:
(1) the node is a leaf
– Delete it immediately
(2) the node has one child
– Adjust a pointer from the parent to bypass that node
38
Threaded Binary Tree
39
Summary
• A directed tree is an acyclic graph, which has only
one node with indegree 0 (root) and all other
nodes have indegree 1.
• A node that has an outdegree of 0, is called a Leaf
node or Terminal node.
• A tree in which outdegree of each node is less than
or equal to two is called a Binary tree.
• If the outdegree of every node is either 0 or 2, it is
called Strictly Binary Tree.
40
Thank You!
41