Introduction to Data Structures Notes
Introduction to Data Structures Notes
Data Structures:
A data structure is an arrangement of data in a computer's memory or even disk storage.
Data structures can be classified into two types
Algorithm:
Step by Step process of representing solution to a problem in words is called an Algorithm.
Characteristics of an Algorithm:
Input : An algorithm should have zero or more inputs
Output: An algorithm should have one or more outputs
Finiteness: Every step in an algorithm should end in finite amount of time
Unambiguous: Each step in an algorithm should clearly stated
Effectiveness: Each step in an algorithm should be effective
1
Data Structures and Algorithms
2
Data Structures and Algorithms
Stack :
Stack is a Linear Data Structure which follows Last in First Out mechanism.
It means: the first element inserted is the last one to be removed
Stack uses a variable called top which points topmost element in the stack. top is incremented
while pushing (inserting) an element in to the stack and decremented while poping (deleting) an
element from the stack
D top
top
C C
top C
B top B B
BA
A top A A A
Push(A) Push(B) Push(C) Push(D) Pop()
Applications of Stack:
3
Data Structures and Algorithms
Queue:
Queue is a Linear Data Structure which follows First in First out mechanism.
It means: the first element inserted is the first one to be removed
Queue uses two variables rear and front. Rear is incremented while inserting an element into the
queue and front is incremented while deleting element from the queue
rear
D rear
C rear C D
rear
B rear B B C
front front
A front A A front A front B
Insert(A) Insert(B) Insert(C) Insert(D) Delete()
Note:
While inserting an element into the queue, queue is full condition should be checked
While deleting an element from the queue, queue is empty condition should be checked
Applications of Queues:
Real life examples
Waiting in line
Waiting on hold for tech support
Applications related to Computer Science
Threads
Job scheduling (e.g. Round-Robin algorithm for CPU allocation)
4
Data Structures and Algorithms
Linked List:
To overcome the disadvantage of fixed size arrays linked list were introduced.
A linked list consists of nodes of data which are connected with each other. Every node consist
of two parts data and the link to other nodes. The nodes are created dynamically.
NODE
bat
Data link
5
Data Structures and Algorithms
Trees :
A tree is a Non-Linear Data Structure which consists of set of nodes called vertices and set of
edges which links vertices
Terminology:
Root Node: The starting node of a tree is called Root node of that tree
Terminal Nodes: The node which has no children is said to be terminal node or leaf .
Non-Terminal Node: The nodes which have children is said to be Non-Terminal Nodes
Degree: The degree of a node is number of sub trees of that node
Depth: The length of largest path from root to terminals is said to be depth or height of
the tree
Siblings: The children of same parent are said to be siblings
Ancestors: The ancestors of a node are all the nodes along the path from the root to the
node
A Property Value
Number of nodes : 9
Height : 4
Root Node : A
B C Leaves : ED, H, I, F, C
Interior nodes : D, E, G
Number of levels : 5
Ancestors of H : I
D E F Descendants of B : D,E, F
Siblings of E : D, F
H I
6
Data Structures and Algorithms
Binary Trees:
Binary trees are special class of trees in which max degree for each node is 2
Recursive definition:
A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint
binary trees called the left subtree and the right subtree.
Any tree can be transformed into binary tree. By left child-right sibling representation.
E C
G D
K F
Inorder: In inorder traversing first left subtree is visited followed by root and right subtree
Preorder: In preorder traversing first root is visited followed by left subtree and right subtree.
Postorder: In post order traversing first left tree is visited followed by right subtree and root.
7
Data Structures and Algorithms
63
41 89
72 95
34 56
8
Data Structures and Algorithms
Avl Tree:
If in a binary search tree, the elements are inserted in sorted order then the height will be n,
where n is number of elements. To overcome this disadvantage balanced trees were introduced.
Balanced binary search trees
An AVL Tree is a binary search tree such that for every internal node v of T, the
heights of the children of v can differ by at most 1.
4
44
2 3
17 78
1 2 1
32 50 88
1 1
48 62
9
Data Structures and Algorithms
Graphs
A graph is a Non-Linear Data Structure which consists of set of nodes called vertices V and set
of edges E which links vertices
0 0
1 2 1 2
3
3 4 5 6
Graph Tree
Graph Traversal:
Problem: Search for a certain node or traverse all nodes in the graph
Depth First Search
Once a possible path is found, continue the search until the end of the path
Breadth First Search
Start several paths at a time, and advance in each one step at a time
10
Data Structures and Algorithms
What is all the fuss about objects and object-oriented technology? Is it real? Or is it hype? Well,
the truth is--it's a little bit of both. Object-oriented technology does, in fact, provide many
benefits to software developers and their products. However, historically a lot of hype has
surrounded this technology, causing confusion in both managers and programmers alike. Many
companies fell victim to this hardship (or took advantage of it) and claimed that their software
products were object-oriented when, in fact, they weren't. These false claims confused
consumers, causing widespread misinformation and mistrust of object-oriented technology.
Object:
As the name object-oriented implies, objects are key to understanding object-oriented
technology. You can look around you now and see many examples of real-world objects: your
dog, your desk, your television set, your bicycle.
Definition: An object is a software bundle of variables and related methods
Class:
In the real world, you often have many objects of the same kind. For example, your bicycle is
just one of many bicycles in the world. Using object-oriented terminology, we say that your
11
Data Structures and Algorithms
bicycle object is an instance of the class of objects known as bicycles. Bicycles have some state
(current gear, current cadence, two wheels) and behavior (change gears, brake) in common.
However, each bicycle's state is independent of and can be different from other bicycles.
Definition: A class is a blueprint or prototype that defines the variables and methods common to
all objects of a certain kind.
Inheritance:
Acquiring the properties of one class in another class is called inheritance
The Benefits of Inheritance
Subclasses provide specialized behaviors from the basis of common elements provided
by the super class. Through the use of inheritance, programmers can reuse the code in the
superclass many times.
Programmers can implement superclasses called abstract classes that define "generic"
behaviors. The abstract superclass defines and may partially implement the behavior but
much of the class is undefined and unimplemented. Other programmers fill in the details
with specialized subclasses.
Data Abstraction:
The essential element of object oriented programming in abstraction. The complexity of
programming in object oriented programming is maintained through abstraction.
For example, the program consist of data and code which work over data. While executing a
program we don’t thing in which location that data is being stored how the input device is
transferring the input to the memory etc. this abstraction allows us to execute the program
without thinking deeply about the complexity of execution of program.
Encapsulation:
Encapsulation is the mechanism that binds together code and the data and keeps them safe from
outside world. In the sense it is a protective wrapper that prevents the code and data from being
12
Data Structures and Algorithms
accessed by other code defied outside the wrapper. Access is controlled through a well defined
interface.
Polymorphism:
Existing in more that one form is called polymorphism.
Polymorphism means the ability to take more that one form. For example an operation may
exhibit different behavior in different behavior in different instances.
For example consider operation of addition. For two numbers the operation will generate a sum.
If the operands are string the operation would produces a third string by concatenation.
C++ supports polymorphism through method overloading and operator overloading
Method overloading:
if the same method name used for different procedures that the method is said to be overloaded.
Dynamic Binding:
Binding refer to the linking of a procedure call to the code to be executed in response to the call.
Dynamic binding means that the code associated with a given procedure call is not know until
the time of the call at runtime. It is associated with a polymorphism reference depends on the
dynamic type of that reference.
Message communication:
An object oriented program consists of objects that communicate with each other. The process
of programming in an object oriented language therefore involves the following basic steps:
1. creating classes that define objects and their behaviors.
2. creating objects from class definitions.
3. establishing communication among objects.
13
Data Structures and Algorithms
An Abstract Data Type (ADT) is more a way of looking at a data structure: focusing on what it
does and ignoring how it does its job. A stack or a queue is an example of an ADT. It is
important to understand that both stacks and queues can be implemented using an array. It is also
possible to implement stacks and queues using a linked list. This demonstrates the "abstract"
nature of stacks and queues: how they can be considered separately from their implementation.
To best describe the term Abstract Data Type, it is best to break the term down into "data type"
and then "abstract".
Data type:
When we consider a primitive type we are actually referring to two things: a data item with
certain characteristics and the permissible operations on that data. An int in Java, for example,
can contain any whole-number value from -2,147,483,648 to +2,147,483,647. It can also be used
with the operators +, -, *, and /. The data type's permissible operations are an inseparable part of
its identity; understanding the type means understanding what operations can be performed on it.
In C++, any class represents a data type, in the sense that a class is made up of data (fields) and
permissible operations on that data (methods). By extension, when a data storage structure like a
stack or queue is represented by a class, it too can be referred to as a data type. A stack is
different in many ways from an int, but they are both defined as a certain arrangement of data
and a set of operations on that data.
abstract
Now lets look at the "abstract" portion of the phrase. The word abstract in our context stands for
"considered apart from the detailed specifications or implementation".
In C++, an Abstract Data Type is a class considered without regard to its implementation. It can
be thought of as a "description" of the data in the class and a list of operations that can be carried
out on that data and instructions on how to use these operations. What is excluded though, is the
14
Data Structures and Algorithms
details of how the methods carry out their tasks. An end user (or class user), you should be told
what methods to call, how to call them, and the results that should be expected, but not HOW
they work.
We can further extend the meaning of the ADT when applying it to data structures such as a
stack and queue. In Java, as with any class, it means the data and the operations that can be
performed on it. In this context, although, even the fundamentals of how the data is stored should
be invisible to the user. Users not only should not know how the methods work, they should also
not know what structures are being used to store the data.
Consider for example the stack class. The end user knows that push() and pop() (amoung other
similar methods) exist and how they work. The user doesn't and shouldn't have to know how
push() and pop() work, or whether data is stored in an array, a linked list, or some other data
structure like a tree.
15
Data Structures and Algorithms
Pop()
{
If( stack is empty) print” stack under flow”
else
Decrement top
}
Display()
{
If ( stack is empty) print” no element to display”
else
for i= top to 0 step -1
Print satck[i];
}
16
Data Structures and Algorithms
Stack ADT
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
17
Data Structures and Algorithms
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};
void main()
{
int ch;
stack st;
clrscr();
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
}
OUTPUTS
1.push 2.pop 3.display 4.exit
Enter ur choice2
stack under flow
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element2
inserted2
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element3
inserted3
1.push 2.pop 3.display 4.exit
Enter ur choice2
deleted3
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element5
18
Data Structures and Algorithms
Insert ( item)
{
If rear = max -1 then print “ queue is full”
else
{
Increment rear
Queue [rear]=item;
}
}
Delete()
{
If front = rear print “queue is empty”
else
Increment front
}
Display()
{
If front=rear print “queue is empty “
else
For i =front to rear
Print queue[i];
}
19
Data Structures and Algorithms
Queue ADT
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class queue
{
int queue[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
queue[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<queue[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty";
20
Data Structures and Algorithms
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue[i]<<" ";
}
};
void main()
{
int ch;
queue qu;
clrscr();
while(1){
cout <<"\n1.insert 2.delet 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
qu.insert(ch);
break;
case 2: qu.delet(); break;
case 3: qu.display();break;
case 4: exit(0);
}
}
}
OUTPUT
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element21
inserted21
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element22
inserted22
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element16
inserted16
1.insert 2.delet 3.display 4.exit
Enter ur choice3
21 22 16 1.insert 2.delet 3.display 4.exit
21
Data Structures and Algorithms
Pop()
{
If(head is null) print” stack under flow”
else
goto last but one node and let it be temp
temp->next=NULL
}
Display()
{
If ( head=NULL) print” no element to display”
else
{
Temp=head;
While(temp!=NULL)
{
Print(“temp->data)
Temp=temp->next;
}
}
22
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
class node *next;
int data;
};
23
Data Structures and Algorithms
temp1->next=NULL;
temp1->data=x;
}
}
void display()
{
node *temp;
temp=head;
if (tos < 0)
{
cout <<" stack under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}
void pop()
{
node *temp;
temp=head;
if( tos < 0 )
{
cout <<"stack under flow";
return;
}
tos--;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
}
};
void main()
{
stack s1;
int ch;
clrscr();
while(1)
{
cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ru choice:";
24
Data Structures and Algorithms
OUTPUT
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element23
25
Data Structures and Algorithms
Insert ( item)
{
If rear = max -1 then print “ queue is full”
else
{
Increment rear
Create a new node called item
goto last node in the list and let it be temp
temp-next=item;
item-next=NULL;
}
}
Delete()
{
If front = rear print “queue is empty”
else
{
Increment front
head=head-next;
}
}
Display()
{
If front=rear print “queue is empty “
else Temp=head;
While(temp!=NULL)
{
Print(“temp-data)
Temp=temp-next;
}
26
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
class node *next;
int data;
};
if (rare < 0 )
{
head =new node;
head->next=NULL;
head->data=x;
rare ++;
}
else
{
node *temp,*temp1;
temp=head;
if(rare >= 4)
{
cout <<"queue over flow";
return;
}
rare++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
27
Data Structures and Algorithms
temp->next=temp1;
temp1->next=NULL;
temp1->data=x;
}
}
void display()
{
node *temp;
temp=head;
if (rare < 0)
{
cout <<" queue under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}
void pop()
{
node *temp;
temp=head;
if( rare < 0)
{
cout <<"queue under flow";
return;
}
if(front == rare)
{
front = rare =-1;
head=NULL;
return;
}
front++;
head=head->next;
}
};
void main()
{
queue s1;
int ch;
28
Data Structures and Algorithms
clrscr();
while(1)
{
cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ru choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"\n enter a element";
cin >> ch;
s1.push(ch);
break;
case 2: s1.pop();break;
case 3: s1.display();
break;
case 4: exit(0);
}
}
}
OUTPUT
29
Data Structures and Algorithms
Deletefirst()
{
If (dequeue is empty) print” no node to delete”;
else
{
Head=head-next;
Head-prev=NULL;
}
}
30
Data Structures and Algorithms
Deletelast()
{
if (dequeue is empty) print” no node to delete”;
else
{
tail=tail-prev;
tail-next=NULL;
}
}
Displayfirst()
{
if( dequeue is empty) print “ no node to display”
else
{
temp=head;
while(temp-next!=null) then do
{
print(temp-data);
temp=temp-next;
}
}
}
Displaylast()
{
if( dequeue is empty) print “ no node to display”
else
{
temp=tail
while(temp-prevt!=null) then do
{
print(temp-data);
temp=temp-prev;
}
}
}
31
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
int data;
class node *next;
class node *prev;
};
void push(int x)
{
node *temp;
int ch;
if(top1+top2 >=5)
{
cout <<"dqueue overflow";
return ;
}
if( top1+top2 == 0)
{
head = new node;
head->data=x;
head->next=NULL;
head->prev=NULL;
tail=head;
top1++;
}
32
Data Structures and Algorithms
else
{
temp->prev=NULL;
head->prev=temp;
head=temp;
}
else
{
top2++;
temp=new node;
temp->data=x;
temp->next=NULL;
temp->prev=tail;
tail->next=temp;
tail=temp;
}
}
}
void pop()
{
int ch;
cout <<"Delete 1.First Node 2.Last Node\n Enter ur choice:";
cin >>ch;
if(top1 + top2 <=0)
{
cout <<"\nDqueue under flow";
return;
}
if(ch==1)
{
head=head->next;
head->prev=NULL;
top1--;
}
else
33
Data Structures and Algorithms
{
top2--;
tail=tail->prev;
tail->next=NULL;
}
}
void display()
{
int ch;
node *temp;
cout <<"display from 1.Staring 2.Ending\n Enter ur choice";
cin >>ch;
if(top1+top2 <=0)
{
cout <<"under flow";
return ;
}
if (ch==1)
{
temp=head;
while(temp!=NULL)
{
cout << temp->data <<" ";
temp=temp->next;
}
}
else
{
temp=tail;
while( temp!=NULL)
{
cout <<temp->data << " ";
temp=temp->prev;
}
}
}
};
void main()
{
dqueue d1;
int ch;
clrscr();
while (1)
34
Data Structures and Algorithms
{
cout <<"1.INSERT 2.DELETE 3.DISPLAU 4.EXIT\n Enter ur choice:";
cin >>ch;
switch(ch)
{
case 1: cout <<"enter element";
cin >> ch;
d1.push(ch); break;
case 2: d1.pop(); break;
case 3: d1.display(); break;
case 4: exit(1);
}
}
}
OUTPUT
1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:1
enter element4
1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:1
enter element5
Add element 1.FIRST 2.LAST
enter ur choice:1
1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:1
enter element6
Add element 1.FIRST 2.LAST
enter ur choice:2
1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:3
display from 1.Staring 2.Ending
Enter ur choice1
5 4 6 1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:2
Delete 1.First Node 2.Last Node
Enter ur choice:1
1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:3
display from 1.Staring 2.Ending
Enter ur choice1
4 6 1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:4
35
Data Structures and Algorithms
Algorithm Deletet()
{
If (dequeue is empty) print” no node to delete”;
else
{
Front=(front+1) mod max
}
}
Algorithm display()
{
If (front >rear) display elements for front to max and 0 to rear
Else display elements from front to rear
}
36
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class cqueue
{
int q[5],front,rare;
public:
cqueue()
{
front=-1;
rare=-1;
}
void push(int x)
{
if(front ==-1 && rare == -1)
{
q[++rare]=x;
front=rare;
return;
}
else if(front == (rare+1)%5 )
{
cout <<" Circular Queue over flow";
return;
}
rare= (rare+1)%5;
q[rare]=x;
}
void pop()
{
if(front==-1 && rare== -1)
{
cout <<"under flow";
return;
}
else if( front== rare )
{
front=rare=-1;
return;
37
Data Structures and Algorithms
}
front= (front+1)%5;
}
void display()
{
int i;
if( front <= rare)
{
for(i=front; i<=rare;i++)
cout << q[i]<<" ";
}
else
{
for(i=front;i<=4;i++)
{
cout <<q[i] << " ";
}
for(i=0;i<=rare;i++)
{
cout << q[i]<< " ";
}
}
}
};
void main(){
int ch;
cqueue q1;
clrscr();
while( 1)
{
cout<<"\n1.INSERT 2.DELETE 3.DISPLAY 4.EXIT\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout<<"enter element";
cin >> ch;
q1.push(ch);
break;
case 2: q1.pop(); break;
case 3: q1.display(); break;
case 4: exit(0);
}
}
}
38
Data Structures and Algorithms
OUTPUT
39
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
# define max 10
class Dictionary
{
public:
int index;
Dictionary();
void insert(int);
void search(int);
void delete_ele(int);
};
Dictionary::Dictionary()
{
index=-1;
for(int i=0;i<max;i++)
{
root[i]=NULL;
ptr[i]=NULL;
temp[i]=NULL;
}
}
40
Data Structures and Algorithms
{
root[index]=ptr[index];
root[index]->next=NULL;
temp[index]=ptr[index];
}
else
{
temp[index]=root[index];
while(temp[index]->next!=NULL)
temp[index]=temp[index]->next;
temp[index]->next=ptr[index];
}
}
41
Data Structures and Algorithms
temp[index]->data=-1;
temp[index]=NULL;
free(temp[index]);
}
void main()
{
int val,ch,n,num;
char c;
Dictionary d;
clrscr();
do
{
cout<<"\nMENU:\n1.Create";
cout<<"\n2.Search for a value\n3.Delete an value";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter the number of elements to be inserted:";
cin>>n;
cout<<"\nEnter the elements to be inserted:";
for(int i=0;i<n;i++)
{
cin>>num;
d.insert(num);
}
break;
case 2: cout<<"\nEnter the element to be searched:";
cin>>n;
d.search(n);
case 3: cout<<"\nEnter the element to be deleted:";
cin>>n;
d.delete_ele(n);
break;
default: cout<<"\nInvalid choice....";
}
cout<<"\nEnter y to continue......";
cin>>c;
}while(c=='y');
getch();
}
42
Data Structures and Algorithms
43
Data Structures and Algorithms
OUTPUT
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:1
Enter y to continue......y
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:2
44
Data Structures and Algorithms
AVL TREE
Algorithm insertion(int x)
{
If(tree is empty) then root is empty
Otherwise
{
temp=search(item); // temp is the node where search for the
item halts
if( item > temp) then temp-right=item;
otherwise temp-left =item
Algorithm Search(int x)
Algorithm delete()
{
Case 1: the node to be deleted is a leaf
Case 2: the node to be deleted has no right child, that
is, its right subtree is empty
Case 3: the node to be deleted has no left child, that
is, its left subtree is empty
Case 4: the node to be deleted has a left child and a
right child
}
45
Data Structures and Algorithms
AVL TREE
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void insert(int,int );
void delte(int);
void display(int);
int search(int);
int search1(int,int);
int avltree[40],t=1,s,x,i;
void main()
{
int ch,y;
for(i=1;i<40;i++)
avltree[i]=-1;
while(1)
{
cout <<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.SEARCH\n5.EXIT\nEnter
your choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"enter the element to insert";
cin >> ch;
insert(1,ch);
break;
case 2:
cout <<"enter the element to delete";
cin >>x;
y=search(1);
if(y!=-1) delte(y);
else cout<<"no such element in avlavltree";
break;
case 3:
display(1);
cout<<"\n";
for(int i=0;i<=32;i++)
cout <<i;
cout <<"\n";
break;
46
Data Structures and Algorithms
case 4:
cout <<"enter the element to search:";
cin >> x;
y=search(1);
if(y == -1) cout <<"no such element in avltree";
else cout <<x << "is in" <<y <<"position";
break;
case 5:
exit(0);
}
}
}
}
else {
avltree[2*x+1]=ch;
y=log(2*x)/log(2);
if(height(1,y))
{
if(x%2==1)
update1();
else
update2();
}
}
47
Data Structures and Algorithms
t++;
}
void delte(int x)
{
if( avltree[2*x]==-1 && avltree[2*x+1]==-1)
avltree[x]=-1;
else if(avltree[2*x]==-1)
{ avltree[x]=avltree[2*x+1];
avltree[2*x+1]=-1;
}
else if(avltree[2*x+1]==-1)
{ avltree[x]=avltree[2*x];
avltree[2*x]=-1;
}
else
{
avltree[x]=avltree[2*x];
delte(2*x);
}
t--;
}
int search(int s)
{
if(t==1)
{
cout <<"no element in avltree";
return -1;
}
if(avltree[s]==-1)
return avltree[s];
if(avltree[s]>x)
search(2*s);
else if(avltree[s]<x)
search(2*s+1);
else
return s;
}
void display(int s)
{
if(t==1)
{
cout <<"no element in avltree:";
return;
}
48
Data Structures and Algorithms
for(int i=1;i<40;i++)
if(avltree[i]==-1)
cout <<" ";
else cout <<avltree[i];
return ;
}
49
Data Structures and Algorithms
OUTPUT
do u want to continuey
do u want to continuey
itemfound
do u want to continuey
50
Data Structures and Algorithms
51
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
void main()
{
clrscr();
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
OUTPUT
52
Data Structures and Algorithms
enterno of vertices9
ente no of edges9
EDGES
12
23
15
14
47
78
89
26
57
enter initial vertex1
Visited vertices
12 4 5 3 6 7 8 9
53
Data Structures and Algorithms
54
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
void main()
{
int m;
clrscr();
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
55
Data Structures and Algorithms
OUTPUT
enterno of vertices9
ente no of edges9
EDGES
12
23
26
15
14
47
57
78
89
enter initial vertex1
ORDER OF VISITED VERTICES1 2 3 6 4 7 8 9 5
56
Data Structures and Algorithms
Prim’s Algorithm
Algorithm Prim(E,Cost,n,t)
{
Let (k, l) be an edge of minimum cost in E;
Mincost= cost[k,l];
t[1,1]=k;
t[1,2]=l;
for i=1 to n do
{
If (cost[i, l]<cost[k,l]) then near[i]=l;
Else
Near[i]=k;
Near[k]=near[j]=0;
}
For i=2 to n -1 do
{
Let j be an index such that nearpj]!= 0 and cost[j,near[j]]
is minimum
T[I,1]=j ; t[I,2]=near[j]
mincost=mincost + cost[j,near[j]];
near[j]=0;
for k=1 to n do
if(near[k] !=0 ) and cost[k,near[k]]) >cost[k,j])
then near[j]=k
}
57
Data Structures and Algorithms
Prim’s Algorithm
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u;
void main()
{
int m,c;
clrscr();
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES Cost\n";
for(k=1;k<=m;k++)
{
cin >>i>>j>>c;
cost[i][j]=c;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0)
cost[i][j]=31999;
58
Data Structures and Algorithms
{
visit[j]=1;
stk[top]=j;
top++;
m=cost[v][j];
u=j;
}
}
cost[v][u]=31999;
v=u;
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}
OUTPUT
enterno of vertices7
ente no of edges9
EDGES Cost
1 6 10
6 5 25
5 4 22
4 3 12
3 2 16
2 7 14
5 7 24
4 7 18
1 2 28
ORDER OF VISITED VERTICES1 6 5 4 3 2
59
Data Structures and Algorithms
Kruskal’s Algorithm
}
If( i != n-1) the write (“ no spanning tree”);
else
Return mincost
}
}
60
Data Structures and Algorithms
Kruskal’s Algorithm
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,m,c,visit,visited[10],l,v,count,count1,vst,p;
main()
{
int dup1,dup2;
cout<<"enter no of vertices";
cin >> n;
cout <<"enter no of edges";
cin >>m;
cout <<"EDGE Cost";
for(k=1;k<=m;k++)
{
cin >>i >>j >>c;
cost[i][j]=c;
cost[j][i]=c;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0)
cost[i][j]=31999;
visit=1;
while(visit<n)
{
v=31999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]!=31999 && cost[i][j]<v && cost[i][j]!=-1 )
{
count =0;
for(p=1;p<=n;p++)
{
if(visited[p]==i || visited[p]==j)
count++;
}
if(count >= 2)
{
for(p=1;p<=n;p++)
if(cost[i][p]!=31999 && p!=j)
dup1=p;
61
Data Structures and Algorithms
for(p=1;p<=n;p++)
if(cost[j][p]!=31999 && p!=i)
dup2=p;
if(cost[dup1][dup2]==-1)
continue;
}
l=i;
k=j;
v=cost[i][j];
}
cout <<"edge from " <<l <<"-->"<<k;
cost[l][k]=-1;
cost[k][l]=-1;
visit++;
count=0;
count1 =0;
for(i=1;i<=n;i++)
{
if(visited[i]==l)
count++;
if(visited[i]==k)
count1++;
}
if(count==0)
visited[++vst]=l;
if(count1==0)
visited[++vst]=k;
}
}
62
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int shortest(int ,int);
int cost[10][10],dist[20],i,j,n,k,m,S[20],v,totcost,path[20],p;
void main()
{
int c;
clrscr();
cout <<"enter no of vertices";
cin >> n;
cout <<"enter no of edges";
cin >>m;
cout <<"\nenter\nEDGE Cost\n";
for(k=1;k<=m;k++)
{
cin >> i >> j >>c;
cost[i][j]=c;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0)
cost[i][j]=31999;
cout <<"enter initial vertex";
cin >>v;
cout << v<<"\n";
shortest(v,n);
shortest(int v,int n)
{
int min;
for(i=1;i<=n;i++)
{
S[i]=0;
dist[i]=cost[v][i];
}
path[++p]=v;
63
Data Structures and Algorithms
S[v]=1;
dist[v]=0;
for(i=2;i<=n-1;i++)
{
k=-1;
min=31999;
for(j=1;j<=n;j++)
{
if(dist[j]<min && S[j]!=1)
{
min=dist[j];
k=j;
}
}
if(cost[v][k]<=dist[k])
p=1;
path[++p]=k;
for(j=1;j<=p;j++)
cout<<path[j];
cout <<"\n";
//cout <<k;
S[k]=1;
for(j=1;j<=n;j++)
if(cost[k][j]!=31999 && dist[j]>=dist[k]+cost[k][j] && S[j]!=1)
dist[j]=dist[k]+cost[k][j];
}
}
OUTPUT
enter no of vertices6
enter no of edges11
enter
EDGE Cost
1 2 50
1 3 45
1 4 10
2 3 10
2 4 15
3 5 30
4 1 10
64
Data Structures and Algorithms
4 5 15
5 2 20
5 3 35
653
enter initial vertex1
1
14
145
1452
13
65
Data Structures and Algorithms
66
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
class node *left;
class node *right;
int data;
};
67
Data Structures and Algorithms
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);
} }
while(p!=NULL)
{
cout <<p->data << " ";
if(p->right!=NULL)
{
stk[top]=p->right->data;
top++;
}
p=p->left;
if(p==NULL && top>0)
{
68
Data Structures and Algorithms
p=pop(root);
}
}
}
node * pop(node *p)
{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
void main()
{
tree t1;
int ch,n,i;
while(1)
{
cout <<"\n1.INSERT\n2.DISPLAY 3.PREORDER TRAVERSE\n4.EXIT\nEnter
your choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter no of elements to insert:";
cout<<"\n enter the elements";
cin >> n;
for(i=1;i<=n;i++)
{ cin >> ch;
t1.insert(ch);
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.preorder(t1.root); break;
case 4: exit(1);
}
}
}
OUTPUT
69
Data Structures and Algorithms
1.INSERT
2.DISPLAY 3.PREORDER TRAVERSE
4.EXIT
Enter your choice:1
enter no of elements to insert
enter the elements7
5 24 36 11 44 2 21
1.INSERT
2.DISPLAY 3.PREORDER TRAVERSE
4.EXIT
Enter your choice:2
2 5 11 21 24 36 44
1.INSERT
2.DISPLAY 3.PREORDER TRAVERSE
4.EXIT
Enter your choice:3
5 2 24 11 21 36 44
1.INSERT
2.DISPLAY 3.PREORDER TRAVERSE
4.EXIT
Enter your choice:4
70
Data Structures and Algorithms
71
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
class node *left;
class node *right;
int data;
};
72
Data Structures and Algorithms
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);
} }
cout<<temp->data;
display(temp->right);
}
void inorder( node *root)
{
node *p;
p=root;
top=0;
do
{
while(p!=NULL)
{
stk[top]=p->data;
top++;
p=p->left;
}
if(top>0)
{
p=pop(root);
73
Data Structures and Algorithms
void main()
{
tree t1;
int ch,n,i;
while(1)
{
cout <<"\n1.INSERT\n2.DISPLAY 3.INORDER TRAVERSE\n4.EXIT\nEnter
your choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter no of elements to insert:";
cin >> n;
for(i=1;i<=n;i++)
{ cin >> ch;
t1.insert(ch);
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.inorder(t1.root); break;
case 4: exit(1);
}
}
74
Data Structures and Algorithms
OUTPUT
1.INSERT
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:1
enter no of elements to inser
5 24 36 11 44 2 21
1.INSERT
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:3
251121243644
1.INSERT
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:3
251121243644
1.INSERT
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:4
75
Data Structures and Algorithms
}
else
{
pop stack into current and v;
if(v == 1)
{
push current and 2 onto stack;
current = current->rlink;
v = 0;
}
else
visit current;
}}
76
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
class node *left;
class node *right;
int data;
};
77
Data Structures and Algorithms
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);
} }
while(1)
{
while(p!=NULL)
{
stk[top]=p->data;
top++;
if(p->right!=NULL)
stk[top++]=-p->right->data;
p=p->left;
78
Data Structures and Algorithms
}
node * pop(node *p)
{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
void main()
{
tree t1;
int ch,n,i;
clrscr();
while(1)
{
cout <<"\n1.INSERT\n2.DISPLAY 3.POSTORDER TRAVERSE\n4.EXIT\
nEnter your choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter no of elements to insert:";
cout<<"\n enter the elements";
cin >> n;
for(i=1;i<=n;i++)
{ cin >> ch;
t1.insert(ch);
79
Data Structures and Algorithms
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.postorder(t1.root); break;
case 4: exit(1);
}
}
}
OUTPUT
1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:1
enter no of elements to insert:
enter the elements7
5 24 36 11 44 2 21
1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:2
2 5 11 21 24 36 44
1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:3
2 21 11 44 36 24 5
1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:4
80
Data Structures and Algorithms
Algorithm quicksort(a[],p,q)
{
V=a[p];
i=p;
j=q;
if(i<j)
{
repeat
{
repeat
I=i+1;
Until( a[i]> v);
Repeat
J=j-1;
Until(a[j]<v);
If(i<j) then interchange ( a[i],a[j])
}until (i<=j);
interchange(a[j], a[p])
}
quicksort(a[],p,j);
quicksort(a[],j+1,q);
81
Data Structures and Algorithms
Quick Sort
#include<iostream.h>
#include<conio.h>
int a[10],l,u,i,j;
void quick(int *,int,int);
void main()
{
clrscr();
cout <<"enter 10 elements";
for(i=0;i<10;i++)
cin >> a[i];
l=0;
u=9;
quick(a,l,u);
cout <<"sorted elements";
for(i=0;i<10;i++)
cout << a[i] << " ";
getch();
}
82
Data Structures and Algorithms
a[l]=temp;
cout <<"\n";
for(i=0;i<10;i++)
cout <<a[i]<<" ";
quick(a,l,j-1);
quick(a,j+1,u);
}
}
OUTPUT
enter 10 elements5 2 3 16 25 1 20 7 8 61 14
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 8 16 20 7 25 61
1 2 3 5 7 8 20 16 25 61
1 2 3 5 7 8 16 20 25 61
1 2 3 5 7 8 16 20 25 61 sorted elements1 2 3 5 7 8 16 20 25 61
83
Data Structures and Algorithms
Algorithm Mergesort(low,high)
{
If(low<high)
{
Mid=(low+high)/2;
Mergesort(low,mid);
Mergesort(mid+1,high)
Merge(low,mid,high);
}
}
Algorithm Merge(low,mid,high)
{
h=low; i=low; j=mid+1;
While(h<=mid and j<=high) do
{
If(a[h]<a[j]) then
{
b[i]=a[h];
h=h+1;
}
else
{
b[i]=a[j];
J=j+1;
}
if(h>mid)
{
For k= j to high do
b[i]=a[k]; i=i+1;
}
Else
{
For k=h to mid do
b[i]=a[k]; i=i+1;
}
For k= low to high do a[k]=b[k];
}
}
84
Data Structures and Algorithms
Merge Sort
Merge Sort
#include<iostream.h>
#include<conio.h>
int a[20],i,n,b[20];
void main()
{
clrscr();
cout <<"\N enter no of elements";
cin >> n;
cout <<"enter the elements";
for(i=0;i<n;i++)
cin >> a[i];
mergesort(a,0,n-1);
cout <<" numbers after sort";
for(i=0;i<n;i++)
cout << a[i] << " ";
getch();
}
85
Data Structures and Algorithms
j=mid+1;
while(h<=mid && j<=high)
{
if(a[h]<=a[j])
b[i]=a[h++];
else
b[i]=a[j++];
i++;
}
if( h > mid)
for(k=j;k<=high;k++)
b[i++]=a[k];
else
for(k=h;k<=mid;k++)
b[i++]=a[k];
cout <<"\n";
for(k=low;k<=high;k++)
{
a[k]=b[k];
cout << a[k] <<" ";
}
OUTPUT
N enter no of elements8 12 5 61 60 50 1 70 81
enter the elements
5 12
60 61
5 12 60 61
1 50
70 81
1 50 70 81
1 5 12 50 60 61 70 81 numbers after sort1 5 12 50 60 61 70 81
86
Data Structures and Algorithms
Definition: A heap is a list in which each element contains a key, such that the key in the
element at position k in the list is at least as large as the key in the element at position 2k + 1
(if it exists), and 2k + 2 (if it exists)
Algorithm Heapify(a[],n)
{
For i=n/2 to 1 step -1
Adjustify (a,i,n);
}
Algorithm Adjustify(a[],i,n)
{
Repeat
{
J=leftchild(i)
Compare left and right child of a[i] and store the index of grater number in j
Compare a[j] and a[i]
If (a[j]>a[i]) then
Copy a[j] to a[i] and move to next level
}until(j<n)
87
Data Structures and Algorithms
Heap Sort
#include<stdio.h>
#include<conio.h>
int a[20],n;
main()
{
int i,j,temp;
clrscr();
printf("ente n");
scanf("%d",&n);
printf("enter the elements");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heapify(a,n);
for(j=1;j<=n;j++)
printf("%d",a[j]);
for(i=n;i>=2;i--)
{
temp=a[i];
a[i]=a[1];
a[1]=temp;
adjust(a,1,i-1);
printf("\n");
for(j=1;j<=n;j++)
printf("%d ",a[j]);
}
printf("\nelements after sort");
for(i=1;i<=n;i++)
printf("%d ",a[i]);
}
heapify(int a[],int n)
{
int i;
for( i=n/2;i>=1;i--)
adjust(a,i,n);
}
88
Data Structures and Algorithms
int j,iteam;
j=2*i;
iteam=a[i];
while(j<=n)
{
if(j<n && a[j]<a[j+1])
j=j+1;
if(iteam>=a[j])
break;
a[j/2]=a[j]; j=2*j;
}
a[j/2]=iteam;
}
89
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
int cost[10][10],a[10][10],i,j,k,c;
void main()
{
int n,m;
cout <<"enter no of vertices";
cin >> n;
cout <<"enter no od edges";
cin >> m;
cout<<"enter the\nEDGE Cost\n";
for(k=1;k<=m;k++)
{
cin>>i>>j>>c;
a[i][j]=cost[i][j]=c;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(a[i][j]== 0 && i !=j)
a[i][j]=31999;
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
90
Data Structures and Algorithms
OUTPUT
enter no of vertices3
enter no od edges5
enter the
EDGE Cost
124
216
1 3 11
313
232
Resultant adj matrix
046
502
370
91
Data Structures and Algorithms
Algorithm Delete(x)
{
Search for x in the tree
If (not found) print” not found”
Otherwise{
If ( x has no child) delete x;
If(x has left child) move the left child to x position
If(x has right child) move the right child to x position
If(x has both left and right children) replace ‘x’ with
greatest of left subtree of ‘x ‘ or smallest of right
subtree of ‘x’ and delete selected node in the subtree
}
}
92
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
class node *left;
class node *right;
int data;
};
93
Data Structures and Algorithms
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);
} }
94
Data Structures and Algorithms
{
tree t1;
int ch,n,i;
clrscr();
while(1)
{
cout <<"\n1.INSERT\n2.POP\n3.DISPLAY\n4.EXIT\nEnter your choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter no of elements to insert:";
cout<<"\n enter the elements";
cin >> n;
for(i=1;i<=n;i++)
{ cin >> ch;
t1.insert(ch);
}
break;
case 2: t1.pop(); break;
case 3: t1.display(t1.root);break;
case 4: exit(1);
}
}
}
1.INSERT
2.POP 3.DISPLAY 4.EXIT
Enter your choice:1
enter no of elements to insert:
enter the elements7
5 24 36 11 44 2 21
1.INSERT
2.POP 3.DISPLAY 4.EXIT
Enter your choice:3
2 5 11 21 24 36 44
95
Data Structures and Algorithms
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#define MAX 10
int find(int i,int j);
void print(int,int);
int p[MAX],q[MAX],w[10][10],c[10][10],r[10][10],i,j,k,n,m;
char idnt[7][10];
void main()
{
clrscr();
cout << "enter the no, of identifiers";
cin >>n;
cout <<"enter identifiers";
for(i=1;i<=n;i++)
gets(idnt[i]);
cout <<"enter success propability for identifiers";
for(i=1;i<=n;i++)
cin >>p[i];
cout << "enter failure propability for identifiers";
for(i=0;i<=n;i++)
cin >> q[i];
for(i=0;i<=n;i++)
{
w[i][i]=q[i];
c[i][i]=r[i][i]=0;
w[i][i+1]=q[i]+q[i+1]+p[i+1];
r[i][i+1]=i+1;
c[i][i+1]=q[i]+q[i+1]+p[i+1];
}
w[n][n]=q[n];
r[n][n]=c[n][n]=0;
for(m=2;m<=n;m++)
{
for(i=0;i<=n-m;i++)
{
j=i+m;
w[i][j]=w[i][j-1]+p[j]+q[j];
k=find(i,j);
r[i][j]=k;
c[i][j]=w[i][j]+c[i][k-1]+c[k][j];
96
Data Structures and Algorithms
}
}
cout <<"\n";
print(0,n);
OUTPUT
enter the no, of identifiers4
enter identifiersdo
if
int
while
enter success propability for identifiers3 3 1 1
enter failure propability for identifiers2 3 1 1 1
97
Data Structures and Algorithms
4. Describe PRIVATE, PROTECTED and PUBLIC - the differences and give examples.
5. What is a COPY CONSTRUCTOR and when is it called (this is a frequent question !)?
6. Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have
a base class SHAPE, how would I define DRAW methods for two objects CIRCLE and
SQUARE.
7. What is the word you will use when defining a function in base class to allow this
8. You have two pairs: new() and delete() and another pair : alloc() and free(). Explain
15. What are Virtual Functions? How to implement virtual functions in “C”
98
Data Structures and Algorithms
19. How to write a program such that it will delete itself after exectution?
20. Can we generate a C++ source code from the binary file?
27. What will happen if I allocate memory using “new” and free it using “free” or allocate
30. Write any small program that will compile in “C” but not in “C++”
99
Data Structures and Algorithms
100