DS Lab 09
DS Lab 09
Task 01-05:
/*1. Implement the following insertions in the AVL tree (1,2,3,4,5,6,7)
2. Delete value 3 from the tree and balance it.
3. Do a pre-order, in order and post-order traversal of the tree before
deletion and after deletion.
4. Search for any value in the tree if it is present print, it with its index
(key) value otherwise inserts it into the tree and balances it with the
appropriate rotations.
5. Find the kth smallest and largest value in the AVL tree and print its
key also print both the left side and right side height of the tree starting
from root.*/
#include <iostream>
using namespace std;
class Node
{
public:
int key;
Node *left;
Node *right;
int height;
};
return node;
}
if (root == nullptr)
return root;
return root;
}
k--;
if (k == 0)
return root->key;
k--;
if (k == 0)
return root->key;
int main()
{
Node *root = nullptr;
int searchValue = 5;
Node *found = search(root, searchValue);
if (found != nullptr)
{
cout << "Found " << searchValue << " in the tree.\n";
}
else
{
root = insertNode(root, searchValue);
cout << searchValue << " not found. Inserted into the tree.\n";
}
int k = 3;
cout << k << "rd smallest element is " << findKthSmallest(root, k) <<
"\n";
k = 3;
cout << k << "rd largest element is " << findKthLargest(root, k) << "\
n";
cout << "Left subtree height: " << getHeight(root->left) << "\n";
cout << "Right subtree height: " << getHeight(root->right) << "\n";