0% found this document useful (0 votes)
40 views15 pages

ASSIGN#4

The document describes an AVL tree data structure implemented as a C++ class template. It includes functions for inserting and deleting nodes, rotating the tree for balance, calculating heights and balancing factors, and traversing the tree via preorder, inorder and postorder traversal. It also includes functions for finding the minimum/maximum nodes, counting nodes and leaves, and searching for a key. The main function demonstrates creating an AVL tree, inserting nodes, deleting nodes, and searching.

Uploaded by

Zerry Merry
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
40 views15 pages

ASSIGN#4

The document describes an AVL tree data structure implemented as a C++ class template. It includes functions for inserting and deleting nodes, rotating the tree for balance, calculating heights and balancing factors, and traversing the tree via preorder, inorder and postorder traversal. It also includes functions for finding the minimum/maximum nodes, counting nodes and leaves, and searching for a key. The main function demonstrates creating an AVL tree, inserting nodes, deleting nodes, and searching.

Uploaded by

Zerry Merry
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 15

DATA STRUCTURES

NAME: MUHAMMAD ZARAIZ

ROLL NO:21F-9151

SECTION C

TASK 1:

HEADER:

#pragma once
#ifndef AVLTree
#define AVLTree
template<class T >
struct Node
{
T data;
Node<T> *left;
Node<T> *right;
};
template<class T >
class avlTree
{
public:
Node<T> *root;
int height(Node<T> *);
int diff(Node<T> *);
Node<T> *rr_rotation(Node<T> *);
Node<T> *ll_rotation(Node<T> *);
Node<T> *lr_rotation(Node<T> *);
Node<T> *rl_rotation(Node<T> *);
Node<T>* balance(Node<T> *);
Node<T>* insert(Node<T> *, T);
void display(Node<T> *, T);
void inorder(Node<T> *);
void preorder(Node<T> *);
void postorder(Node<T> *);
Node<T>* deleteKey(Node<T>* t, T x);
Node<T>* findMin(Node<T>*);
Node<T>* findMax(Node<T>*);
void searchKey(T k);
Node<T>* searchKey(Node<T>* node, T key);
int treeNodeCount(Node<T>* t);
int treeLeavesCount(Node<T>* t);
avlTree();
void insert(T x);
void deleteKey(T x);
void display();
void display_tree();
};
#endif
.CPP FILE

#include <iostream>
#include <iomanip>
#include "AVLTree.h"
using namespace std;

template<class T >
avlTree<T>::avlTree()
{
root = NULL;
}
template<class T >
void avlTree<T>::display()
{
display(root, 1);
}

template<class T>
void avlTree<T>::display_tree()
{
cout << "preOrder: ";
preorder(root);
cout << "\ninOrder: ";
inorder(root);
cout << "\npostOrder: ";
postorder(root);
cout << "\ntreeHeight: ";
cout << height(root);
cout << "\ntreeNodeCount: ";
cout << treeNodeCount(root);
cout << "\ntreeLeavesCount: ";
cout << treeLeavesCount(root);
cout << "\nMinimum of tree: ";
cout << findMin(root)->data;
cout << "\nMaximum of tree: ";
cout << findMax(root)->data;
cout << endl;
}

template<class T >
void avlTree<T> ::insert(T x)
{
insert(root, x);
}

template<class T >
void avlTree<T> ::deleteKey(T x)
{
deleteKey(root, x);
}

template<class T >
int avlTree<T>::height(Node<T> *temp)
{
int h = 0;
if (temp != NULL)
{
int l_height = height(temp->left);
int r_height = height(temp->right);
int max_height = max(l_height, r_height);
h = max_height + 1;
}
return h;
}

template<class T >
int avlTree<T>::diff(Node<T> *temp)
{
int l_height = height(temp->left);
int r_height = height(temp->right);
int b_factor = l_height - r_height;
return b_factor;
}

template<class T >
Node<T> *avlTree<T>::rr_rotation(Node<T> *root)
{
Node *temp;
temp = root->right;
root->right = temp->left;
temp->left = root;
return temp;
}

template<class T >
Node<T> *avlTree<T>::ll_rotation(Node<T> *root)
{
Node *temp;
temp = root->left;
root->left = temp->right;
temp->right = root;
return temp;
}

