0% found this document useful (0 votes)
8 views9 pages

L1F22BSCS0422 Dsa CP3

The document contains C++ code for implementing heap sort and a binary search tree (BST). It includes functions for heapifying an array, sorting it, and performing various tree operations such as insertion, traversal (in-order, pre-order, post-order), and calculating the sum of nodes greater than a specified value. Additionally, it features a main function that demonstrates the usage of these data structures and algorithms.

Uploaded by

hjamshaid81
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)
8 views9 pages

L1F22BSCS0422 Dsa CP3

The document contains C++ code for implementing heap sort and a binary search tree (BST). It includes functions for heapifying an array, sorting it, and performing various tree operations such as insertion, traversal (in-order, pre-order, post-order), and calculating the sum of nodes greater than a specified value. Additionally, it features a main function that demonstrates the usage of these data structures and algorithms.

Uploaded by

hjamshaid81
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/ 9

Hassan Jamshaid

L1F22BSCS0422

D-16

Question#1:

#include <iostream>
using namespace std;

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


{

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

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; i >= 0; i--)
heapify(arr, n, i);

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


{
swap(arr[0], arr[i]);

cout << "Max Heap: ";


cout << arr[i] << endl;

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[] = { 25, 40, 35, 20, 30, 50 };
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
cout << "Sorted array is \n";
display(arr, n);
}

Question#2:

#include<iostream>
using namespace std;

struct node
{
int data;
node* left;
node* right;
public:
node(int d)
{
this->data = d;
this->left = NULL;
this->right = NULL;
}
};

class BST
{
node* root;

node* in(node* n, int k)


{
if (n == NULL)
{
return new node(k);
}
if (k < n->data)
{
n->left = in(n->left, k);
}
else
{
n->right = in(n->right, k);
}
return n;
}
void IOR(node* n)
{
if (n != NULL)
{
IOR(n->left);
cout << n->data << " ";
IOR(n->right);
}
}
void PRE(node* n)
{
if (n != NULL)
{
cout << n->data << " ";
PRE(n->left);
PRE(n->right);
}
}
void POS(node* n)
{
if (n != NULL)
{
POS(n->left);
POS(n->right);
cout << n->data << " ";
}
}
void ip(node* &n)
{
int data;
cout << "Enter Root:" << endl;
cin >> data;
cout << "Enter Data in Tree:" << endl;
while (data != -1)
{
n = in(n, data);
cin >> data;
}
}
bool search(node* n, int k)
{
if (n == NULL)
{
return false;
}
if (n->data == k)
{
return true;
}
else if (k < n->data)
{
return search(n->left,k);
}
else
{
return search(n->right, k);
}
}

int sumGreaterThanN(node* n, int N)


{
if (n == NULL)
{
return 0;
}

int sum = 0;

if (n->data > N)
{
sum += n->data;
}

sum += sumGreaterThanN(n->left, N);


sum += sumGreaterThanN(n->right, N);

return sum;
}
public:
BST()
{
root = NULL;
}
void insert(int d)
{
root = in(root, d);
}
void inorder()
{
IOR(root);
}
void input()
{
ip(root);
}
void preorder()
{
PRE(root);
}
void postorder()
{
POS(root);
}
bool searchBST(int d)
{
return search(root, d);
}
int sum(int N)
{
int x;
x = sumGreaterThanN(root, N);
return x;
}
};

int main()
{
BST b;
b.input();
cout << "IN ORDER:" << endl;
b.inorder();
cout << endl;
cout << "PRE ORDER:" << endl;
b.preorder();
cout << endl;
cout << "POST ORDER:" << endl;
b.postorder();
cout << endl;
int x;
cout << "Enter N to find sum of nodes:" << endl;
cin >> x;
cout << "Sum of nodes greater than" << x << "=" << b.sum(x);
}

DRY RUN IS ATTACHED WITH THIS FILE

You might also like