DSA Assignment
DSA Assignment
Submitted By
Areeba Noor 22-Arid-4353
Ayesha Fayaz 22-Arid-4354
Khansa Rehman 22-Arid-4368
Mahnoor Nadeem 22-Arid-4369
Submitted To
Sir Ashar
1
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the
difference between heights of left and right subtrees for any node cannot be
more than one.
The difference between the heights of the left subtree and the right subtree for any
node is known as the balance factor of the node.
The AVL tree is named after its inventors, Georgy Adelson-Velsky and Evgenii
Landis, who published it in their 1962 paper “An algorithm for the organization of
information”.
The above tree is AVL because the differences between the heights of left and right
subtrees for every node are less than or equal to 1.
2
Operations On AVL Trees
Insertion
Deletion
Selection[ It is similar to performing a search in BST]
#include<iostream>
using namespace std;
class Node
{
public:
int key;
Node *left;
Node *right;
int height;
};
int height(Node *N)
{
if (N == NULL)
return 0;
return N->height;
}
int max(int a, int b)
{
return (a > b)? a : b;
}
Node* newNode(int key)
{
Node* node = new Node();
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return(node);
}
Node *rightRotate(Node *y)
{
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left),
height(y->right)) + 1;
3
x->height = max(height(x->left),
height(x->right)) + 1;
return x;
}
y->left = x;
x->right = T2;
x->height = max(height(x->left),
height(x->right)) + 1;
y->height = max(height(y->left),
height(y->right)) + 1;
return y;
}
4
node->left = leftRotate(node->left);
return rightRotate(node);
}
return node;
}
int main()
{
Node *root = NULL;
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
cout << "Preorder traversal of the " "constructed AVL tree is \n";
preOrder(root);
return 0;
}
Output
Preorder traversal of the constructed AVL tree is
30 20 10 25 40 50
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
6
// rotate subtree rooted with x
// See the diagram given above.
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
7
if (balance < -1 && key > node->right->key)
return leftRotate(node);
else
{
// node with only one child or no child
if( (root->left == NULL) | |(root->right == NULL) )
8
Node *temp = root->left ?
root->left :
root->right;
// No child case
if (temp == NULL)
{
temp = root;
root = NULL;
}
else
// One child case
*root = *temp; // Copy the contents of
// the non-empty child
free(temp);
}
else
{
}
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->left), height(root->right));
{
root->left = leftRotate(root->left);
return rightRotate(root);
}
9
if (balance < -1 &&
getBalance(root->right) > 0)
{
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
int main()
{
Node *root = NULL;
preOrder(root);
root = deleteNode(root, 10);
cout << "\nPreorder traversal after"<< " deletion of 10 \n";
preOrder(root);
return 0;
}
Output
Preorder traversal of the constructed AVL tree is
9 1 0 -1 5 2 6 10 11
Preorder traversal after deletion of 10
1 0 -1 9 5 2 6 11
10
Time Complexity:
The rotation operations (left and right rotate) take constant time as only few pointers
are being changed there. Updating the height and getting the balance factor also take
constant time. So the time complexity of AVL delete remains same as BST delete
which is O(h) where h is height of the tree. Since AVL tree is balanced, the height is
O(Logn). So time complexity of AVL delete is O(Log n).
Auxiliary Space: O(1), since no extra space is used.
#include <iostream>
using namespace std;
class BST {
private:
struct node {
int key;
struct node* left, *right;
};
struct node* root;
struct node* newNode(int item) {
struct node* temp = new struct node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
11
public:
BST() : root(NULL) {}
int main() {
BST bst;
// Searching in a BST
if (!bst.search(key))
cout << key << " not found" << endl;
else
cout << key << " found" << endl;
}
return 0;
}
12
Output
Enter the number of keys to be inserted:
5
Enter the keys to be inserted:
24
25
26
27
28
Enter the number of keys to be searched: 3
Enter the keys to be searched:
28
28 found
25
25 found
27
27 found
Time Complexity: O(log N), where N is the number of nodes of the tree
Auxiliary Space: O(1)
13