template<class T >
Node<T> *avlTree<T>::lr_rotation(Node<T> *root)
{
Node *temp;
temp = root->left;
root->left = rr_rotation(temp);
return ll_rotation(root);
}

template<class T >
Node<T> *avlTree<T>::rl_rotation(Node<T> *root)
{
Node *temp;
temp = root->right;
root->right = ll_rotation(temp);
return rr_rotation(root);
}

template<class T >
Node<T> *avlTree<T>::balance(Node<T> *temp)
{
int bal_factor = diff(temp);
if (bal_factor > 1)
{
if (diff(temp->left) > 0)
temp = ll_rotation(temp);
else
temp = lr_rotation(temp);
}
else if (bal_factor < -1)
{
if (diff(temp->right) > 0)
temp = rl_rotation(temp);
else
temp = rr_rotation(temp);
}
return temp;
}

template<class T >
Node<T> *avlTree<T>::insert(Node<T> *root, T value)
{
if (root == NULL)
{
root = new Node;
root->data = value;
root->left = NULL;
root->right = NULL;
return root;
}
else if (value < root->data)
{
root->left = insert(root->left, value);
root = balance(root);
}
else if (value >= root->data)
{
root->right = insert(root->right, value);
root = balance(root);
}
return root;
}

template<class T >
void avlTree<T>::display(Node<T> *ptr, T level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level + 1);
cout << endl;
if (ptr == root)
cout << "Root -> ";
for (i = 0; i < level && ptr != root; i++)
cout << " ";
cout << ptr->data;
display(ptr->left, level + 1);
}
}

template<class T >
void avlTree<T>::inorder(Node<T> *tree)
{
if (tree == NULL)
return;
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}

