0% found this document useful (0 votes)
21 views20 pages

ds3 Removed

The document outlines various tasks related to data structures in C, focusing on doubly linked lists, trees, and their representations. It includes functions for inserting and deleting nodes in doubly linked lists, concatenating lists, and traversing binary trees in different orders. Additionally, it discusses threaded binary trees, their advantages and disadvantages, and provides examples of tree representations and traversals.

Uploaded by

gautampl444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

ds3 Removed

The document outlines various tasks related to data structures in C, focusing on doubly linked lists, trees, and their representations. It includes functions for inserting and deleting nodes in doubly linked lists, concatenating lists, and traversing binary trees in different orders. Additionally, it discusses threaded binary trees, their advantages and disadvantages, and provides examples of tree representations and traversals.

Uploaded by

gautampl444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

MODULE III

1. Write a C function to insert_front and delete_front using Doubly Linked List(8)


2. Write a c function for concatenation of two doubly linked list(5)
3. Describe the doubly linked list with advantages and disadvantages. Write a c function to
delete a node from a circular doubly linked list with header node(8)
4. For the given sparse matrix, write the diagrammatic linked list representation(4)
3000
5006
A 0000
4008
0090
5. What is a tree. With suitable example define (10)
a. Binary tree
b. Complete binary tree
c. Level of the binary tree
d. Degree of the tree
e. Strictly binary tree
f. Skewed binary tree
6. Consider the following tree T. Write the in-order, pre-order and post-order traversals for
the tree T along with C functions. Also find the depth of the tree T(10)

B C

D G H
E

J K

7. Give the following traversal, draw a binary tree


a. Inorder:4 2 5 1 6 7 3 8 L
Postorder: 4 5 2 6 7 8 3 1
b. Preorder: A B C E I F J D G H K L
Inorder: E I C F J B G D K H L
8. Represent the below given tree in A
a. Linked list representation
b. Left child right sibling representation B D
C

E G H
F

J K
9. Define Threaded binary tree. List its advantages and disadvantages. Draw the one way
threading and two way threading of the following binary tree (8)
A

B C

D F G
E
G

