0% found this document useful (0 votes)
5 views16 pages

Umer Major

Uploaded by

Daud Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Umer Major

Uploaded by

Daud Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Q1

Answer:

Tree 1
In-order: D G B E H I F C A
Pre-order: A B D G E C F H I
Post-order: G D E H I F C B A

Tree 2
In-order: 5 4 8 11 10 17 19 31 43 49
Pre-order: 11 4 5 8 19 17 10 31 43 49
Post-order: 5 4 8 10 17 31 43 49 19 11

Tree 3
In-order: I D J B F G H K C A
Pre-order: A B D I J C F G H K
Post-order: I J D B F K H G C A
Q2
Answer:
Steps for constructing the Binary Search Tree:
1. Start with Pre-order Traversal: The first element of the pre-order
traversal is the root of the tree. From the given pre-order sequence,
we know that the root of the tree is 12.

2. Divide the In-order Traversal:


Elements to the left of 12 in the in-order sequence represent the left
subtree: 4, 8, 10.
Elements to the right of *12* in the in-order sequence represent the
right subtree: *14, 20, 22*.

3. Build the Left Subtree:


From the pre-order traversal, after 12, the next element is 8, which is
the root of the left subtree.
Use the same process to find the left and right children of 8 by
dividing the in-order sequence 4, 8, 10:
Left child of 8 is 4 (only one element on the left side).
Right child of 8 is 10 (one element on the right side).

4. Build the Right Subtree:


From the pre-order traversal, after *8, the next element is **20*,
which is the root of the right subtree.
Using the in-order sequence *14, 20, 22*, we can deduce that:
Left child of *20* is *14* (only one element on the left side).
Right child of *20* is *22* (only one element on the right side).

Final Tree:

12
/ \
8 20
/\ /\
4 10 14 22

Q.3
Answer:

Min-Heap Implementation

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class MinHeap {
private:
vector<int> heap;

void heapifyUp(int index) {


while (index > 0 && heap[index] < heap[(index - 1) / 2]) {
swap(heap[index], heap[(index - 1) / 2]);
index = (index - 1) / 2;
}
}

void heapifyDown(int index) {


int size = heap.size();
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;
int smallest = index;

if (leftChild < size && heap[leftChild] < heap[smallest])


smallest = leftChild;

if (rightChild < size && heap[rightChild] < heap[smallest])


smallest = rightChild;

if (smallest != index) {
swap(heap[index], heap[smallest]);
heapifyDown(smallest);
}
}

public:
void insert(int value) {
heap.push_back(value);
heapifyUp(heap.size() - 1);
display();
}

void deleteMin() {
if (heap.empty()) return;
swap(heap[0], heap[heap.size() - 1]);
heap.pop_back();
heapifyDown(0);
display();
}

void display() {
for (int val : heap) cout << val << " ";
cout << endl;
}
};

int main() {
MinHeap minHeap;
minHeap.insert(3);
minHeap.insert(2);
minHeap.insert(15);
minHeap.deleteMin();
}

Max-Heap Implementation:
cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class MaxHeap {
private:
vector<int> heap;

void heapifyUp(int index) {


while (index > 0 && heap[index] > heap[(index - 1) / 2]) {
swap(heap[index], heap[(index - 1) / 2]);
index = (index - 1) / 2;
}
}

void heapifyDown(int index) {


int size = heap.size();
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;
int largest = index;
if (leftChild < size && heap[leftChild] > heap[largest])
largest = leftChild;

if (rightChild < size && heap[rightChild] > heap[largest])


largest = rightChild;

if (largest != index) {
swap(heap[index], heap[largest]);
heapifyDown(largest);
}
}

public:
void insert(int value) {
heap.push_back(value);
heapifyUp(heap.size() - 1);
display();
}

void deleteMax() {
if (heap.empty()) return;
swap(heap[0], heap[heap.size() - 1]);
heap.pop_back();
heapifyDown(0);
display();
}

void display() {
for (int val : heap) cout << val << " ";
cout << endl;
}
};

int main() {
MaxHeap maxHeap;
maxHeap.insert(3);
maxHeap.insert(2);
maxHeap.insert(15);
maxHeap.deleteMax();
}

Q.4
Answer:

Insertion Sort

cpp
#include <iostream>
using namespace std;

void insertionSort(int arr[], int n) {


for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

void display(int arr[], int n) {


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
display(arr, n);
}

Bubble Sort

cpp
#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}

void display(int arr[], int n) {


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
display(arr, n);
}

Heap Sort
#include <iostream>
using namespace std;

void heapify(int arr[], int n, int i) {


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;

if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void heapSort(int arr[], int n) {


for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}

for (int i = n - 1; i > 0; i--) {


swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}

void display(int arr[], int n) {


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
display(arr, n);
}
Quick Sort

#include <iostream>
using namespace std;

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = low - 1;

for (int j = low; j < high; j++) {


if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

void display(int arr[], int n) {


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
display(arr, n);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy