ds3 Removed
ds3 Removed
B C
D G H
E
J K
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.
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):
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
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
B C
D G H
E
J K
B D
C
E G H
F
J K
• 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
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
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
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