.
P
i
1
T
1
T
2
.
.
T
j
1
S
1
S
2
. S
k
1
.
.
1 1
.
1 2 2 .
.
2 3 3 .
.
3 .
.
o A priority queue can be represented in memory using two ways.
 Using a single onedimensional array.
 Using a oneway linked list.
o Normally the priority queue is represented using
separate one dimensional queues. Each follows FIFO
behaviour.
ADVANTAGE:
They are useful scheduling of jobs seeking the CPU. All the jobs
needing CPU forms a CPU queue, which is generally known as ready
queue.
29 Royal EducationSYBCA Material
Pi
Tj
S
k
Royal Education Preparation without Parents Blessing is Stack without Top
Q.6 Take types of queue.
I. Simple Queue
II. Circular Queue
III. Dqueu
IV. Priority Queue
Q.7 Why circular queue is more suitable than simple queue?
+ Algorithm of simple queue is wasteful in terms of storage if the
front pointer F never manages to catch up to the rear pointer.
+ An arbitrary large amount of memory would be required to
accommodate the elements.
+ These methods of performing operations on a queue should be
only useful when the queue is emptied at certain intervals.
+ A more suitable method of representing a queue which
prevents an excessive use of memory is to arrange the elements
Q[1],Q[2],.Q[N] in a circular fashion.
+ In other words in a simple queue we face the problem of queue
overflow frequently.
Here it may happen that a simple queue contains only one element
at N
th
position. All other locations (from 1to N1) are empty, but still
we get overflow because of (R=N). If some how, we are able to
utilize the previous (N1) locations, then overflow will only occur if
queue contain total N elements and you will try to insert more
elements and you will try to insert more elements.
+ The above problem can be solved if we treat a simple queue as
a circular queue. i.e. The elements of the queue are arranged in a
circular queue is referred as circular queue.
Q.8 Application of queue.
There are several algorithms that use queues to
solve problems efficiently.
When the jobs are submitted to a networked
printer, they are arranged in order of arrival. Thus; essentially,
jobs sent to a printer are placed on a queue.
Virtually, every reallife lines a queue.
e.g.:lines at ticket counters at cinema halls, railway station, bus
stands, etc. in all these e.g. are on first come first serve basis.
Q.9 Explain circular queues operations threw algorithm.
After reaching at last position of the array, the rear moves
towards the first position of the array, only when there is a vacant
place at the beginning of the array.
+ In Circular queue, the value of rear is incremented
If (rear ==size1)
Rear=0;
Else
Rear++;
30 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
+ Similarly the value of front is incremented as:
If (front ==size1)
front=0;
Else
front++;
TAKE THE PRINT OUTS ON PG:153,154,155
Q.10 Explain Deque, its structure, its operation with algorithm.
A deque (Double ended queue) is a linear list in which insertion and
deletions are made at both ends but not in the middle.
Representation of a deque
A deque is more general then stack or queue.
There are two variations of a deque.
An input restricted and an output restricted deque.
The inputrestricted deque allows insertion at only one end(i.e. at
rear) and deletions occur at both ends.
The output restricted deque allows deletions at only one end(i.e. at
front) and insertions occur at both ends.
There are two types deque.
1) IRD:
In this type of the deque insertion of new element is restricted to only
one end of the list (rear end).But removal of elements can be take
place at both ends.
2) ORD:
In this type of deque removal of the element is restricted to only one
end (front).But insertions of new elements can be take place at both
ends.
Algorithm:
>> Insertion from right end in a deque
DQ:deque
N:elements which is maintained by array
F:front
R:rear
ITEM:It is new data to be inserted
RDQInsert (DQ, F, R, N, ITEM)
Step 1. [Overflow?]
31 Royal EducationSYBCA Material
Deletion
Insertion
Front
Rear
Deletion
Insertion
Royal Education Preparation without Parents Blessing is Stack without Top
If (F==1 and R==N) OR (F==R+1)
Then
Write (OVERFLOW)
Return
Step 2. If(F==0)
Then
F=R=1
Else if(R==N1)
Then
R=1
Else
R=R1
Step 3. DQ[R]=ITEM
Step 4. Return
>> Insertion from left end in a deque
DQ:deque
N:elements which is maintained by array
F:front
R:rear
ITEM:It is new data to be inserted
LDQInsert (DQ, F, R, N, ITEM)
Step 1. [Overflow?]
If (F==1 and R==N) OR (F==R+1)
Then
Write (OVERFLOW)
Return
Step 2. If(F==0)
Then
F=F1
If (F==0)
Then
F=N
Step 3. DQ[F]=ITEM
Step 4. Return
>> Deletion from right end in a deque
DQ:deque
N:elements which is maintained by array
F:front
R:rear
ITEM:It is a variable which is stored a deleted item.
RDQDeletion (DQ, F, R, N, ITEM)
Step 1. [Underflow?]
If (F==0)
Then
Write (UNDERFLOW)
32 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
Return
Step 2. ITEM=DQ[R]
Step 3. If (F==R)
Then
F=R=0
Return
Else
R=R1
If(R=0)
Then
R=N
Step 4. Return
>> Deletion from left end in a deque
DQ:deque
N:elements which is maintained by array
F:front
R:rear
ITEM:It is a variable which is stored a deleted item.
LDQDeletion (DQ, F, R, N, ITEM)
Step 1. [Underflow?]
If (F==0)
Then
Write (UNDERFLOW)
Return
Step 2. ITEM=DQ[F]
Step 3. If (F==R)
Then
F=R=0
Else If (F=N+1)
Then
F=1
Else
F=F1
Step 4. Return
Simulation
One of the areas to which queues can be applied is that of
simulation. Simulation is the process of forming a theoretical
model from a real situation in order to understand the impact
of modifications and effect of introducing various strategies on
the situation.
A simulation program attempts to model a real world
situation in order to learn something about it as it would be
too expensive or dangerous to experiment with the real
system. Areas such as military operations are safer to
simulate than to fieldtest.
33 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
Large number of different situation may be included in a
simulation and their results can be compared. If program
successfully represents real world situation, the results of the
program would also be reflected.
Any process or situation we wish to simulate is considered a
system. A System is a group of objects interacting in order to
produce some result. A Continuous System has parameters
which can take any real value in some given interval.
Discrete Systems have parameters which can only take
values from a fixed number of choices.
In order to simulate a system a model of the system must
be produce & to determine the structure of a model for some
situation, the entities, attributes & activities of the system
should be determine.
An entity represents the components of the system & is the
object that is persons, places or things of interest in the
simulations.
Attributes denote the characteristics of these entities. The
state of the system at any given time is specified by the
attributes of the system entities and the related among the
entities at that time.
An activity is a process that causes a change of system
state & event is the occurrences of an activity at a particular
instant of time.
If we consider a bank system an example entities should be
customer. The attribute for the customer could be the balance
in the account. The activity might include deposits or
withdrawals. An event would occur when a customer enters or
leave the queue.
LINKED LIST
34 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
Below are the algorithm mentioned for all linked list please
refer to most important questions which are mentioned
below.
Write an algorithm to append an element to the end of
doubly link list and delete the last element from the link list
Write an algorithm to delete all occurrences of a given
element in singly linked list
Write an algorithm to insert a node in the single link list
in ascending order
Create an algorithm to merge two single link list into
first(page 47 Summanta)
Algorithm to insert an element in a doubly linked list
Algorithm to delete an element in a doubly linked list
Write a short note on circular link list
Link List
ADVANTAGE OF CIRCULAR LINKED LIST: 
In circular linked list the last node points to the first node so there
is no any node which link portion is null. The advantage of circular
linked list is that suppose we are on the second last node of the
linked list and then we want to traverse to first node than we can
reach to first node second last node to last node and from last node
to first node while in singly linked list it is not possible because last
node points to null.
Q. What is linked list.Explain each type of linked list with its
structure.
A linked list is a linear collection of data elements, called nodes.The
linear order is given by pointers.
1. Singly linked list
2. Doubly linked list
3. Circular Singly linked list
4. Circular Doubly linked list
5. Polynomial linked list
Singly Linked List:
In this linked lists each nodes are divided into two parts and it had one
way node structure.
3 2 4 10
Struct singly
{
Int data;
Struct singly*next;
}*head==NULL;
35 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
Doubly Linked List:
In this linked lists each nodes are divided into three parts and it had
two way node structures.
3 3 7 2 14 1 10 0
Struct doubly
{
Int data;
Struct doubly*next;
Struct doubly*prev;
}*head==NULL;
Circular Singly Linked List:
In this one way circulation can happen and each node has one
pointer but it is circular otherwise it is quite similar with singly linked
list.
3 2 1 10
Struct singly
{
Int data;
Struct singly*next;
}*head==NULL;
Circular Doubly Linked List:
In this one way circulation can happen and each node has one
pointer but it is circular otherwise it is quite similar with singly linked
list.
3 3 7 2 14 1 10 0
Struct doubly
{
Int data;
Struct doubly*next;
Struct doubly*prev;
}*head==NULL;
Polynomial is an important application of linked list is to
accommodate a number of polynomials of growing sizes so that their
combined size does not exceed the total memory available.
36 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
COEFF EXP LINK
Structure of polynomial
Struct poly
{
Int coeff;
Int exp;
Struct poly*next;
}
e.g.: p(x)=3x
2
7x
2
+14x+10
3 3 7 2 14 1 10 0
Q. Application of linked list
To implement other data structures such as stacks, queues, trees and
graphs.
To manipulate polynomials.
To represent sparse matrices.
To implement polynomial manipulation
37 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
TREE
Q.1 State the rules of converting a general tree into binary
tree also convert the given general tree in binary tree.
Ans. A general tree can have any number of nodes. The children of a
node are called Siblings of each other.
To represent the general tree as binary trees, two pointers are
maintained. One pointer points to first child of a node and other
pointer points to the siblings of the node. The structure of the nodes
can be defined as follows:
Struct general
{
int data;
struct general*first child;
struct general*siblings;
};
The inmemory view of a node of the tree is shown in figure:
General
data first sibling
child
While representing a general tree as a binary tree, the pointer to
the first child of a particular node is made the left of that node and
the right child is pointer to the siblings of the node.
Thus while representing a general tree any node has only two
children. Hence, a general tree can be represented as a binary tree.
Q.2 Define binary tree and expression tree.
Ans A binary tree T is a finite set of nodes, such that
(a) T is empty (called empty binary tree) or
(b) T contains a specially designated nodes of T, and remaining
nodes of T form two disjoint binary trees T1 and T2 which are
called left subtree and right subtree respectively.
or
Binary Tree: 
If in a directed tree the outdegree of every node is less than or equal to
2 than the tree is called Binary Tree.
38 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
Sample Binary Tree
Q.3 Difference between Tree and Binary Tree.
Ans.
Tree Binary Tree
A Tree can never be Empty A Binary tree may be empty
A Tree a node may have any
number of Children.
A Binary Tree a node may have
at most two children.
Q.4 Define Expression Tree.
Ans. An Expression tree is a binary tree which stores an arithmetic
expression.
The leaves of an expression tree are operands. Expression tree is
always a
Binary tree because an arithmetic expression contains either
binary
Operator or unary operator (hence a internal nodes has at most
two
Children) following figure show an expression tree for me
arithmetic
Expression.
(A+B*C) ((D*E+F)/G)
TREE TRAVERSAL OPERATION: 
Traversal operation is that operation by which each node in the tree
is processed exactly one in a systematic manner.
There are three main ways of traversing a binary tree:
1. Preorder Traversal: 
In preorder traversal each node is processed in following manner:
I. Process the root node.
II. Traverse the left subtree in preorder.
III. Traverse the right subtree in preorder.
2. Inorder Traversal: 
The inorder traversal of a binary tree is given by the following
manner:
I. Traverse the left subtree in inorder.
II. Process the root node.
III. Traverse the right subtree in inorder.
3. Postorder Traversal: 
The Postorder Traversal of a binary tree as follow:
I. Traverse the left subtree in postorder.
II. Traverse the right subtree in postorder
III. Process the root node.
39 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
Q.5 Write the applications of Tree.
Ans. The presentation contains three applications of trees. The first
describes methods for the mechanical manipulation of arithmetic
expressions.
An interesting application of trees concerning the construction
and
Maintenance of symbol tables.
The tree also plays an important role n the areas of syntax analysis,
where it is used to display the structure of Sentence in a language
and is used in defining unambiguous languages.
Q.6 Advantages & Disadvantages of Threaded Binary Tree.
Ans.
Advantages:
The traversal operation is faster than that of its unthreaded
version, because with threaded binary tree nonrecursive
implementation is possible which can run faster and does not
require the botheration stack management.
The second advantage is more suitable with a threaded binary
tree; we can efficiently determine the predecessor and successor
nodes starting from any node. In case of unthreaded binary tree,
however, this task is more time consuming and difficult. For this
case, a stack is required to provide upward pointing information in
the tree whereas in a threaded binary tree, without having to incur
the overhead of using a stack mechanism the same can be carried
out with the threads.
Any node can be accessible from any other node. Threads are
usually more to upward whereas links are downward. Thus in a
threaded tree, one can move in either direction and nodes are in
fact circularly linked. This is not possible in unthreaded counter
part because there we can move only in down ward direction
starting from root.
Insertions into and deletions from a threaded tree are although
time consuming operations (since we have to manipulate both
links and threads) but these are very easy to implement.
Q.7 Enlist types of tree. Explain any two of them with diagram.
Ans. Types of binary trees.
There are several types of binary tree possible each with its own
properties. Few important & frequently used trees are as below:
1) Expression Tree.
2) Binary Tree.
3) Heap Tree.
4) Threaded Binary Tree.
5) Huffman Tree.
6) Height Balanced Tree(also know an AVL Tree)
7) Decision Tree.
40 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
Binary Search Tree:
A binary tree T is termed as binary search tree (or binary sorted
tree) if each node N of T satisfies the following property:
The value at N is greater than every value in the left subtree of N
and is less than every value in the right Subtree of N. figure shows
two binary search trees for two different types of data.
Q.8 Write an algorithm to delete a node from ordered binary
tree.
Ans. Case 1; N is the leave node
Case 2: N has exactly one child
Case 3: N has two children.
Algorithm BST_DELETE (ITEM)
Input: ITEM is the data of the node to be deleted.
Output: if the node with data as ITEM exist it is deleted else a
message.
Data Structure: Linked structure of binary tree. Pointer to the root
node is ROOT.
Steps:
1. ptr = ROOT, flag = FALSE
2.while (ptr != NULL) and (flag = FALSE) do // step to find the
location of the node
1. case : ITEM < ptr.DATA
1. parent =ptr
2. ptr = ptr.LCHILD
2. case: ITEM > ptr.DATA
1. parent = ptr
2. ptr = ptr.RCHILD
3. case: ptr.DATA = ITEM
1. flag = TRUE
4. End Case
3. End While
4. If (flag = FALSE) then //when node does not exist
1. Print Item does not exist: No deletion
2. Exit. // Quit the execution.
5. End if
/* DECIDE THE CASE OF DELETION */
1 if (ptr.LCHILD =NULL) and (ptr.RCHILD = NULL) then
// node has no child
1. case =1
2 Else
1. if (ptr.LCHILD != NULL) and (ptr.RCHILD !=) then
// node contains both child.
41 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
1. case = 3
2. else
1. case = 2 // node contains only one
child
3. End if
3. End if
/* DELETION: CASE 1 */
1 if (case = 1) then
1. If (parent. CHILD = ptr) then // if the node is a left child
1. parent. CHILD = NULL // set pointer of its parent
2. Else
1. parent. CHILD = NULL
3. End IF
4. Return NODE (ptr) // return deleted node to the memory
bank
5. EndIf
/* DELETION: CASE2 */
11. if(case = 2) then
1. if (parent.LCHILD = ptr) then
1. If (ptr.LCHILD = NULL) then
1. Parent.LCHILD = ptr.RCHILD
2. Else
1. Parent.LCHILD = ptr.LCHILD
3. End If
2. Else
1. If (parent.RCHILD = ptr) then
1. If (ptr.LCHILD = NULL) then
1. Parent.RCHILD = ptr.RCHILD
2. Else
1. Parent.RCHILD = ptr.LCHILD
3. End if
2. End if
3. EndIf
4. RETURN_NODE (ptr)
12. End IF
13. If (case = 3)
1. ptr1 = SUCC(ptr)
2. item1 = ptr1.DATA
3. BST_DELETE (item1)
4. ptr.DATA = ITEM
14. EndIf
15. Stop
Q.9 Define AVL Tree.
42 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
Ans. An AVL tree is a binary tree in which the left and right sub trees of
any Node may differ in height by at most 1, and in which both the sub
trees are Them selves AVL Tree.
Q.10 Define B Tree.
Ans. Btree is a multiway search tree of order n that satisfies the
following
Conditions:
(a) All the nonleaf nodes (expect the root node) have at least n/2
children and at the most n children.
(b) The nonleaf root node may have at the most n nonempty child
and at least two child nodes.
(c) A Btree can exist with only one node i.e. the root node
containing no child.
(d) If a node has n children then it must have n1 values. All the
values of a particular node are in increasing order.
(e) All the values that appear on the left most child of a node are
smaller than the first value of that node. All the values that appear
on the right most child of a node are greater than the last value of
that node.
(f) If x and y are any two i
th
and (i+1)
th
values of a node, where x
<y, then all the values appearing on the (i+1)
th
subtree of that
node are greater than x and less than y.
(g) All the leaf nodes should appear on the same level.
Q.11 Write recursive algorithms for pre order and post order
traversals.
Ans Algorithm PREORDER (ROOT)
Input: ROOT is the pointer to the root node of the binary tree.
Output: Visiting of all the nodes in preorder fashion.
Data Structure: Linked structure of binary tree.
Steps:
1. ptr = ROOT //start from the ROOT
2. If (ptr! = NULL) then //if it is not an empty node
1. VISIT (ptr) //visit the node
2. PREORDER(ptr.LC) //traverse the left subtree of the
node in preorder
3. PREORDER(ptr.RC) //traverse the right subtree of
the the node in preorder
3. EndIf
4. Stop
Algorithm POSTORDER (ROOT)
Input: ROOT is the pointer to the root node of the binary tree.
Output: Visiting of all the nodes in preorder fashion.
Data structure: Linked structure of binary tree.
Steps:
43 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top
1. ptr = ROOT //Start from the root.
2. If (ptr! = NULL) then //if it is not an empty node.
1. POSTORDER (ptr.LC)
//Traverse the left subtree of the node in inorder.
2. POSTORDER(ptr.RC)
//Traverse the right subtree of the node in
inorder.
3. VISIT (ptr) //visit the node.
3. EndIf
4. Stop
Q.12 Write non recursive algorithms for pre order and post
order traversals.
Algorithm preorder_stack (root)
Input: a binary tree represented with three parallel arrays, root node is
being identified by the root.
Output: visiting all the nodes in the tree in preorder fashion.
Data Structure: a Stack info and three parallel arrays representation of
Binary tree.
Steps:
1. PUSH (ROOT) // push the root nodes location
into the stack INFO.
2. While (TOP! =0) do //till the stack is not empty
do
1. ptr = POP() //get the top element in the stack
2. If (ptr! = NULL) then
1. VISIT (DATA [ptr]) //to visit the current node
2. PUSH (RCHILD [ptr]) //next left child of the current
node will be visited.
3. PUSH (LCHILD [ptr])
3. EndIf
3. End While
4. Stop
Algorithm INORDER_STACK (ROOT)
Input: A binary tree represented with tree parallel arrays, root node is
Being identified by ROOT.
Output: Visiting all the nodes in the tree in inorder fashion.
Data Structure: A stack INFO and three parallel arrays representation
of binary tree.
Steps:
1. ptr = ROOT
2. While (TOP! = 0) or (ptr! = NULL) do
1. If (ptr! = NULL) then
1. PUSH (ptr)
2. ptr = LCHILD [ptr]
44 Royal EducationSYBCA Material
33
Royal Education Preparation without Parents Blessing is Stack without Top
2. Else
1. ptr = POP( )
2. VISIT (DATA [ptr])
3. ptr = RCHILD [ptr]
3. EndIf
3. End While
4. Stop
Q.13 what is binary search tree?
A binary tree T is termed as binary search tree(or binary sorted
tree) if each node N of satisfies the following property
The value of N is greater than every value in the left sub tree of N
and is less than every value in the right subtree of N.
Explain following terms.
Height of tree: Maximum number of nodes that is possible in a path
starting from root node to a leaf node is called the height of a tree.
Degree of a tree: Maximum number of children that is possible for a
node is called the degree of the tree
Full binary tree: A binary is a full binary tree if it contains maximum
possible number of nodes in all levels
Complete binary tree is said to be complete if all its levels accepts
the possibly last level have the maximum number of possible nodes
and all the nodes at the last level appear as far as possible.
Forest: Forest is defined as collection of disjoint tree as shown in the
figure below
If we delete root node and all its branches connected with
nodes at level 1 then we get a set of disjoint trees .The set of disjoint
trees is called a forest.
Ordered Tree: If in a directed tree an ordering of the nodes at
each level is given than such a tree is called an Ordered Tree.
Complete mary Tree OR Full mary Tree: 
In a directed tree if the outdegree of every node is exactly equal to m
or 0 than the tree is called a Complete or Full mary Tree.
Level of Tree: 
The level of any node is the length of its path from the root.
mary Tree: 
If in a directed tree the outdegree of every node is less than or equal to
m then the tree is called an mary tree.
45 Royal EducationSYBCA Material
33
Royal Education Preparation without Parents Blessing is Stack without Top
Q.14 What is AVL tree? Discuss its four cases of rotation.
AVL Tree
Def: A binary search tree is said to be height balanced binary search
tree if all its nodes have a balanced factor of 1, 0, and 1 that is,
bf = h
L
 h
