You are on page 1of 17

MCS-021

Qst.1 Write an algorithm for the implementation of a Dequeue.


Ans: Dequeue (a double ended queue) is an abstract data type similar to queue, where addition and deletion of
elements are allowed at both the ends. Like a linear queue and a circular queue, a dequeue can also be
implemented using arrays or linked lists.
Array implementation of a dequeue
If a Dequeue is implemented using arrays, then it will suffer with the same problems that a linear queue had
suffered. Program 5.8 gives the array implementation of a
Dequeue.
#include stdio.h
#define QUEUE_LENGTH 10;
int dq[QUEUE_LENGTH];
int front, rear, choice,x,y;
main()
{
int choice,x;
front = rear = -1; /* initialize the front and rear to null i.e empty queue */
printf (enter 1 for addition and 2 to remove element from the front of the queue");
printf (enter 3 for addition and 4 to remove element from the rear of the queue");
printf(Enter your choice);
scanf(%d,&choice);
switch (choice)
{
case 1:
printf (Enter element to be added :);
scanf(%d,&x);
add_front(x);
break;
case 2:
delete_front();
break;
case 3:
printf (Enter the element to be added :);
scanf(%d ,&x);
add_rear(x);
break;
case 4:
delete_rear();
break;
}
}
/**************** Add at the front ***************/
add_front(int y)
{
if (front == 0)
{

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 1

MCS-021
printf(Element can not be added at the front);
return;
else
{
front = front - 1;
dq[front] = y;
if (front == -1 ) front = 0;
}
}
/**************** Delete from the front ***************/
delete_front()
{
if front == -1
printf(Queue empty);
else Queues
return dq[front];
if (front = = rear)
front = rear = -1
else
front = front + 1;
}
/**************** Add at the rear ***************/
add_rear(int y)
if (front == QUEUE_LENGTH -1 )
{
printf(Element can not be added at the rear )
return;
else
{
rear = rear + 1;
dq[rear] = y;
if (rear = = -1 )
rear = 0;
}
}
/**************** Delete at the rear ***************/
delete_rear()
{
if rear == -1
printf(deletion is not possible from rear);
else
{
if (front = = rear)
front = rear = -1

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 2

MCS-021
else
{ rear = rear 1;
return dq[rear];
}
Linked list implementation of a dequeue
Double ended queues are implemented with doubly linked lists. A doubly link list can traverse in both the
directions as it has two pointers namely left and right. The right pointer points to the next node on the right
where as the left pointer points to the previous node on the left. Program 5.9 gives the linked list
implementation of a Dequeue:.
# include stdio.h
#define NULL 0
struct dq {
int info;
int *left;
int *right;
};
typedef struct dq *dqptr;
dqptr p, tp; dqptr head;
dqptr tail;
main()
{
int choice, I, x;
dqptr n;
dqptr getnode();
printf(\n Enter 1: Start 2 : Add at Front 3 : Add at Rear 4: Delete at Front 5:
Delete at Back);
while (1)
{
printf(\n 1: Start 2 : Add at Front 3 : Add at Back 4: Delete at Front 5: Delete
at Back 6 : exit);
scanf(%d, &choice);
switch (choice)
{
case 1:
create_list();
break;
case 2:
eq_front();
break;
case 3:
eq_back();
break;

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 3

MCS-021
case 4:
dq_front();
break;
case 5:
dq_back();
break;
case 6 :
exit(6);
}
}
}
create_list()
{
int I, x;
dqptr t;
p = getnode();
tp = p;
p->left = getnode();
p->info = 10;
p_right = getnode();
return;
}
dqptr getnode()
{
p = (dqptr) malloc(sizeof(struct dq));
return p;
}
dq_empty(dq q)
{
return q->head = = NULL;
28
} Queues
eq_front(dq q, void *info)
{
if (dq_empty(q))
q->head = q->tail = dcons(info, NULL, NULL);
else
{
q-> head -> left =dcons(info, NULL, NULL);
q->head -> left ->right = q->head;
q ->head = q->head ->left;
}
}
eq_back(dq q, void *info)
{

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 4

MCS-021
if (dq_empty(q))
q->head = q->tail = dcons(info, NULL, NULL)
else
{
q-> tail -> right =dcons(info, NULL, NULL);
q->tail -> right -> left = q->tail;
q ->tail = q->tail ->right;
}
}
dq_front(dq q)
{
if dq is not empty
{
dq tp = q-> head;
void *info = tp -> info;
q ->head = q->head-> right;
free(tp);
if (q->head = = NULL)
q -> tail = NULL;
else
q -> head -> left = NULL;
return info;
}
}
dq_back(dq q)
{
if (q!=NULL)
{
dq tp = q-> tail;
*info = tp -> info;
q ->tail = q->tail-> left;
free(tp);
if (q->tail = = NULL)
q -> head = NULL;
else
q -> tail -> right = NULL;
return info;
}
}

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 5

MCS-021
Qst.2 Implement multiple queues in a single dimensional array. Write algorithms for various queue
operations for them.
Ans: we have seen the representation of a single queue, but many practical applications in computer science
require several queues. Multiqueue is a data structure where multiple queues are maintained. This type of data
structures are used for process scheduling. We may use one dimensional array or multidimensional array to
represent a multiple queue.

Figure: Multiple queues in an array


A multiqueue implementation using a single dimensional array with m elements is depicted in Figure Each
queue has n elements which are mapped to a liner array of m elements.
Array Implementation of a multiqueue
Program gives the program segment using arrays for the addition of an element to a queue in the multiqueue.
addmq(i,x) /* Add x to queue i */
{
int i,x;
++rear[i];
if ( rear[i] == front[i+1])
printf(Queue is full);
else
{
rear[i] = rear[i]+1;
mqueue[rear[i]] = x;
}
}
A multiqueue implementation using a single dimensional array with m elements is depicted in Figure. Each
queue has n elements which are mapped to a liner array of m elements.
Array Implementation of a multiqueue
Program 5.4 gives the program segment using arrays for the addition of an element to
a queue in the multiqueue.
addmq(i,x) /* Add x to queue i */
{

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 6

MCS-021
int i,x;
++rear[i];
if ( rear[i] == front[i+1])
printf(Queue is full);
else
{
rear[i] = rear[i]+1;
mqueue[rear[i]] = x;
}
}
Array implementation of a circular queue
A circular queue can be implemented using arrays or linked lists. Program 5.6 gives
the array implementation of a circular queue.
#include "stdio.h"
void add(int);
void deleteelement(void);
int max=10; /*the maximum limit for queue has been set*/
static int queue[10];
int front=0, rear=-1; /*queue is initially empty*/
void main()
{
int choice,x;
printf ("enter 1 for addition and 2 to remove element front the queue and 3 for exit");
printf("Enter your choice");
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf ("Enter the element to be added :");
scanf("%d",&x);
add(x);
break;
case 2 :
deleteelement();
break;
}
}
void add(int y)
{
if(rear == max-1)
rear = 0;
else
rear = rear + 1;
if( front == rear && queue[front] != NULL)

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 7

MCS-021
printf("Queue Overflow");
23
Else
queue[rear] = y;
}
void deleteelement()
{
int deleted_front = 0;
if (front == NULL)
printf("Error - Queue empty");
else
{
deleted_front = queue[front];
queue[front] = NULL;
if (front == max-1)
front = 0;
else
front = front + 1;
}
}
Qst.3 Write a note of not more than 5 pages summarizing the latest research in the area of Trees.
Refer to various journals and other online resources. Indicate them in your assignment

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 8

MCS-021
Ans:

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 9

MCS-021

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 10

MCS-021

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 11

MCS-021

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 12

MCS-021
Qst.4 What are AVL trees? What are Red-Black trees? What are the differences between them?
Ans: AVL trees: An AVL tree is another balanced binary search tree. Named after their inventors, AdelsonVelskii and Landis, they were the first dynamically balanced trees to be proposed. Like red-black trees, they are
not perfectly balanced, but pairs of sub-trees differ in height by at most 1, maintaining an O(logn) search time.
Addition and deletion operations also take O(logn) time.
Definition of an AVL tree
An AVL tree is a binary search tree which has the following properties:
1. The sub-trees of every node differ in height by at most one.
2. Every sub-tree is an AVL tree.

Balance requirement for an AVL tree: the left and right sub-trees differ by at most 1 in height

Yes
Examination shows that each left sub-tree has a height 1 greater than each right sub-tree.

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 13

MCS-021

No
Sub-tree with root 8 has height 4 and sub-tree with root 18 has height 2
Insertion
As with the red-black tree, insertion is somewhat complex and involves a number of cases. Implementations of
AVL tree insertion may be found in many textbooks: they rely on adding an extra attribute, the balance factor
to each node. This factor indicates whether the tree is left-heavy (the height of the left sub-tree is 1 greater than
the right sub-tree), balanced (both sub-trees are the same height) or right-heavy (the height of the right sub-tree
is 1 greater than the left sub-tree). If the balance would be destroyed by an insertion, a rotation is performed to
correct the balance.

A new item has been added to the left subtree of node 1, causing its height to become 2 greater than 2's right
sub-tree (shown in green). A right-rotation is performed to correct the imbalance.
Red Black Tree: A redblack tree is a type of self-balancing binary search tree, a data structure used
in computer science, typically to implement associative arrays.

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 14

MCS-021
Since it is a balanced tree, it guarantees insertion, search and delete to be O(log n) in time, where n is the total
number of elements in the tree.
A redblack tree is a special type of binary tree, used in computer science to organize pieces of
comparable data, such as text fragments or numbers.
The leaf nodes of redblack trees do not contain data. These leaves need not be explicit in computer memory
a null child pointer can encode the fact that this child is a leafbut it simplifies some algorithms for operating
on redblack trees if the leaves really are explicit nodes. To save memory, sometimes a single sentinel
node performs the role of all leaf nodes; all references from internal to leaf nodes then point to the sentinel
node.
Redblack trees, like all binary search trees, allow efficient in-order traversal (that is: in the order LeftRoot
Right) of their elements. The search-time results from the traversal from root to leaf, and therefore a balanced
tree, having the least possible tree height, results in O(log n) search time.
Properties

An example of a redblack tree


In addition to the requirements imposed on a binary search trees, with redblack trees:
1. A node is either red or black.
2. The root is black. (This rule is sometimes omitted. Since the root can always be changed from red to
black, but not necessarily vice-versa, this rule has little effect on analysis.)
3. All leaves (NIL) are black. (All leaves are same color as the root.)
4. Both children of every red node are black.
5. Every simple path from a given node to any of its descendant leaves contains the same number of black
nodes.

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 15

MCS-021
These constraints enforce a critical property of redblack trees: that the path from the root to the furthest leaf is
no more than twice as long as the path from the root to the nearest leaf. The result is that the tree is roughly
height-balanced. Since operations such as inserting, deleting, and finding values require worst-case time
proportional to the height of the tree, this theoretical upper bound on the height allows redblack trees to be
efficient in the worst case, unlike ordinary binary search trees. Red-black trees are in general not weightbalanced, that is sibling nodes can have hugely differing numbers of descendants.
To see why this is guaranteed, it suffices to consider the effect of properties 4 and 5 together. For a redblack
tree T, let B be the number of black nodes in property 5. Therefore the shortest possible path from the root of T
to any leaf consists of B black nodes. Longer possible paths may be constructed by inserting red nodes.
However, property 4 makes it impossible to insert more than one consecutive red node. Therefore the longest
possible path consists of 2B nodes, alternating black and red.
The shortest possible path has all black nodes, and the longest possible path alternates between red and black
nodes. Since all maximal paths have the same number of black nodes, by property 5, this shows that no path is
more than twice as long as any other path.
In many of the presentations of tree data structures, it is possible for a node to have only one child, and leaf
nodes contain data. It is possible to present redblack trees in this paradigm, but it changes several of the
properties and complicates the algorithms. For this reason, this article uses "null leaves", which contain no data
and merely serve to indicate where the tree ends, as shown above. These nodes are often omitted in drawings,
resulting in a tree that seems to contradict the above principles, but in fact does not. A consequence of this is
that all internal (non-leaf) nodes have two children, although one or both of those children may be null leaves.
Property 5 ensures that a red node must have either two black null leaves or two black non-leaves as children.
For a black node with one null leaf child and one non-null-leaf child, properties 3, 4 and 5 ensure that the nonnull-leaf child must be a red node with two black null leaves as children.
Some explain a redblack tree as a binary search tree whose edges, instead of nodes, are colored in red or black,
but this does not make any difference. The color of a node in this article's terminology corresponds to the color
of the edge connecting the node to its parent, except that the root node is always black (property 2) whereas the
corresponding edge does not exist.

Applications
Redblack trees offer worst-case guarantees for insertion time, deletion time, and search time. Not only does
this make them valuable in time-sensitive applications such as real-time applications, but it makes them
valuable building blocks in other data structures which provide worst-case guarantees; for example, many data
structures used in computational geometry can be based on redblack trees, and the Completely Fair
Scheduler used in current Linux kernels uses redblack trees.

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 16

MCS-021
The AVL tree is another structure supporting O(log n) search, insertion, and removal. It is more rigidly
balanced than redblack trees, leading to slower insertion and removal but faster retrieval. This makes it
attractive for data structures that may be built once and loaded without reconstruction, such as language
dictionaries (or program dictionaries, such as the opcodes of an assembler or interpreter).
Redblack trees are also particularly valuable in functional programming, where they are one of the most
common persistent data structures, used to construct associative arrays and sets which can retain previous
versions after mutations. The persistent version of redblack trees requires O(log n) space for each insertion or
deletion, in addition to time.
For every 2-4 tree, there are corresponding redblack trees with data elements in the same order. The insertion
and deletion operations on 2-4 trees are also equivalent to color-flipping and rotations in redblack trees. This
makes 2-4 trees an important tool for understanding the logic behind redblack trees, and this is why many
introductory algorithm texts introduce 2-4 trees just before redblack trees, even though 2-4 trees are not often
used in practice.
In 2008, Sedgewick introduced a simpler version of the redblack tree called the left-leaning red-black tree by
eliminating a previously unspecified degree of freedom in the implementation. The LLRB maintains an
additional invariant that all red links must lean left except during inserts and deletes. Redblack trees can be
made isometric to either 2-3 trees, or 2-4 trees, for any sequence of operations. The 2-4 tree is ometry was
described in 1978 by Sedgewick. With 2-4 trees, the isometry is resolved by a "color flip," corresponding to a
split, in which the red color of two children nodes leaves the children and moves to the parent node. The tango
tree, a type of tree optimized for fast searches, usually uses redblack trees as part of its data structure.

For More Solutions Contact To Mr. Bilal Ali : Brain Cafe Computer Classes, Near U.P.
Tech. Chowk Lucknow. Contact Number:+91 9984736691,+91 9450148850
E_Mail_Id- Lucknowcomputerclasses@gmail.com, FB Page- facebook.com/bilalali0786 17

You might also like