You are on page 1of 28

DATA STRUCTURES

1. What is a data structure?


Data structure :
A data structure is a group of data elements grouped together under one name. These
data elements, known as members, can have different types and different lengths. Data
structures are declared in C++ using the following syntax:

struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object _names;

where structure_name is a name for the structure type, object_name can be a set of valid
identifiers for objects that have the type of this structure. Within braces { } there is a list
with the data members, each one is specified with a type and a valid identifier as its
name.

The first thing we have to know is that a data structure creates a new type: Once a data
structure is declared, a new type with the identifier specified as structure_name is created
and can be used in the rest of the program as if it was any other type.

2. What does abstract data type means?
Abstract data type :
Abstract data type is a specification of data types having some defined set of operations
and which are independent of their implementation.
Example :stack is an Abstract data type. but stack implemented with an array and with
linked list are both different data structures.

3. Evaluate the following prefix expression " ++ 26 + - 1324" (Similar types can be asked)
Prefix expression evaluation : ++26+-1324
++26+-1324
+(2+6)+(1-3)24
+(2+6)((1-3)+(24))
(2+6)+((1-3)+(24))
8+(-2+24)
30


4. Convert the following infix expression to post fix notation ((a+2)*(b+4)) -1
Infix to post fix notation : ((a+2)*(b+4)) -1

( ( a + 2 ) * ( b + 4 ) ) - 1
\ / /
a2+ b4+ /
\ / /
/
a2+b4+* /
\ /
a2+b4+*1-

5. How is it possible to insert different type of elements in stack?
It is possible to insert different types of elements in a stack. Elements in a stack can be
inserted using the Push operation. This operation writes an element on the stack and
moving the pointer.
(Or)
Different elements can be inserted into a stack. This is possible by implementing union /
structure data type. It is efficient to use union rather than structure, as only one items
memory is used at a time.

6. Stack can be described as a pointer. Explain.
Stack is described as pointer

Stack can be described as a pointer as it contains a head pointer always pointing to the
topmost element of the stack. The Push and Pop operations are performed using this
pointer.

7. Write a Binary Search program
Binary search program :

#include<conio.h>
#include<iostream.h>
#include<process.h>

int binarysearch(int list[], int end, int target, int &locn)
{
int first=0, mid, last=end;
while(first<=last)
{
mid=(first+last)/2;
if(target>list[mid])
first=mid+1;
else if(target<list[mid])
last=mid-1;
else
break;
}
locn=mid+1;
return(target==list[mid]);
}

void main()
{
int a[10],i,s=0,n,loc,flag=0;
clrscr();
cout<<"\n Enter the no. of element to store:\n";
cin>>n;
cout<<"Enter the Elements:\n";
for(i=0;i<n;i++)
cin>>a[i];

cout<<"\n The Elements are:\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";

cout<<"\n Enter the Element to search:\n";
cin>>s;

if(binarysearch(a,n,s,&loc))
cout<<"\nThe element "<<s<< " is available at location"<<loc<<endl;
else
cout<<"\nThe element "<<s<< " is not found in the List"<<endl;

}
8. Write programs for Bubble Sort, Quick sort
Program for bubble sort, Quick sort :

//PROGRAM FOR BUBBLE SORT
#include<stdio.h>
#include<conio.h>
#define SIZE 5
void main()
{
int a[SIZE],n,i,j,temp;
clrscr();
printf("enter the elements ");
for(i=0;i<SIZE;i++)
scanf("%d",&a[i]);
printf("the sorted list is :->\n");
for(i=0;i<SIZE;i++)
for(j=i;j<SIZE-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

for(i=0;i<SIZE;i++)
printf("%d",a[i]);
getch();
}

/*QUICK SORT*/
#include<stdio.h>
#include<conio.h>

int split(int [],int,int);
void quicksort(int [],int,int);

void main()
{
int arr[20],n,i;
clrscr();
printf("\nQUICk SORT\n");
printf("Enter the no.of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\nArray before sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
quicksort(arr,0,n);
printf("\nArray after sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
getch();
}


void quicksort(int a[],int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(a,lower,upper);
quicksort(a,lower,i-1);
quicksort(a,i+1,upper);
}
}

int split(int a[],int lower,int upper)
{
int i,p,q,t;
p=lower+1;
q=upper;
i=a[lower];
while(q>=p)
{
while(a[p]<i)
p++;
while(a[q]>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[lower];
a[lower]=a[q];
a[q]=t;
return(q);
}

9. Explain about the types of linked lists
Types of linked lists :

Singly linked list: It has only head part and corresponding references to the next nodes.
Doubly linked list: A linked list which both head and tail parts, thus allowing the
traversal in bi-directional fashion. Except the first node, the head node refers to the
previous node.
Circular linked list: A linked list whose last node has reference to the first node.

10. How would you sort a linked list?
Sort linked list
#include <iostream>
using namespace std;
#define NULL 0
class LinkList
{
private:
struct Node
{
Node* prev;
int value;
Node *next;
};
Node *first;

public :

LinkList()
{
Node *t1 = new Node();
t1->prev = NULL;
t1->value = 4;
t1->next = NULL;
first = t1;

Node *t2 = new Node();
t2->prev = t1;
t2->value = 6;
t2->next = NULL;
t1->next = t2;

Node *t3 = new Node();
t3->prev = t2;
t3->value = 1;
t3->next = NULL;
t2->next = t3;

Node *t4 = new Node();
t4->prev = t3;
t4->value = 5;
t4->next = NULL;
t3->next = t4;

Node *t5 = new Node();
t5->prev = t4;
t5->value = 8;
t5->next = NULL;
t4->next = t5;

Node *t6 = new Node();
t6->prev = t5;
t6->value = 1;
t6->next = NULL;
t5->next = t6;

Node *t7 = new Node();
t7->prev = t6;
t7->value = 0;
t7->next = NULL;
t6->next = t7;
}

~LinkList()
{
Node *temp = first, *current = first;
while(current != NULL)
{
temp = current->next;
delete current;
current = temp;
}

}

void Display()
{
Node *temp;
for(temp = first; temp != NULL; temp = temp->next)
{
cout<<temp->value<<" , ";
}
cout<<endl;
}


void Sort()
{
Node *current, *cur;

for(current = first; current->next != NULL; current = current->next)
{
Node *minimum = current;
for(cur = current ; cur != NULL; cur = cur->next)
{
if(minimum->value > cur->value)
{
minimum = cur;
}
}
if(minimum != current)
{
Node *current_previous, *current_next, *min_previous, *min_next;

// Initialize them
current_next = current->next;
min_previous = minimum->prev;
min_next = minimum->next;
current_previous = current->prev;

if(current_previous == NULL)
{
// Change the First Node
first = minimum;
}
if(current->next == minimum)
{
// Nodes are Adjacent
minimum->prev = current_previous;
minimum->next = current;

current->prev = minimum;
current->next = min_next;

if(min_next)
{
min_next->prev = current;
}
if(current_previous)
{
current_previous->next = minimum;
}
}
else
{
minimum->prev = current_previous;
minimum->next = current_next;

current->prev = min_previous;
current->next = min_next;

if(current_next)
{
current_next->prev = minimum;
}
if(min_previous)
{
min_previous->next = current;
}
if(min_next)
{
min_next->prev = current;
}
if(current_previous)
{
current_previous->next = minimum;
}
}
current = minimum;
}
}

}
};





void main()
{
LinkList list;

cout<<"LinkList = ";

list.Display();

cout<<"\n\nSorting \n\n";

list.Sort();

cout<<"LinkList = ";

list.Display();

}
11. Write the programs for Linked List (Insertion and Deletion) operations
Programs for Linked List operations :

#include<iostream>
#include<stdlib.h>
using namespace std;
#define TRUE 1
#define FALSE 0
#define NULL 0
class AVL;
class AVLNODE
{
friend class AVL;
private:
int data;
AVLNODE *left,*right;
int bf;
};
class AVL
{
private:
AVLNODE *root;
public:
AVLNODE *loc,*par;
AVL()
{
root=NULL;
}
int insert(int);
void displayitem();
void display(AVLNODE *);
void removeitem(int);
void remove1(AVLNODE *,AVLNODE *,int);
void remove2(AVLNODE *,AVLNODE *,int);
void search(int x);
void search1(AVLNODE *,int);
};
int AVL::insert(int x)
{
AVLNODE *a,*b,*c,*f,*p,*q,*y,*clchild,*crchild;
int found,unbalanced;
int d;
if(!root) //special case empty tree
{ y=new AVLNODE;
y->data=x;
root=y;
root->bf=0;
root->left=root->right=NULL;
return TRUE; }
//phase 1:locate insertion point for x.a keeps track of the most
// recent node with balance factor +/-1,and f is the parent of a
// q follows p through the tree.
f=NULL;
a=p=root;
q=NULL;
found=FALSE;
while(p&&!found)
{ //search for insertion point for x
if(p->bf)
{
a=p;
f=q;
}
if(x<p->data) //take left branch
{
q=p;
p=p->left;
}
else if(x>p->data)
{
q=p;
p=p->right;
}
else
{
y=p;
found=TRUE;
}
} //end while
//phase 2:insert and rebalance.x is not in the tree and
// may be inserted as the appropriate child of q.
if(!found)
{
y = new AVLNODE;
y->data=x;
y->left=y->right=NULL;
y->bf=0;
if(x<q->data) //insert as left child
q->left=y;
else
q->right=y; //insert as right child
//adjust balance factors of nodes on path from a to q
//note that by the definition of a,all nodes on this
//path must have balance factors of 0 and so will change
//to +/- d=+1 implies that x is inserted in the left
// subtree of a d=-1 implies
//to that x inserted in the right subtree of a.


if(x>a->data)
{
p=a->right;
b=p;
d=-1;
}
else
{
p=a->left;
b=p;
d=1;
}
while(p!=y)
if(x>p->data) //height of right increases by 1
{
p->bf=-1;
p=p->right;
}
else //height of left increases by 1
{
p->bf=1;
p=p->left;
}
//is tree unbalanced
unbalanced=TRUE;
if(!(a->bf)||!(a->bf+d))
{ //tree still balanced
a->bf+=d;
unbalanced=FALSE;
}
if(unbalanced) //tree unbalanced,determine rotation type
{
if(d==1)
{ //left imbalance
if(b->bf==1) //rotation type LL
{
a->left=b->right;
b->right=a;
a->bf=0;
b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->left=c->right;
c->left=b;
c->right=a;


switch(c->bf)
{
case 1: a->bf=-1; //LR(b)
b->bf=0;
break;
case -1:b->bf=1; //LR(c)
a->bf=0;
break;
case 0: b->bf=0; //LR(a)
a->bf=0;
break;
}
c->bf=0;
b=c; //b is the new root
} //end of LR
} //end of left imbalance
else //right imbalance
{
if(b->bf==-1) //rotation type RR
{
a->right=b->left;
b->left=a;
a->bf=0;
b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->right=c->left;
c->right=b;
c->left=a;
switch(c->bf)
{
case 1: a->bf=-1; //LR(b)
b->bf=0;
break;
case -1:b->bf=1; //LR(c)
a->bf=0;
break;
case 0: b->bf=0; //LR(a)
a->bf=0;
break;
}
c->bf=0;
b=c; //b is the new root
} //end of LR
}
//subtree with root b has been rebalanced and is the new subtree

if(!f)
root=b;
else if(a==f->left)
f->left=b;
else if(a==f->right)
f->right=b;
} //end of if unbalanced
return TRUE;
} //end of if(!found)
return FALSE;
} //end of AVL INSERTION

void AVL::displayitem()
{
display(root);
}
void AVL::display(AVLNODE *temp)
{
if(temp==NULL)
return;
cout<<temp->data<<" ";
display(temp->left);
display(temp->right);
}
void AVL::removeitem(int x)
{
search(x);
if(loc==NULL)
{
cout<<"\nitem is not in tree";
return;
}
if(loc->right!=NULL&&loc->left!=NULL)
remove1(loc,par,x);
else
remove2(loc,par,x);
}
void AVL::remove1(AVLNODE *l,AVLNODE *p,int x)
{
AVLNODE *ptr,*save,*suc,*psuc;
ptr=l->right;
save=l;
while(ptr->left!=NULL)
{
save=ptr;
ptr=ptr->left;
}
suc=ptr;
psuc=save;
remove2(suc,psuc,x);
if(p!=NULL)
if(l==p->left)
p->left=suc;
else
p->right=suc;
else
root=l;
suc->left=l->left;
suc->right=l->right;
return;
}
void AVL::remove2(AVLNODE *s,AVLNODE *p,int x)
{
AVLNODE *child;
if(s->left==NULL && s->right==NULL)
child=NULL;
else if(s->left!=NULL)
child=s->left;
else
child=s->right;
if(p!=NULL)
if(s==p->left)
p->left=child;
else
p->right=child;
else
root=child;

}
void AVL::search(int x)
{
search1(root,x);
}
void AVL::search1(AVLNODE *temp,int x)
{
AVLNODE *ptr,*save;
int flag;
if(temp==NULL)
{
cout<<"\nthe tree is empty";
return;
}
if(temp->data==x)
{
cout<<"\nthe item is root and is found";
par=NULL;
loc=temp;
par->left=NULL;
par->right=NULL;
return; }
if( x < temp->data)
{
ptr=temp->left;
save=temp;
}
else
{
ptr=temp->right;
save=temp;
}
while(ptr!=NULL)
{
if(x==ptr->data)
{ flag=1;
cout<<"\nitemfound";
loc=ptr;
par=save;

}
if(x<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
if(flag!=1)
{
cout<<"item is not there in tree";
loc=NULL;
par=NULL;
cout<<loc;
cout<<par;
}
}

main()
{
AVL a;
int x,y,c;
char ch;
do
{
cout<<"\n1.insert";
cout<<"\n2.display";
cout<<"\n3.delete";
cout<<"\n4.search";
cout<<"\n5.exit";
cout<<"\nEnter u r choice to perform on AVL tree";
cin>>c;


switch(c)
{
case 1:cout<<"\nEnter an element to insert into tree";
cin>>x;
a.insert(x);
break;
case 2:a.displayitem(); break;
case 3:cout<<"\nEnter an item to deletion";
cin>>y;
a.removeitem(y);
break;
case 4:cout<<"\nEnter an element to search";
cin>>c;
a.search(c);
break;
case 5:exit(0); break;
default :cout<<"\nInvalid option try again";
}
cout<<"\ndo u want to continue";
cin>>ch;
}
while(ch=='y'||ch=='Y');
}

12. What data structure would you mostly likely see in a non recursive implementation of
a recursive algorithm?
data structure would you most likely see in a non recursive implementation of a
recursive algorithm :

Stack
13. What do you mean by Base case, Recursive case, Binding Time, Run-Time Stack and
Tail Recursion?
These terms are found in Recursion.
Base Case: it is the case in recursion where the answer is known, or we can say the
termination condition for a recursion to unwind back.
For example to find Factorial of num using recursion:
int Fact(int num){
if(num==1 || num==0)//base case return 1; else // recursive case: return num*Fact(num-
1); }.
Recursive case: It is the case which brings us to the closer answer.

Run Time Stack: It is a system stack us to save the frame stack of a function every
recursion or every call. This frame stack consists of the return address, local variables and
return value if any.

Tail Recursion: The case where the function consist of single recursive call and it is the
last statement to be executed. A tail Recursion can be replace by iteration. The above
function consists of tail recursion case. whereas the below function does not.
void binary(int start,int end,int el){ int mid; if(end>start){ mid=(start+end)/2;
if(el==ar[mid]) return mid; else{ if(el>ar[mid]) binary(mid+1,end,ele); else
binary(start,mid-11,ele); } } }

14. Explain quick sort and merge sort algorithms and derive the time-constraint
relation for these.
Quick sort algorithm

function quicksort(array)
var list less, greater
if length(array) 1
return array
select and remove a pivot value pivot from array
for each x in array
if x pivot then append x to less
else append x to greater
return concatenate(quicksort(less), pivot, quicksort(greater))

Merge sort

function mergesort(m)
var list left, right, result
if length(m) 1
return m
else
var middle = length(m) / 2
for each x in m up to middle - 1
add x to left
for each x in m at and after middle
add x to right
left = mergesort(left)
right = mergesort(right)
if last(left) first(right)
append right to left
return left
result = merge(left, right)
return result

function merge(left,right)
var list result
while length(left) > 0 and length(right) > 0
if first(left) first(right)
append first(left) to result
left = rest(left)
else
append first(right) to result
right = rest(right)
if length(left) > 0
append rest(left) to result
if length(right) > 0
append rest(right) to result
return result

Time constraint for merge sort :

Suppose (for simplicity) that n = 2
k
for some entire k. Let T(n) the time used to sort n elements.
As we can perform separation and merging in linear time, it takes cn time to perform these two
steps, for some constant c. So,
T(n) = 2T(n/2) + cn.
In the same way:
T(n/2) = 2T(n/4) + cn/2, so

T(n) = 4T(n/4) + 2cn.
Going in this way ...
T(n) = 2
m
T(n/2
m
) + mcn, and

T(n) = 2
k
T(n/2
k
) + kcn = nT(1) + cnlog
2
n = O(n log n).
Remember, as n=2
k
k = log
2
n!
The general case requires a bit more work, but it takes O(n log n) time anyway.

Time constraint for Quick sort :

O(n
2
)

15. Explain binary searching, Fibinocci search.
Binary search and Fibinocci search :

Binary Search:
o Binary search is the process of locating an element in a sorted list. The search
starts by dividing the list into two parts.
The algorithm compares the median value. If the search element is less than the median
value, the top list only will be searched, after finding the middle element of that list.
The process continues until the element is found or the search in the top list is completed.
The same process is continued for the bottom list, until the element is found or the search
in the bottom list is completed. If an element is found that must be the median value.
Fibonacci Search:
Fibonacci search is a process of searching a sorted array by utilizing divide and conquer
algorithm.
Fibonacci search examines locations whose addresses have lower dispersion.
When the search element has non-uniform access memory storage, the Fibonacci search
algorithm reduces the average time needed for accessing a storage location.

16. What is the maximum total number of nodes in a tree that has N levels? Note that the
root is level (zero)
If root is at level 0 then :
Case 0:
When level is 1 max nodes is 1
Case 1:
When level is 1 then max would be 3.
Case 2:
When level is 2 then max nodes would be 7

So formula would be 2^(n+1) -1

2^(0+1)-1=1
2^(1+1)-1=3
2^(2+1)-1=7
17. How many different binary trees and binary search trees can be made from three
nodes that contain the key values 1, 2 & 3?
Number of Binary search tree -- catalan number - (2n)C(n) /(n+1)
Number of Binary trees - catalan number * n! = 2n ! / (n+1) !
6 binary trees, 3 binary search tree
18. A list is ordered from smaller to largest when a sort is called. Which sort would take the
longest time to execute?
A list is ordered from smaller to largest when a sort is called Which sort would take the longest
time to execute -- Quick Sort
19. A list is ordered from smaller to largest when a sort is called. Which sort would take the
shortest time to execute?
A list is ordered from smaller to largest when a sort is called Which sort would take the shortest
time to execute Insertion Sort

20. When will you sort an array of pointers to list elements, rather than sorting the
elements themselves?
If the Data structures that we are sorting are big and located at different places then we prefer
sorting pointers rather than data itself .

21. The element being searched for is not found in an array of 100 elements. What is the
average number of comparisons needed in a sequential search to determine that the element
is not there, if the elements are completely unordered?
Avarage number of comparision will be (N+1)/2(N-size of array).Because:If elements is in 1st
position no of cpmparision will be one and if the element is in the last position then no of
comparisions will be N.

22. What is the average number of comparisons needed in a sequential search to determine
the position of an element in an array of 100 elements, if the elements are ordered from
largest to smallest?
Avarage number of comparision will be (N+1)/2(N-size of array).Because:If elements is in 1st
position no of cpmparision will be one and if the element is in the last position then
no of comparisions will be N.

23. Which sort show the best average behavior?
quick sort shows the best average behavior.

24. What is the average number of comparisons in a sequential search?
The average number of comparisions in a sequential search is (N+1)/2
f(n)= 1.Pn + 2.Pn + 3.Pn +...+ N.Pn
where
Pn = 1/N
f(n)= 1.1/N +2.1/N + 3.1/N+....+N.1/N
= (1+2+3+....+N)1/N
= N(N+1)/2N
= (N+1)/2

25. Which data structure is needed to convert infix notations to post fix notations?
Stack is data structure needed to convert infix notation to postfix notation.

//C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX
EXPRESSION USING STACKS.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
char stack[MAX];
int top=-1;
char pop();
void push(char item);
int prcd(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
break;
case '*':
case '/':return 4;
break;
case '^':
case '$':return 6;
break;
case '(':
case ')':
case '#':return 1;
break;
}
}
int isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '(':
case ')':return 1;
break;
default:return 0;
}
}
void convertip(char infix[],char postfix[])
{
int i,symbol,j=0;
stack[++top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else{
if(symbol=='(')push(symbol);
else if(symbol==')')
{
while(stack[top]!='(')
{
postfix[j]=pop();
j++;
}
pop();//pop out (.
}
else{
if(prcd(symbol)>prcd(stack[top]))
push(symbol);
else{
while(prcd(symbol)<=prcd(stack[top]))
{
postfix[j]=pop();
j++;
}
push(symbol);
}//end of else.
}//end of else.
}//end of else.
}//end of for.
while(stack[top]!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';//null terminate string.
}
void main()
{
char infix[20],postfix[20];
clrscr();
printf("Enter the valid infix string:\n");
gets(infix);
convertip(infix,postfix);
printf("The corresponding postfix string is:\n");
puts(postfix);
getch();
}
void push(char item)
{
top++;
stack[top]=item;
}
char pop()
{
char a;
a=stack[top];
top--;
return a;
}


26. What do you mean by:
o Syntax Error
o Logical Error
o Runtime Error
How can you correct these errors?

Syntax errors -- Errors in spelling and grammar.You can use the compiler or interpreter to
uncover syntax errors.You must have a good working knowledge of error messages to discover
the cause of the error.
Logical errors -- Errors that indicate the logic used when coding the program failed to solve the
problem.You do not get error messages with logic errors.Your only clue to the existence of logic
errors is the production of wrong solutions.

Runtime Error occurs at run-time.Such error cause a program to end abrubtly or even cause
system shut-down.Such errors are hard to detect.
e.g. Error caused due to low system memory.

27. In which data structure, elements can be added or removed at either end, but not in the
middle?
A double ended queue (deque) is a linear list in which elements can be added or removed at
either end but not in the middle..it is the contraction of name double ended queue..

28. How will inorder, preorder and postorder traversals print the elements of a tree?
Preorder ,postorder ,inorder traversals

void inorder(node * root)
{
if(root!= NULL)
{
inorder(root->leftchild);
printf("%d ",root->data);
inorder(root->rightchild);
}
else
return;

}

void postorder(node * root)
{
if(root!= NULL)
{
postorder(root->leftchild);
postorder(root->rightchild);
printf("%d ",root->data);
}
else
return;
}

void preorder(node * root)
{
if(root!= NULL)
{
printf("%d ",root->data);
preorder(root->leftchild);
preorder(root->rightchild);
}
else
return;

}
in order:LOR

pre order:OLR

post order:LRO

L=LEFT CHILD
R=RIGHT CHILD
O=ROOT NODE


29. Parenthesis are never needed in prefix or postfix expressions. Why?
Because there is not an "order of operations" in prefix or postfix notation. The order in which
you put the numbers and operators is the order in which calculation occurs.

Parenthesis are used to define the sequence or priority of execution of operations which is useful
in infix notation. Whereas we have already taken the consideration of order of execution of
operands during the time of conversion to prefix or postfix form.These orders are maintained
with the help of Stack
i.e. Last in First out. So we donot need parenthesis in
prefix and postfix notions.

30. Which one is faster? A binary search of an ordered set of elements in an array or a
sequential search of the elements.
Faster :- Binary search vs Sequential search
The binary search is faster than the sequential search.Thecomplexity of binary search is 'log n'
where as thecomplexity of sequential search is 'n'.Since each time weare proceeding we have to
deal with only half of the elements of the array than the previous one.So we can easily get a
number from an array of elements through binary search than sequential search.

You might also like