Al Murtuza Arabi 2212074642

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

7. If the number of nodes in a BST is “n”, calculate its height.

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;

After adding them 2^0 +2^1+ 2^2+.........+2^h= 2^(h+1) -1

So the relation we get for height h the number of nodes n =2^(h+1) -1

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

Full binary tree:

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

The insertion operation is known as The insertion operation is known as


push and the deletion operation is enqueue and the deletion operation is
known as pop. known as dequeue.

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.

It is used to solve recursion-based It is used to solve sequential


problems. processing-based problems.

It has a simple Implementation.Only one Two-pointers are used.Complex


pointer is used. implementation.

Real life applications of Stack:

1. For parentheses checking we use Stack


2. For Undo redo operations in applications we use stack
3. Recursive problems / methods are allocated in the memory as stacks
4. The history of a web browser is also stacked
Real life applications of Queue:

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.?

Ans: the implementation with some functions are_

#include <iostream>

using namespace std;

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;
}

void display(node* head)


{
node *temp=head;
do
{
cout<<temp->data<<"";
temp=temp->next;
}while(temp!=head);
cout<<endl;
}

void insertattail(node* &head,int val)


{

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;
}

Ans to the question no 1:

void DeleteItem(int item) {


Node* temp = head;
Node* prev = nullptr;

while (temp && temp->data != item) {


prev = temp;
temp = temp->next;
}
if (temp) {
if (prev) {
prev->next = temp->next;
} else {
head = temp->next;
}
delete temp;
}
}

void RetrieveItem(int& item, bool& found) {


Node* temp = head;
found = false;

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) .

Ans to the question no 2:

For certain uses a tail pointer in a linked list is helpful.Such as-


1.Assuming we want to insert and delete nodes at the tail of a list more frequently, it
is certainly a wise choice to keep a tail node .Then inserting at the end becomes
O(1) instead of O(N).
2.In a circular linked list a tail pointer makes it easy to maintain and update data at
the end.
3.In the case of doubly linked lists,a tail pointer can help operations such as
deleting.

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

You might also like