template<class T >
void avlTree<T> ::preorder(Node<T> *tree)
{
if (tree == NULL)
return;
cout << tree->data << " ";
preorder(tree->left);
preorder(tree->right);

template<class T >
Node<T>* avlTree<T>::findMin(Node<T>* t) {
if (t == NULL)
{
return t;
}
else if (t->left == NULL)
{
return t;
}
else
return findMin(t->left);
}

template<class T >
Node<T>* avlTree<T>::findMax(Node<T>* t)
{
if (t == NULL)
return t;
else if (t->right == NULL)
return t;
else return findMax(t->right);
}

template<class T >
void avlTree<T>::postorder(Node<T> *tree)
{
if (tree == NULL)
return;
postorder(tree->left);
postorder(tree->right);
cout << tree->data << " ";
}

template<class T >
Node<T>* avlTree<T>::deleteKey(Node<T>* t, T x) {
Node* temp;

if (t == NULL) return NULL;

else if (x < t->data) t->left = deleteKey(t->left, x);


else if (x > t->data) t->right = deleteKey(t->right, x);

else if (t->left && t->right) {


temp = findMin(t->right);
t->data = temp->data;
t->right = deleteKey(t->right, t->data);
}

else {
temp = t;
if (t->left == NULL) t = t->right;
else if (t->right == NULL) t = t->left;
delete temp;
}
if (t == NULL) return t;

t = balance(t);
}

template<class T >
int avlTree<T> ::treeNodeCount(Node<T>* t)
{
int count = 0, h = height(t);

while (t != nullptr) {
if (height(t->right) == h - 1)
{
count += (1 << h);
t = t->right;
}
else
{
count += (1 << h - 1);
t = t->left;
}
--h;
}

return count;
}

template<class T >
int avlTree<T> ::treeLeavesCount(Node<T>* t)
{
if (t == NULL)
return 0;
if (t->left == NULL && t->right == NULL)
return 1;
else
{
return treeLeavesCount(t->left) + treeLeavesCount(t->right);
}
}

template<class T >
void avlTree<T> ::searchKey(T k)
{
searchKey(root, k);
}

template<class T >
Node<T>* avlTree<T> ::searchKey(Node<T>* node, T key)
{
if (node == nullptr || key == node->data)
{
return node;
}
if (key < node->data)
{
return searchKey(node->left, key);
}
return searchKey(node->right, key);
}

MAIN.CPP
#include<iostream>
using namespace std;
#include "AVLTree.h"
int main()
{
avlTree<int> *tree = new avlTree();
tree->insert(65);
tree->insert(55);
tree->insert(22);
tree->insert(44);
tree->insert(61);
tree->insert(19);
tree->insert(90);
tree->insert(10);
tree->insert(78);
tree->display_tree();
cout << endl << endl;

cout << "after deleting our BST will become" << endl;
tree->deleteKey(55);
tree->deleteKey(44);
tree->deleteKey(78);
tree->display_tree();
cout << endl;

cout << "your searched key is " << tree->searchKey(17)->data << endl;
delete tree;

system("pause");
return 0;
}
OUTPUT:
TASK 2:

#include<iostream>
#include <algorithm>

using namespace std;


template<class T >
struct Node
{
T data;
Node<T> *left;
Node<T> *right;
};
template<class T >
class avlTree
{
public:
Node<T> *root;
int height(Node<T> *);
int diff(Node<T> *);
Node<T> *rr_rotation(Node<T> *);
Node<T> *ll_rotation(Node<T> *);
Node<T> *lr_rotation(Node<T> *);
Node<T> *rl_rotation(Node<T> *);
Node<T>* balance(Node<T> *);
Node<T>* insert(Node<T> *, T);
void display(Node<T> *, T);
void inorder(Node<T> *);
void preorder(Node<T> *);
void postorder(Node<T> *);
Node<T>* deleteKey(Node<T>* t, T x);
Node<T>* findMin(Node<T>*);
Node<T>* findMax(Node<T>*);
void searchKey(T k);
Node<T>* searchKey(Node<T>* node, T key);
int treeNodeCount(Node<T>* t);
int treeLeavesCount(Node<T>* t);
avlTree();
void insert(T x);
void deleteKey(T x);
void display();
void display_tree();
Node* sortedArrayToBST(int sortedArray[], int left, int right)
};
template<class T >
avlTree<T>:: avlTree()
{
root = NULL;
}
template<class T >
void avlTree<T>:: display()
{
display(root, 1);
}

template<class T>
void avlTree<T>:: display_tree()
{
cout << "preOrder: ";
preorder(root);
cout << "\ninOrder: ";
inorder(root);
cout << "\npostOrder: ";
postorder(root);
cout << "\ntreeHeight: ";
cout << height(root);
cout << "\ntreeNodeCount: ";
cout << treeNodeCount(root);
cout << "\ntreeLeavesCount: ";
cout << treeLeavesCount(root);
cout << "\nMinimum of tree: ";
cout << findMin(root)->data;
cout << "\nMaximum of tree: ";
cout << findMax(root)->data;
cout << endl;
}

template<class T >
void avlTree<T> :: insert(T x)
{
insert(root, x);
}

template<class T >
void avlTree<T> :: deleteKey(T x)
{
deleteKey(root, x);
}

template<class T >
int avlTree<T>::height(Node<T> *temp)
{
int h = 0;
if (temp != NULL)
{
int l_height = height(temp->left);
int r_height = height(temp->right);
int max_height = max(l_height, r_height);
h = max_height + 1;
}
return h;
}

template<class T >
int avlTree<T>::diff(Node<T> *temp)
{
int l_height = height(temp->left);
int r_height = height(temp->right);
int b_factor = l_height - r_height;
return b_factor;
}

template<class T >
Node<T> *avlTree<T>::rr_rotation(Node<T> *root)
{
Node *temp;
temp = root->right;
root->right = temp->left;
temp->left = root;
return temp;
}

template<class T >
Node<T> *avlTree<T>::ll_rotation(Node<T> *root)
{
Node *temp;
temp = root->left;
root->left = temp->right;
temp->right = root;
return temp;
}

template<class T >
Node<T> *avlTree<T>::lr_rotation(Node<T> *root)
{
Node *temp;
temp = root->left;
root->left = rr_rotation(temp);
return ll_rotation(root);
}

template<class T >
Node<T> *avlTree<T>::rl_rotation(Node<T> *root)
{
Node *temp;
temp = root->right;
root->right = ll_rotation(temp);
return rr_rotation(root);
}

template<class T >
Node<T> *avlTree<T>::balance(Node<T> *temp)
{
int bal_factor = diff(temp);
if (bal_factor > 1)
{
if (diff(temp->left) > 0)
temp = ll_rotation(temp);
else
temp = lr_rotation(temp);
}
else if (bal_factor < -1)
{
if (diff(temp->right) > 0)
temp = rl_rotation(temp);
else
temp = rr_rotation(temp);
}
return temp;
}

template<class T >
Node<T> *avlTree<T>::insert(Node<T> *root, T value)
{
if (root == NULL)
{
root = new Node;
root->data = value;
root->left = NULL;
root->right = NULL;
return root;
}
else if (value < root->data)
{
root->left = insert(root->left, value);
root = balance(root);
}
else if (value >= root->data)
{
root->right = insert(root->right, value);
root = balance(root);
}
return root;
}

template<class T >
void avlTree<T>::display(Node<T> *ptr, T level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level + 1);
cout << endl;
if (ptr == root)
cout << "Root -> ";
for (i = 0; i < level && ptr != root; i++)
cout << " ";
cout << ptr->data;
display(ptr->left, level + 1);
}
}

template<class T >
void avlTree<T>::inorder(Node<T> *tree)
{
if (tree == NULL)
return;
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}

template<class T >
void avlTree<T> ::preorder(Node<T> *tree)
{
if (tree == NULL)
return;
cout << tree->data << " ";
preorder(tree->left);
preorder(tree->right);
}

template<class T >
Node<T>* avlTree<T>::findMin(Node<T>* t) {
if (t == NULL)
{
return t;
}
else if (t->left == NULL)
{
return t;
}
else
return findMin(t->left);
}

template<class T >
Node<T>* avlTree<T>::findMax(Node<T>* t)
{
if (t == NULL)
return t;
else if (t->right == NULL)
return t;
else return findMax(t->right);
}

template<class T >
void avlTree<T>::postorder(Node<T> *tree)
{
if (tree == NULL)
return;
postorder(tree->left);
postorder(tree->right);
cout << tree->data << " ";
}

template<class T >
Node<T>* avlTree<T>::deleteKey(Node<T>* t, T x) {
Node* temp;

if (t == NULL) return NULL;

else if (x < t->data) t->left = deleteKey(t->left, x);


else if (x > t->data) t->right = deleteKey(t->right, x);

else if (t->left && t->right) {


temp = findMin(t->right);
t->data = temp->data;
t->right = deleteKey(t->right, t->data);
}

else {
temp = t;
if (t->left == NULL) t = t->right;
else if (t->right == NULL) t = t->left;
delete temp;
}
if (t == NULL) return t;

t = balance(t);
}

template<class T >
int avlTree<T> :: treeNodeCount(Node<T>* t)
{
int count = 0, h = height(t);

while (t != nullptr) {
if (height(t->right) == h - 1)
{
count += (1 << h);
t = t->right;
}
else
{
count += (1 << h - 1);
t = t->left;
}
--h;
}

return count;
}

template<class T >
int avlTree<T> :: treeLeavesCount(Node<T>* t)
{
if (t == NULL)
return 0;
if (t->left == NULL && t->right == NULL)
return 1;
else
{
return treeLeavesCount(t->left) + treeLeavesCount(t->right);
}
}

template<class T >
void avlTree<T> ::searchKey(T k)
{
searchKey(root, k);
}

template<class T >
Node<T>* avlTree<T> :: searchKey(Node<T>* node, T key)
{
if (node == nullptr || key == node->data)
{
return node;
}
if (key < node->data)
{
return searchKey(node->left, key);
}
return searchKey(node->right, key);
}
template<class T>
Node<T>* avlTree<T>::sortedArrayToBST(int sortedArray[], int left, int right)
{
Node *temp(int data);
Node *root;
if (left > right)
{
return NULL;
}

int middle = (left + right) / 2;


root = temp(sortedArray[middle]);

root->left = sortedArrayToBST(sortedArray, left, middle - 1);


root->right = sortedArrayToBST(sortedArray, middle + 1, right);

return root;
}

int main()
{
avlTree<int> *tree = new avlTree();

cout << endl;


int sorted_array[] = { 34,21,55,76,32,87,23,56 };
int n = 8;

tree->sortedArrayToBST(sorted_array, 0, n - 1);
tree->display();

delete tree;

system("pause");
return 0;
}

You might also like