Al Murtuza Arabi 2212074642
Al Murtuza Arabi 2212074642
Al Murtuza Arabi 2212074642
Ans:
We know that, in BST the max number of nodes in any level is 2^n.
As such - we get in ;
In the 1st level -2^0;
In the 2nd level -2^1;
In the 3rd level -2^2;
………………
In the h level - 2^h;
So,
n+1=2^(h+1).
Log₂(n+1)=log₂2^(h+1).
h+1=Log₂(n+1).
And h= [Log₂(n+1)-1] (ans)
6. Explain what a complete binary tree is, and what a full binary tree is. Give
examples.
Ans:
A complete binary tree is a binary tree where all the levels are filled completely
except the lowest level nodes which are filled from left as possible.
Example:
F
B G
A C H
In a full Binary tree every node has either no children (a leaf node) or two children.
Example:
1
2 3
4 4
5. Discuss the differences between the Stack and Queue data structures, from your
understanding.
Give a few real-life examples of when to use which data structure.?
Ans:
Stack Queue
The working principle is that the The working principle is that the
element that is inserted last will come element that is inserted first will come
out first.LIFO method out first.FIFO method
It has only one end,called top.insertion It has two ends, the rear and front. The
and deletion take place at top rear end is used for inserting whereas
the front end is used for deleting.
1. In a call center, customers call and their requests are added to a queue.
2. Documents in a printer are stored in a queue until the printer is ready to print .
3. The operating system handles its tasks in FIFO manner.
4. Why is the Mod operator used in “Enqueue” operation of Queue data structure?
Ans:
The modulus operator is used so that the queue can be represented by an
array.This technique is referred to as a circular queue.
rear = (rear +1) % maxQue; this line of code ensures that the index rear stays within
the bounds of the array and helps it wrap around.
The mod operator works as the dividend exceeds the value of the divider the
remainder becomes 1. Hence you keep moving circularly, from 0 to n-1 and then to
zero.
3.Implement a circular linked list, using the knowledge from Singly Linked list
implementation.?
#include <iostream>
class node {
public :
int data;
node* next;
node(int val)
{
data=val;
next=NULL;
}
};
void insetathead(node* &head,int val)
{
node* n=new node(val);
if(head==NULL)
{
n->next=n;
head=n;
}
node *temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=n;
n->next=head;
head=n;
}
if(head==NULL)
{
insetathead(head,val);
return;
}
node* n=new node(val);
node *temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=n;
n->next=head;
}
int main()
{
node * head=NULL;
insertattail(head,1);
insertattail(head,2);
insertattail(head,3);
insertattail(head,4);
insetathead(head,5);
display(head);
return 0;
}
while (temp) {
if (temp->data == item) {
found = true;
break;
}
temp = temp->next;
}
}
The implementation.
int main() {
SortedList list1;
list1.InsertItem(3);
list1.InsertItem(1);
list1.InsertItem(2);
int itemtofind = 7;
bool found = false;
list1.RetrieveItem(itemtofind, found);
if (found) {
cout<<"Item was found"<<endl;
} else {
cout<<"Item was not found"<<endl;
int itemtodelete = 1;
list1.DeleteItem(itemtodelete);
For deletion , since we need to traverse the entire list to find the item to delete in the
worst case . The time complexity is O(N).
For retrieving , the worst-case time complexity is also O(N) .
8. The In-Order and Pre-Order traversal of a Binary Search tree is given. Construct
the tree. (15)
In-order traversal: 77, 90, 126, 127, 129, 137, 142, 150, 199, 278, 282, 287, 291,
300, 309
Pre-Order traversal: 199, 90, 77, 126, 129, 127, 150, 137, 142, 278, 282, 291, 287,
300, 309
The tree ;
199
90 278
77 126 …… 282
…… 129 ….. 291
127 150 287 300
137 …. . ….. 309
…. 142
The Post-order:77,127,142,137,150,129,126,90,287,309,300,291,282,278,199