H
10. Explain threaded binary trees and their representation with a neat diagram. Also develop a
C function to do the in-order traversal of a Threaded binary tree(8)
11. Draw the binary tree for the following expression: ((6+(3-2)*5)^2+3. Traverse the above
generated tree using in-order, pre-order and post-order traversals and also write their
respective functions(10)
12. Define expression tree. For the given tree traverse using in-order, pre-order and post-order
traversals.(7)
+

*
+

B *

C
E

13. Outline the steps involved in construction of expression tree. Construct expression tree for
the following input: AB+C* (10)
14. Write the routines for(10)
a. Copying of Binary trees
b. Testing equality of binary trees
1. Write a C function to insert_front and delete_front using Doubly Linked List(8)
Doubly Linked List
• A doubly linked list or a two-way linked list is a more complex type of linked list which
contains a pointer to the next as well as the previous node in the sequence.
• Each node has three parts
• data – contains data element
• next – contains the address of the next node in the list
• prev - contains the address of the preceding node in the list

Insert Front
struct node *insert_beg(struct node *start)
{
struct node *new_node;
int num;
printf("\n Enter the data : ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
start -> prev = new_node;
new_node -> next = start;
new_node -> prev = NULL;
start = new_node;
return start;
}

Delete Front
struct node *delete_beg(struct node *start)
{
struct node *ptr;
ptr = start;
start = start -> next;
start -> prev = NULL;
free(ptr);
return start;
}

2. Write C function for the concatenation of two doubly linked lists. (5marks)
C Function to concatenate two doubly linked lists
void concatenate()
{
struct node *link;
list = even;
link = list;
while(link->next!= NULL)
{
link = link->next;
}
link->next = odd;
odd->prev = link;
while(link->next!= NULL)
{
link = link->next;
}
list_last = link;
}

3. Describe the doubly linked list with advantages and disadvantages. Write a c function
to delete a node from a circular doubly linked list with header node(8)
Advantages of DLL over SLL A DLL can be traversed in both forward and backward
direction.

ii. The delete operation in DLL is more efficient if pointer to the node to be deleted is given.

iii. In DLL, to delete a node, pointer to the previous node is needed. To get this previous
node, sometimes the list is traversed. In DLL, we can get the previous node using previous
pointer.
Disadvantages of DLL over SLL:
i. Every node of DLL Require extra space for an previous pointer.

ii. All operations require an extra pointer previous to be maintained. For example, in
insertion, we need to modify previous pointers together with next pointers.

Circularly Doubly Linked List with Header node


The circular doubly linked list does not contain NULL in the previous field of the first node
and the next field of the last node. Rather, the next field of the last node stores the address of
the first node of the list, i.e., START. Similarly, the previous field of the first field stores the
address of the last node.

/* Function to delete a node from a circular doubly linked list */


void deleteDLL(int position)
{
if(head==NULL) return;
if(position==1)
{
delete_head();
return;
}
node *current = head;
node *temp;
int count = 0;
do
{
count++;
temp = current;
current = current->next;
} while(current->next != head && count<position-1);
return;
}
printf("Position (%d) does not exist!\n", position);
}

4. For the given sparse matrix, write the diagrammatic linked list representation(4)
3000
5006
A 0000
4008
0090
Sparse Matrix
• A sparse matrix has relative number of elements 0. Representing a sparse matrix using 2-
D array takes substantial amount of space with no use.
• A sparse matrix can be represented by triplets with maximum column of 3 and each row
will have corresponding row number, column number of non-zero elements and non-zero
element itself.
• Those triples can also be represented by a singly linked list having members row, col, val
(non-zero) and address of next node:
• Structure definition:
struct spmlist
{
int row,col,val;
struct spmlist *next;
};
• Triplets in given sparse matrix (row, colum and non-zero element):

Sparse Matrix Triplet


3000 003
5006 105
0000 136
4008 304
0090 338
429

5. What is a tree. With suitable example define (10)


a. Binary tree
b. Complete binary tree
c. Level of the binary tree
d. Degree of the tree
e. Strictly binary tree
f. Skewed binary tree
a. Binary trees
• A binary tree is a data structure that is defined as a collection of elements called nodes.
• In a binary tree, the topmost element is called the root node, and each node has 0, 1, or
at the most 2 children.
• A node that has zero children is called a leaf node or a terminal node.
• Example

b. Complete/Full Binary Trees


o A complete binary tree is a binary tree that satisfies two properties.

First, in a complete binary tree, every level, except possibly the last, is
completely filled.
▪ Second, all nodes appear as far left as possible.
o A Binary tree in which memory is occupied from left to right
o Example

c. Level of a tree
● Every node in the tree is assigned a level number in such a way that
o The root node is at level 0
o Immediate children at level 1 and their immediate children at level 2 and so
on
o If a node is at level n then children will be at level n+1
o Thus, every node is at one level higher than its parent

d. Degree of a Tree
● The degree of a tree is the maximum degree of a node in a given tree
● Example: Degree of a tree is 3

e. Strict Binary Tree


• The tree is said to be strictly binary tree, if every non-leaf node in a binary tree
has non-empty left and right sub trees.
• A strictly binary tree with n leaves always contains 2n–1 nodes.
• Example
f. Skewed Binary Tree
• Left Skewed Binary Tree
o If a binary tree has only left sub trees, then it is called left skewed binary
tree
o Example

• Right Skewed Binary Tree


o If a binary tree has only right sub trees, then it is called right skewed binary
tree
o Example

6. Consider the following tree T. Write the in-order, preorder and post-order traversals
for the tree T along with C functions. Also find the depth of the tree T (10)
A

B C

D G H
E

J K

L
In-order Traversal
• The root of each subtree is visited after its left subtree has been traversed but
before the traversal of its right subtree begins.
• The steps for traversing a binary tree in inorder traversal are:
1. Visit the left subtree, using inorder.
2. Visit the root.
3. Visit the right subtree, using inorder.
C Routine for in-order traversal

void inorder(tree—pointer ptr)


{
if(ptr)
{
inorder(ptr->left—child);
printf("%d", ptr->data) ;
inorder(ptr->right—child);
}
}
Preorder Traversal
• Root node is visited before its left and right subtrees are traversed.
• The steps for traversing a binary tree in preorder traversal are:
1. Visit the root.
2. Visit the left subtree, using preorder.
3. Visit the right subtree, using preorder.
C Routine for pre-order traversal
void preorder(tree—pointer ptr) /* preorder tree traversal
{
if (ptr)
{
Printf(“%d”,ptrdata);
preorder(ptr—>left—child) ;
preorder(ptr—>right—child) ;
}
}
Postorder Traversal
• Root is visited after its left and right subtrees have been traversed.
• The steps for traversing a binary tree in postorder traversal are:
1. Visit the left subtree, using postorder.
2. Visit the right subtree, using postorder
3. Visit the root.
C Routine for post-order traversal
void postorder{tree-pointer ptr)
{
if (ptr)
{
postorder(ptr->left—child);
postorder(ptr->right—child) ;
printf {"%d'',ptr->data) ;
}
}
A

B C

D G H
E

J K

In-order Traversal: DBEAGCLJHK


Pre-order Traversal: ABDECGHJLK
Post-order Traversal: DEBGLJKHCA
Depth of the Tree: Maximum level of any node in the given tree. So the depth of the tree
is 4.

7. Give the following traversal, draw a binary tree


a. Inorder:4 2 5 1 6 7 3 8
Postorder: 4 5 2 6 7 8 3 1
b. Preorder: A B C E I F J D G H K L
Inorder: E I C F J B G D K H L A
8. Represent the below given tree in
a. Linked list representation
b. Left child right sibling representation

B D
C

E G H
F

J K

Linked list representation


• Root node will be represented first followed by the list of subtrees of that node i.e all
child nodes
• Recursively repeat for all subtrees
Left child right sibling representation

• In this representation, list is represented by a node which consists of three fields namely
Data field, Left child reference field and Right sibling reference field.
• Data field stores the actual value of a node, left reference field stores the address of the left
child and right reference field stores the address of the right sibling node
9. Define Threaded binary tree. List its advantages and disadvantages. Draw the one
way threading and two way threading of the following binary tree (8)

B C

D F G
E
G

H
Threaded binary tree
• In linked representation of any binary tree, there are more null links than actual
pointers. Specifically, there are n + 1 null links out of 2 n total links.
• J. Perlis and C. Thornton have devised a clever way to make use of these null links.
They replace the null links by pointers, called threads, to other nodes in the tree.\
• Rules
o If ptr -> left-child is null, replace ptr -> left-child with a pointer inorder
predecessor of ptr.
o If ptr -> right-child is null, replace ptr -> right-child with a pointer to the
inorder successor of ptr.

• Advantages
o Reduce memory wastage
o Faster traversal operations
o Efficient determination of predecessor and successor nodes
o Accessing any node in a tree is efficient
o Backward traversal is possible using double threaded binary tree
o Faster in-order traversal
• Disadvantages
o Complicated insertion and deletion operations
o Additional memory is required for left and right thread variables
One-way Threading

• A one-way threaded tree is also called a single-threaded tree. If the thread appears in the right
field, then the right field will be made to point to the in-order successor of the node
• Example
Two way Threaded Binary tree
• In a two- way threaded tree, also called a double-threaded tree, threads will appear
in both the left and the right field of the node.
• While the left field will point to the in-order predecessor of the node, the right field
will point to its successor.
• A two-way threaded binary tree is also called a fully threaded binary tree.
• Example

10. Explain threaded binary trees and their representation with a neat diagram. Also
develop a C function to do the in-order traversal of a Threaded binary tree(8)
Threaded binary tree
• In linked representation of any binary tree, there are more null links than actual
pointers. Specifically, there are n + 1 null links out of 2 n total links.
• J. Perlis and C. Thornton have devised a clever way to make use of these null links.
They replace the null links by pointers, called threads, to other nodes in the tree.\
• Rules
o If ptr -> left-child is null, replace ptr -> left-child with a pointer inorder
predecessor of ptr.
o If ptr -> right-child is null, replace ptr -> right-child with a pointer to the
inorder successor of ptr
o Node Structure
struct node
{
short int leftThread;
struct node * leftChild;
char data;
thread node *rightChild;
short int rightThread;
};
typedef struct node ThreadNode;

o
o Memory representation of Threaded Binary Tree

In-order Traversal of a Threaded Binary Tree


o By using the threads, we can perform an inorder traversal without making use of a stack.
o For any node, ptr, in a threaded binary tree, if ptr → rightThread = TRUE, the inorder
successor of ptr is ptr → rightChild by definition of the threads.
o Otherwise we obtain the inorder successor of ptr by following a path of left-child links from
the right-child of ptr until we reach a node with leftThread = TRUE.

o Finding the inorder successor of a node: The function insucc finds the inorder successor of
any node in a threaded tree without using a stack.
ThreadNode * insucc(ThreadNode *tree)
{
ThreadNode * temp; temp = tree→rightChild;
if (tree→rightThread==’f’) while (temp→leftThread==’f’)
temp = temp→leftChild; return temp;
}
o Inorder traversal of a threaded binary tree
o To perform an inorder traversal we make repeated calls to insucc
o This function assumes that the tree is pointed to by the header node's left child and that the
header node's right thread is FALSE

void tinorder(threaded_pointer tree)


{
threaded_pointer temp - tree;
for (;;)
{
temp = insucc(temp);
if (temp = tree) break;
printf("%3c", temp->data);
}
}
11. Draw the binary tree for the following expression: ((6+(3-2)*5)^2+3. Traverse the
above generated tree using in-order, pre-order and post-order traversals and also
write their respective functions(10)

12. Define expression tree. For the given tree traverse using in-order, pre-order and
post-order traversals.(7)

*
+

B *

C
E

D
Expression Tree
The Expression tree is a binary tree in which each internal node corresponds to the operator
and each leaf node corresponds to the operand
Example

For the given tree traverse using in-order, pre-order and post-order traversal:
13. Outline the steps involved in construction of expression tree. Construct expression
tree for the following input: AB+C* (10)
Expression Tree
The Expression tree is a binary tree in which each internal node corresponds to the operator
and each leaf node corresponds to the operand
Example

Steps Involved in the Construction of Expression Tree


o Read one symbol at a time from the postfix expression
o Check if the symbol is an operand or operator
o If the symbol is an operand, create a one node tree and push a pointer onto a stack
o If the symbol is an operator, pop two pointers from the stack namely T1 & T2 and form a
new tree with root as the operator, T1 & T2 as a left and right child
o A pointer to this new tree is pushed onto the stack
Thus, an expression is created or constructed by reading the symbols or numbers from the left. If
operand, create a node. If operator, create a tree with operator as root and two pointers to left and
right subtree

Example - Postfix Expression Construction


 The input is: a b + c *
o The first two symbols are operands, we create one-node tree and push a pointer to them onto
the stack.

o Next, read a'+' symbol, so two pointers to tree are popped, a new tree is formed and push a
pointer to it onto the stack.
o Next, 'c' is read, we create one node tree and push a pointer to it onto the stack.

o Finally, the last symbol is read ' * ', we pop two tree pointers and form a new tree with a, ' * '
as root, and a pointer to the final tree remains on the stack

14. Write the routines for(10)


a. Copying of Binary trees
b. Testing equality of binary trees
Copying Binary Trees
Testing Equality of Binary Trees

• Another useful operation is determining the equivalence of two binary trees


• Equivalent binary trees have the same structure and the same information in the
corresponding nodes
• That means every branch in one tree corresponds to a branch in the second tree, that is, the
branching of the two trees is identical
• The function equal uses a modification of preorder traversal to test for equality
• This function returns TRUE if the two trees are equivalent and FALSE if they are not

You might also like