Professional Documents
Culture Documents
RP21 Ee 407
RP21 Ee 407
CODE:
#include <iostream>
Program 2
Write a program that reads a list of integers provided by the user and builds a Heap tree
from them. Then, prompt the user to enter a value and search for it in the tree. If the value is
found, print its path in the tree; otherwise, print a message indicating that the value is not in
the tree.
CODE:
#include <iostream>
#include <vector>
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, largest);
}
}
int main() {
vector<int> arr;
int n;
cout << "Enter the number of integers: ";
cin >> n;
cout << "Enter " << n << " integers:\n";
for (int i = 0; i < n; i++) {
int x;
cin >> x;
arr.push_back(x);
}
buildHeap(arr);
int x;
cout << "Enter a value to search for: ";
cin >> x;
int index;
if (search(arr, x, index)) {
cout << x << " is in the tree.\n";
cout << "Path to " << x << ": ";
printPath(arr, index);
} else {
cout << x << " is not in the tree.\n";
}
return 0;
}
Program 3
Implement a function to determine if a binary tree is a Heap tree. The function should take
the root node of the tree as its input and return a boolean value indicating whether or not the
tree is a valid heap
CODE:
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 8, 9, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
int choice;
cin >> choice;
switch (choice) {
case 1:
if (isHeap(arr, n)) {
cout << "The binary tree is a heap\n";
} else {
cout << "The binary tree is not a heap\n";
}
break;
case 2:
return 0;
default:
cout << "Invalid choice\n";
}
return 0;
}
Program 4
Implement a function to find the height of a heap tree. The function should take the root
node of the tree as its input and return the height of the tree as an integer value.
CODE:
#include <iostream>
#include <cmath>
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
int height = 0;
int i = 0;
while (true) {
int next = (i << 1) + 1;
if (next >= n) {
break;
}
i = next;
height++;
}
return height;
}
int main() {
int n;
cout << "Enter the number of integers: ";
cin >> n;
int arr[n];
cout << "Enter the integers: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
buildHeap(arr, n);
return 0;
}
Program 5
Write a program that reads a list of integers from provided by the user and builds a Heap tree
from them. Then, prompt the user to enter a range of values (e.g., "Enter a lower bound and
an upper bound: "). The program should print all the values in the tree that fall within the
specified range
CODE;
#include <iostream>
#include <algorithm>
#include <vector>
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void buildHeap(vector<int>& arr, int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
}
int main() {
int n;
cout << "Enter the number of integers: ";
cin >> n;
vector<int> arr(n);
cout << "Enter the integers: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
buildHeap(arr, n);
cout << "Values within the range [" << lower << ", " << upper << "]: ";
printRange(arr, n, lower, upper);
return 0;
}
Program 6
Implement a function to delete a node from a Heap tree. The function should take the root
node of the tree and the value to be deleted as its input, and return the new root node of the
tree
CODE:
#include <iostream>
#include <algorithm>
arr[i] = arr[n-1];
n--;
heapify(arr, n, i);
}
int main() {
int arr[] = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}