L1F22BSCS0422 Dsa CP3
L1F22BSCS0422 Dsa CP3
L1F22BSCS0422
D-16
Question#1:
#include <iostream>
using namespace std;
int largest = i;
int left = 2 * i;
int right = 2 * i + 1;
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);
heapify(arr, i, 0);
}
}
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;
int sum = 0;
if (n->data > N)
{
sum += n->data;
}
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);
}