R
 <= 1,
For every node in the tree.
(a) Height balanced (b) height
unbalanced
AVL Rotations
In order to balance a tree, one elegant method devised in 1962 by
two Russian mathematicians, G.M.AdelsonVelskii and E.M. Lendis,
and the method is named as AVL rotation in their honour.
There are four cases of rotations possible which is discussed as
below:
Case 1: unbalance occurred due to the insertion in the left subtree of
the left child of the pivot node.
right subtree (A
R
) of left child (A) of pivot node (P) becomes
the left subtree of P.
P becomes the right child of A
Left subtree (A
L
) of A remains the same
46 Royal EducationSYBCA Material
6
8 2
1 4
33
7
7
1
1
0
0
1 0
1
2
1
0
0
0
0
0
6
8 2
1 4
3
6
6
1
Royal Education Preparation without Parents Blessing is Stack without Top
This case is called LefttoLeft insertion.
(a) a height balanced tree as bf <= 1
2
C
47 Royal EducationSYBCA Material
8
1
6
4 7
3
9
1
0
0
0
0
1
5
11
0
0
0
8
10 6
4 7
3
9
2
1
1
0
0
2
5
11
0
0
0
2
0
Royal Education Preparation without Parents Blessing is Stack without Top
(b) after the insertion of 2 into the tree
P
A
(c) AVL rotation as per the lefttoleft
(Case 1) insertion
48 Royal EducationSYBCA Material
A
L
A
R
8
10
6
4 7
3
6
9
2
1
1
0
0
2
5
11
0
0
2
0
8
1 6
4 7
3
6
9
5
11
2
8
10 4
2
9 11
P
R
Royal Education Preparation without Parents Blessing is Stack without Top
A
L
A
R
P
R
(d) After AVL rotation
0
1 0 0
0
0 0
0
(e) Height balanced tree with newly inserted node
2 and after avl rotation
Case 2:
Unbalanced occurred due to the insertion in the right subtree of the
right child of the pivot node. This case is reverse and symmetric to
case 1.
left subtree (B
L
) of right child (B) of pivot node(P) becomes
the right sub
tree of P.
P becomes the left child of B>
Right Subtree (B
R
) of B remains same.
This case is know as righttoright insertion.
49 Royal EducationSYBCA Material
N D N
N
N E N
N
N F N
N
3 6
5
7
8
10
4
3 6
2
9
0
1
5
11
7
Royal Education Preparation without Parents Blessing is Stack without Top
Illustration: case 2 of AVL rotation occurs due to RighttoRight
insertion.
TAKE THE PRINTS ON PAGE: 283[(a),(b)]
Case 3: unbalance occurred due to the insertion in the right subtee of
the left child of the pivot node. The case is know as LefttoRight
insertion.
Case 3 involves two rotations for the manipulation in pointers:
> Rotation 1: (1) left subtree (B
L
)of the right child(B) of the left child
of the pivot node (P) becomes the right subtree of the left
child (A)
(2) left child(A) of the pivot node(P) becomes the left child of B.
> Rotation 2: (1) right subtree (B
R
) of the right child(B) of the left
child (A) of the
Pivot node (P) becomes the left subtree of P.
(2) P becomes the right child of B.
TAKE THE PRINTS ON PAGE: 285[(a),(b), (c),(d)]
Case 4: unbalance occurred due to the insertion in the left sub tree of
right child of the pivot node. This case is the reverse and symmetric
to case 3. this case is know as righttoleft insertion.
There are two rotations for the manipulations of pointers in this case,
these are:
Rotation 1:
right sub tree(B
R
) of the left child (B) of the right child (A) of
the pivot node (P) becomes the left sub tree of A.
right child (A) of the pivot node (P) becomes the right child
of B.
Rotation 2:
left sub tree(B
L
) of the right child (B) of the right child (A) of
the pivot node
(P) becomes the right sub tree of P.
50 Royal EducationSYBCA Material
N D N
N
N E N
N
N F N
N
Royal Education Preparation without Parents Blessing is Stack without Top
P becomes the left child of B.
Q.14 How many nodes are possible in binary tree of height h?
2
h
1
Q.15 Siblings :
The nodes having same parent are called Siblings.
For e.g.
Node B and C are Siblings.
Q.16 explain the following terms:
+ Root Node:
The node in a tree that has no parent is call the root node of a tree.
For e.g.
Node A is root node of a tree.
+ Leaf Node :
The node which is at the end and does not have any child is called the
Leaf node.
Sometime Leaf node is referred as terminal node.
For e.g.
Nodes D, H, L, F, J, and K are leaf nodes.
Skew binary tree: Binary tree has minimum number of nodes, if eah
level has min number of nodes (min possible node must be 1 at every
level)
Q.17 Give the node structure of linked representations of
binary tree.
Ans.
+ Tree is a data structure in which elements are arranged
in nonlinear fashion which requires 2D representation.
+ The figure drawn below shows the tree.
+ In a Link List each node has a link that points to another
node in a tree each node may point to several other nodes as shown
in the figure.
51 Royal EducationSYBCA Material
A
B N C
N D N
N
N E N
N
N F N
N
22
22
11
Royal Education Preparation without Parents Blessing is Stack without Top
+ Trees may have many variation, implementation and
application such as compiler construction, database design, operation
system program etc.
Q.18 What is spanning tree? What minimum spanning tree?
Find out the minimum spanning tree from the following graph
with the help of prims algorithm.
Spanning tree is a sub graph of G, is a tree, and contains
all vertices of G.
A minimum Spanning tree is a spanning tree, but has
weights or lengths associated with the edges, and the total weight of
the tree (the sum of the weight of its edges) is at a minimum.
52 Royal EducationSYBCA Material
V1
V2 V3
V4
V5
V6
V7
4 5
2
2
1
3
9
6
1
7
4
6
 
 
1 
7 4
 9


6
6
22
22
11
22
22
11
44
11
Royal Education Preparation without Parents Blessing is Stack without Top
Initial tree Stage 1
Stage 2 Stage 3
53 Royal EducationSYBCA Material
V1
V3 V2
V4
V5 V6
V7
V1
V3 V2
V4
V5 V6
V7
2222
2
V1
V3 V2
V4
V5 V6
V7
2
1
V1
V3 V2
V4
V5 V6
V7
22
22
11
V1
V3 V2
V4
V5 V6
V7
22
22
11
44
11
Royal Education Preparation without Parents Blessing is Stack without Top
STAGE 4 STAGE 5
54 Royal EducationSYBCA Material
V1
V3 V2
V4
V5 V6
V7
2
2
1
4
V1
V3 V2
V4
V5 V6
V7
2
2
1
62
1
4
V1
V3 V2
V4
V5 V6
V7
22
22
11
44
11
Royal Education Preparation without Parents Blessing is Stack without Top
Q.19 Create nomaru tree and do heap sort on following data.
10,5,70,15,12,35,50
BINARY TREE.
HEAP SORT
1.
55 Royal EducationSYBCA Material
10
5 70
15 35 50 12
10
15 70
5 35 50 12
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
2.
3.
By this three step binary tree represented as heap tree.
10. Give the structure of threaded binary tree.
Struct node
{
Int data;
Char thread;
Struct node*left;
Struct node*right;
}
Q.20 Give advantage and disadvantage of linked list representation of
binary tree.
Advantages:
Any node can be accessed from any other node by calculating
the index and this is efficient from execution point of view.
Here, data are stored only without any pointers to their
successor or ancestor which are mentioned implicitly.
Programming languages, where dynamic memory allocation is
not possible (such as BASIC, FORTRAN) array representation is the
array only means to store a tree.
Disadvantages:
Other than full binary trees, majority of the array entries
may be empty.
It allows only static representation. It doesnt have any
way possible to enhance the tree structure if the array size is limited.
Inserting a new node to it or deleting a node from it are
inefficient with this representation because these require
considerable data movement up and down the array which demand
excessive amount of processing time.
56 Royal EducationSYBCA Material
70
15 10
5 35 50 12
70
5 35 10 12
15
50
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
Q.21 Explain linked representation of binary tree.
Linear representation of binary trees has a number of overheads. In
linked representation of binary trees, all these overheads are taken
care of
DATA
Structure of a node in linked representation
Here LC and RC are two link fields to store the address of left child
and right child of a node; DATA is the information content of the
node. With this representation if one knows the address of the root
node then from it any other node can be accessed.
Both two structures tree and linked look almost similar implies that
the linked representation of a binary tree very closely resembles the
logical structure the data involved.
Q.22 Application of heap tree
Sorting
Priority queue implementation
GRAPH
57 Royal EducationSYBCA Material
RC
LC
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
Graph A graph G= (V, E) is a relation or mapping between non
empty vertices V and nonempty set of edges E.
A graph G consists of non empty set V called
a set of nodes of a graph. These nodes can also be called vertices or
point. A set of E, which is a set of edges of the graph and a mapping
from the set of edges E to a set of pair of elements of V.
n1 n2
n3
n4
n5
These graphs consist of a set of points or vertices
v1, v2, v3, v4 which are connected by lines n1, n2, n3, n4, n5. We
can associate a pair of nodes of a graph to every edge. If an edge N
E is associated with the pair of nodes (u, v), where u, v into v then we
can say that the edge can connects or joins the nodes u and v.
+ Direct Graph :
In a Graph G=(v, e) an edge which is directed from one node to another is called
directed edge and a graph in which every edge directed is called a directed graph or a
digraph. For example, a city maps showing only the
way streets.
+ Mixed Graph :
In a Graph if some edges are directed and some edges are indirected
then such a graph is called mixed graph. For example, a city maps
showing both oneway and twoway streets.
58 Royal EducationSYBCA Material
V
4
V
1
V
3
V
2
V
4
V
1
V
3
V
2
V
1
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
+ Undirected Graph :
In a Graph G= (v, e) when an edge has no specific direction is called an
undirected edge and a graph with every as undirected is called an
undirected graph. For example, A map showing only twoway streets.
+ Complete Graph :
A directed graph is said to be complete if each vertex vi is adjacent
to every other vertex vj in G.
+ Multi Graph :
Any Graph which contains parallel edges is called a multi graph.
+ Simple Graph :
A graph in which there is only one edge between a pair of
nodes is called simple graph.
+ Weighted Graph :
A graph in which weights are assigning to every edge is called a weighted graph.
A graph of a city street may be assign weight according to traffic density on each street.
59 Royal EducationSYBCA Material
V
5
V
2
V
4
V
3
V
6
V
4
V
1
V
3
V
2
V1 V2
V4 V3
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
Q. What is cut vertex?
If a graph is not biconnected then these vertices whose
removal would disconnect the graph are known as cut vertices are
articulation points.
Q. List the application of graph and explain one of them:
Following are the application of the graphs
1) Pert and related technique
PERT means Programming Evaluation and Review Technique.
By this technique a project is completed in minimum time and
with minimum resources.
In pert the different activities and events are arranged
sequentially and network is prepared. From the network the
interrelationship between the activities becomes clear. In Pert the
project is divided into different parts, each part is known as activity,
and when an activity is completed the result is known as an event. By
arranging the activities and events in logical order a network is
obtained. This network can be called a PERT diagram or a PERT chart.
The estimates of time for different activities are obtained and from
them the total time for completing the entire project is determined.
Activity: In any project if time or resources are used for any work it is
called an activity. If an activity is shown as 
4
> it indicates that the
time of 4 units is required for that activity.
Event: The result of any activity is called an event. Each event is
shown by circle in a PERT diagram.
2) Computer graphics application
3) Topological Sorting
Topological Sorting: This is explained in the notes written with
pen
Pert:
Difference between complete and connected graph
+ Null Graph :
A graph containing only isolated nodes is called a null graph.
For example,
60 Royal EducationSYBCA Material
V1
V3 V2
V4
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
Adjacent Node:
Any two nodes that are connected by an edge in a graph
are called adjacent nodes. In the above graph v1 and v3 are
adjacent.
+ Loop :
An edge of a graph, which joins to itself, is called a loop.
OR
An edge that initiates or originates in one node and terminates in a
same node is called a loop.
+ Parallel Edges :
In a Graph are two or more edges originate at one node and terminate
at a same node than those edges are called parallel edges.
+ Isolated Node :
In a graph a node which is not adjacent to any other node is called an
isolated node. For example,
+ Path :
A path is said to traverse through the nodes appearing in a sequence
originating in the initial node of the first edge and ending in a terminal
node of the last edge in a sequence.
61 Royal EducationSYBCA Material
V
3
V
2
V
4
V
1
V
3
V
2
V
4
V
1
V
1
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
+ Length :
The number of edges appearing in a sequence of a path is called
the length of the path.
+ Cycle :
For a directed graph a path which originate and ends in a same
node is called a cycle or circuit.
+ Acyclic :
A simple directed graph, which does not have any cycle is called
acyclic graph.
+ Pendent Vertex :
A vertex vi is a pendent if its indegree is equal to 1 and outdegree is 0.
+ Degree of a Node :
In a directed graph G for any node vertex v the number of edges that
have be as an initial node is the out degree of the node v, The
number of edges that have v as a terminal node is the indegree of
node v and sum of outdegree and indegree is called the total degree.
For an undirected graph the degree or the degree of the
node is equal to the number of node incident with v. The total degree
of loop is two and that of an isolated node is 0.
Indegree 2
62 Royal EducationSYBCA Material
V1 V2
V4 V3
V1 V2
V4 V3
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
Outdegree 0
Total degree 2
b) Loop An edge of a graph which joins a node to itself is called a
loop.
Q.What is a path matrix?
Let G= (V, E) be a simple diagraph which contains n nodes that
are assumed to be ordered. An nxn matrix P whose elements are
given by
P
ij
=1 if there exists a path from v
i
to v
j
=0 otherwise
is called the path matrix of the graph G.
Loop OR Sling: 
An edge of a graph which joins a node to itself is called a Loop or Sling.
Q. Give the different representation of graph
+ Representation of a Graph
A graph can be represented in many ways,
1) Using sets.
2) Using link list.
3) Using matrices.
1 ) Using sets :
This is one of the striate forward methods of
representing a graph. In this method two sets are maintain. Set v, the
set of vertices, v set e, the set of edges.
63 Royal EducationSYBCA Material
V
4
V
1
V
3
V
2
A B
C D
E
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
V = {A, B, C, D, E}
E = {(A, B) (B, E) (E, D) (D, C) (C, A) (C, B) (A, D) (B, D) (A, C)}
2) Using Link List:
In this representation two types of node structures are
use depending on the type of the graph.
In link representation the number of list depends on the number of
vertices in the graph. The header nodes in each list maintain a list of
a node for which header node is meant.
3) Using matrices:
Matrix representation is the most useful way of representing of any
graph. This represent uses a matrix of order n*m when n is the total
no. of vertices in the graph. Let G=(v, e) be a simple diagram in
which v=v1, v2
And the nodes are assumed to b ordered from V1 to Vn. A matrix
Awhose elements Aij are given by,
Aij= {1, if Vi, Vj E and 0, otherwise}
Is called the adjacent matrix of graph G. Any element in the adjacent
matrix is either 0 or 1.
64 Royal EducationSYBCA Material
V1
V2 V3
V4 V5 V6 V7
A B
C D
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
V1 V2 V3 V4
V1 0 1 0 1
V2 0 0 0 1
V3 0 1 0 1
V4 1 0 0 0
1) Write the algorithm for Breadth first search method.
BFS(INDEX)
1.Reach[INDEX]true
DIST [INDEX]0
CALL QINSERT (QUEUE, INDEX)
2. Repeat thru step 5 while queue is not empty
3. Call QDELETE(QUEUE,INDEX)
4.LINKLISTPTR[INDEX]
REPEAT STEP 5 WHILE LINK!= NULL
5. [If this is an unvisited node, label it and add it to the queue
If not REACH [DESTIN (LINK)]
(a) Write the algorithm for DES. What is articulation point?
DFS(INDEX,COUNT)
1. [Update the depth first search number set and mark current
node]
COUNTCOUNT+1
DFN [INDEX] COUNT
REACH [INDEX]TRUE
2.[Set up loop to examine each neighbour of current node]
LINKLISTPTR [INDEX]
Repeat step 3 while LINK! =NULL
3. [If node has not been marked label it and make a recursive call]
If not REACH [DESTIN (LINK)]
Then Call DFS (DESTIN (LINK), COUNT)
LINKEDGEPTR (LINK)
4. [Return to point of call]
65 Royal EducationSYBCA Material
22
11
22
V1 V2
V3 V4
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
Return
Q. Write down the difference between DFS and BFS
DFS BFS
In case of DFS, the concept of
stack is used. The entire
unvisited are pushed in the
stack.
In case of BFS queue is used. All
the unvisited vertices to the one
already visited are placed at the
rear of the queue
DFS picks up vertices all over the
graph (It traverse through the
depth of the graph)
BFS traverse all the vertices close
to the start vertex before
moving to the other vertices
DFS spans very narrow and long
trees
BFS on the other hand creates
very wide and short tree
Show diagram of DFS
Traversal
Show the Diagram of BFS
Traversal
Dijkstras algorithm
Another kind of the shortest path problem is the single source shortest
path problem. In this problem, there is a distinct vertex, called source
vertex and it requires finding the shortest path from this source
vertex to all other vertices.
From v1 to v2 v1v2 1
From v1 to v3 v1v2v3 3
From v1 to v4 v1v2v4 4
From v1 to v5 v1v2v3v5 5
66 Royal EducationSYBCA Material
Shoterst Path Length of the shortest path
v1
V2
V3
V4
V5
22
11
22
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
Algorithm KRUSHKAL (informal description)
1. List all of the edges of the graph G in the increasing order of
weights.
2. Select the smallest edge from the list and add it into the spanning
tree (initially it is empty) if the inclusion of this edge does not make a
cycle.
3. If the selected edge with smallest weight forms a cycle, remove it
from the list is empty.
4. Repeat step 2  3 until the tree contains n 1 edges and the list is
empty.
5. If the tree T contains less then n 1 edges and the list is empty,
no spanning tree is possible for the graph, else return the minimum
spanning.
2
1
6

Edge Weight Selection
67 Royal EducationSYBCA Material
V1
V6
V3
V4
V7
V5
V2
4
2
1
3
7
9 6 1 6
6
16
8 6 1 6
4 6 1 6
2 6 1 6
5 6 1 6
V1
V6
V3
V4
V7
V5
V2
22
11
22
Royal Education Preparation without Parents Blessing is Stack without Top Pointer

v2 v4 1
v3 v6 1
v2 v5 2
v1 v4 2
v4 v5 3 x
v1 v2 4 x
v4 v6 4
v1 v3 5 x
v6 v7 6
v4  v7 7
v3 v4 8
v5 v79
Discuss DFS and draw the DFS spanning tree for the following.
One question, we can answer with graph in figure 3 is can I get from
city x city y on by airlines? This is similar to ask the question. Does a
path exist in the graph from vertex x to vertex y? Using a DFS. Lets
develop an operation that finds a path from start vertex (x) to end
vertex (y).
We need a systematic way to keep track of cities as we
investigate them. With a DFS. We examine the first vertex that is
adjacent from start vertex, if this is end vertex; the search is over
otherwise we examine all the vertices that are adjacent from this
vertex.
Mean while we need to store the other vertices that are
adjacent fro start vertices. If a path doesnt exist from the first
vertices, we come back and try the second, thirdBecause we want
to travel as far as we can down one path back tracking if the end
vertex is not found, a stack would be a good data structure for storing
vertices. Here is the algorithm.
Depth first search
Found < False
Create stack (start)
Push (start, start vertex)
Repeat
Pop (stack, vertex)
If vertex = end vertex
Then
{
Print final
Found < true
}
Else
print the vertex
get adjacent list  (get list adjacent vertices)
push all the adjacent vertices onto stack
Until empty stack (stack) OR Found
68 Royal EducationSYBCA Material
Royal Education Preparation without Parents Blessing is Stack without Top Pointer
If not found
Then print Path does not exist
Q. Prims algorithm
PG 413 Dsamanta
69 Royal EducationSYBCA Material
Гораздо больше, чем просто документы.
Откройте для себя все, что может предложить Scribd, включая книги и аудиокниги от крупных издательств.
Отменить можно в любой момент.