0% found this document useful (0 votes)
7 views13 pages

DS Lab 09

Uploaded by

abuzarali1124
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)
7 views13 pages

DS Lab 09

Uploaded by

abuzarali1124
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/ 13

DS LAB 09

Name: Abuzar Ali Roll#: 23K-0819

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;
};

Node *createNode(int key)


{
Node *node = new Node();
node->key = key;
node->left = node->right = nullptr;
node->height = 1;
return node;
}

int getHeight(Node *node)


{
return node ? node->height : 0;
}

int getBalance(Node *node)


{
return node ? getHeight(node->left) - getHeight(node->right) : 0;
}

int max(int a, int b)


{
return (a > b) ? a : b;
}

Node *rightRotate(Node *y)


{
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
return x;
}

Node *leftRotate(Node *x)


{
Node *y = x->right;
Node *T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return y;
}

Node *insertNode(Node *node, int key)


{
if (node == nullptr)
return createNode(key);

if (key < node->key)


node->left = insertNode(node->left, key);
else if (key > node->key)
node->right = insertNode(node->right, key);
else
return node;

node->height = 1 + max(getHeight(node->left), getHeight(node-


>right));
int balance = getBalance(node);

if (balance > 1 && key < node->left->key)


return rightRotate(node);

if (balance < -1 && key > node->right->key)


return leftRotate(node);
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}

if (balance < -1 && key < node->right->key)


{
node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

Node *minValueNode(Node *node)


{
Node *current = node;
while (current->left != nullptr)
current = current->left;
return current;
}
Node *deleteNode(Node *root, int key)
{
if (root == nullptr)
return root;

if (key < root->key)


root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else
{
if (root->left == nullptr || root->right == nullptr)
{
Node *temp = root->left ? root->left : root->right;
if (temp == nullptr)
{
temp = root;
root = nullptr;
}
else
*root = *temp;
delete temp;
}
else
{
Node *temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
}

if (root == nullptr)
return root;

root->height = 1 + max(getHeight(root->left), getHeight(root->right));


int balance = getBalance(root);

if (balance > 1 && getBalance(root->left) >= 0)


return rightRotate(root);

if (balance > 1 && getBalance(root->left) < 0)


{
root->left = leftRotate(root->left);
return rightRotate(root);
}
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);

if (balance < -1 && getBalance(root->right) > 0)


{
root->right = rightRotate(root->right);
return leftRotate(root);
}

return root;
}

void preOrder(Node *root)


{
if (root != nullptr)
{
cout << root->key << " ";
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(Node *root)
{
if (root != nullptr)
{
inOrder(root->left);
cout << root->key << " ";
inOrder(root->right);
}
}

void postOrder(Node *root)


{
if (root != nullptr)
{
postOrder(root->left);
postOrder(root->right);
cout << root->key << " ";
}
}

Node *search(Node *root, int key)


{
if (root == nullptr || root->key == key)
return root;

if (key < root->key)


return search(root->left, key);
return search(root->right, key);
}

int findKthSmallest(Node *root, int &k)


{
if (root == nullptr)
return -1;

int left = findKthSmallest(root->left, k);


if (left != -1)
return left;

k--;
if (k == 0)
return root->key;

return findKthSmallest(root->right, k);


}
int findKthLargest(Node *root, int &k)
{
if (root == nullptr)
return -1;

int right = findKthLargest(root->right, k);


if (right != -1)
return right;

k--;
if (k == 0)
return root->key;

return findKthLargest(root->left, k);


}

int main()
{
Node *root = nullptr;

int keys[] = {1, 2, 3, 4, 5, 6, 7};


for (int key : keys)
{
root = insertNode(root, key);
}

cout << "Preorder traversal before deletion: ";


preOrder(root);
cout << "\n";
cout << "Inorder traversal before deletion: ";
inOrder(root);
cout << "\n";
cout << "Postorder traversal before deletion: ";
postOrder(root);
cout << "\n";

root = deleteNode(root, 3);

cout << "Preorder traversal after deletion: ";


preOrder(root);
cout << "\n";
cout << "Inorder traversal after deletion: ";
inOrder(root);
cout << "\n";
cout << "Postorder traversal after deletion: ";
postOrder(root);
cout << "\n";

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";

You might also like