0% found this document useful (0 votes)
27 views

Assignment # 3: Source Code

The document contains source code for an AVL tree data structure and hash table implementation in C++. It includes functions for inserting and deleting nodes from the AVL tree, rotating the tree to maintain the balanced property, and calculating heights and balances. The hash table uses the AVL trees at each index based on a hashing function to store and retrieve values.

Uploaded by

Muhammad Jamal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Assignment # 3: Source Code

The document contains source code for an AVL tree data structure and hash table implementation in C++. It includes functions for inserting and deleting nodes from the AVL tree, rotating the tree to maintain the balanced property, and calculating heights and balances. The hash table uses the AVL trees at each index based on a hashing function to store and retrieve values.

Uploaded by

Muhammad Jamal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

DSA

ASSIGNMENT # 3
SOURCE CODE:
#include <iostream>

#include <fstream>

#include<conio.h>

#include <stdio.h>

#include <string.h>

#include <windows.h>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

#include <bits/stdc++.h>

#define C 10

void clearScreen()//function to clear screen in console

// HANDLE is a predefined class

// The handle class stores and manages a pointer to the base //class. The type of the object to which that pointer
points will //vary; it can point at either a base- or a derived-type object. //Users access the operations of the
inheritance hierarchy //through the handle.

HANDLE hStdOut;

CONSOLE_SCREEN_BUFFER_INFO csbi;

DWORD count;

DWORD cellCount;

COORD homeCoords = { 0, 0 };

hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

if (hStdOut == INVALID_HANDLE_VALUE) return;

/* Get the number of cells in the current buffer */

if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;


cellCount = csbi.dwSize.X * csbi.dwSize.Y;

/* Fill the entire buffer with spaces */

if (!FillConsoleOutputCharacter(

hStdOut,

(TCHAR)' ',

cellCount,

homeCoords,

&count

)) return;

/* Fill the entire buffer with the current colors and attributes */

if (!FillConsoleOutputAttribute(

hStdOut,

csbi.wAttributes,

cellCount,

homeCoords,

&count

)) return;

/* Move the cursor home */

SetConsoleCursorPosition(hStdOut, homeCoords);

//-----------------------------------------------------------

struct node{

string data;

int height;

struct node *left;

struct node *right;

};
int max(int n1, int n2){//function to get max of two numbers

if(n1 > n2){

return n1;

return n2;

void printTree(node *root, int s){

if(root == NULL){

return;

s += C;

printTree(root->right,s);

cout<<root->data<<"->"<<endl;

printTree(root->left, s);

void output(node *root, int i){

cout<<"<<"<<i<<">>"<<endl;

printTree(root,0);

int getIndex(string name){

string s1 = bitset<8>(name[0]).to_string();

s1 = s1.substr(s1.length() - 3);
int n = 0;

int s = s1.size();

for(int i = 0; i < s; i++){

if(s1[i] == '1'){

n += pow(2.0, i);

}else if(s1[i] == '1' && i != 0){

n += pow(0.0, i);

return n;

//-----------------------------------------------------------------------------

//-------------------------------------------------------------------------------

class AvlTree{

public:

struct node *root;

AvlTree(){

this->root = NULL;

node *rotateRight(node *currentNode){

node *leftNode = currentNode->left;


node *rightNode = currentNode->right;

leftNode->right = currentNode;

currentNode->left = rightNode;

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

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

return leftNode;

node *rotateLeft(node *currentNode){

node *leftNode = currentNode->left;

node *rightNode = currentNode->right;

rightNode->left = currentNode;

currentNode->right = leftNode;

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

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

return rightNode;

node *minimumValueNode(node *currentNode){

node *temp = currentNode;

while(temp->left != NULL){

temp = temp->left;
}

return temp;

int calculateHeight(struct node *currentNode){

if(currentNode->left && currentNode->right){

if(currentNode->left->height < currentNode->right->height){

return currentNode->right->height + 1;

}else return currentNode->left->height +1;

}else if(currentNode->left && currentNode->right == NULL){

return currentNode->left->height + 1;

}else if(currentNode->left == NULL && currentNode->right){

return currentNode->right->height + 1;

}return 0;

int getHeight(node *currentNode){

if(currentNode == NULL){

return 0;

return currentNode->height;

int calculateBalance(node *currentNode){

if(currentNode == NULL){

return 0;

int balance = getHeight(currentNode->left)-getHeight(currentNode->right);

return balance;

}
node *insertNode(node *root, string data){

if(root == NULL){

struct node *currentNode;

currentNode = new struct node;

currentNode->data = data;

root = currentNode;

root->left = root->right = NULL;

root->height = 1;

return root;

}else{

if(data < root->data){

root->left = insertNode(root->left,data);

}else if(data > root->data){

root->right = insertNode(root->right, data);

}else{

return root;

root->height = calculateHeight(root);

int balance = calculateBalance(root);

if(balance > 1 && data < root->left->data){

return rotateRight(root);

if(balance < -1 && data > root->right->data){

root->left = rotateLeft(root->left);

return rotateRight(root);

}
if(balance > 1 && data > root->left->data){

root->left = rotateLeft(root->left);

return rotateRight(root);

if(balance < -1 && data < root->right->data){

root->right = rotateRight(root->right);

return rotateLeft(root);

return root;

node *deleteNode(node *root, string data){

if(root == NULL){

return root;

if(data < root->data){

root->left = deleteNode(root->left, data);

}else if(data > root->data){

root->right = deleteNode(root->right, data);

}else{

if((root->left == NULL) || (root->right == NULL)){

node *temp;

if(root->left){

temp = root->left;

}else{

temp = root->right;
}

if( temp == NULL){

temp = root;

root == NULL;

}else{

*root = *temp;

free(temp);

}else{

node *temp = minimumValueNode(root->right);

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);

if(root == NULL){

return root;

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

int balance = calculateBalance(root);

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

return rotateRight(root);

if(balance > 1 && calculateBalance(root->left) > 0){

root->left = rotateLeft(root->left);
return rotateRight(root);

if(balance < -1 && calculateBalance(root->right) <= 0){

return rotateLeft(root);

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

root->right = rotateRight(root->right);

return rotateLeft(root);

return root;

};

//-----------------------------------------------------------------

class HashTable{

int size;

AvlTree *tree;

public:

int hasher(string name){

return getIndex(name);

HashTable(int size){

this->size = size;

tree = new AvlTree[size];

}
void insertItem(string data){

int i = hasher(data);

tree[i].root = tree[i].insertNode(tree[i].root, data);

int getHashHeight(int i){

return tree[i].root->height;

deleteItem(string data){

int i = hasher(data);

tree[i].root = tree[i].deleteNode(tree[i].root, data);

void insertItm(string data){

int i = hasher(data);

tree[i].root = tree[i].insertNode(tree[i].root, data);

void displayHash(){

for(int i = 0; i < size; i++){

output(tree[i].root, i);

};
int main(int argc, char** argv) {

string arr[91];

int c = 0;//counter for while loop

string l;//string to store lines from text file temporarily

ifstream file;//creating an instance of ifstream class

string data;

int i;

file.open("C:/Users/hp/Desktop/New folder/NamesToHash.txt");//opening file from location

if(file.is_open()){

while (getline (file, l)){//while end of file is not reached

//get each line from the text file and store in the variable l

arr[c] = l;//assign l to each index position in array

c++;//increment c

if(file.eof()) break;

file.close();//closing the file

int n = sizeof(arr)/sizeof(arr[0]);//size of array

HashTable hash(8);

for(int j = 0; j<n;j++){//inserting data in the hashtable

hash.insertItem(arr[j]);

}
int choice = 0;//user choice

string choice2;

while(choice != 4){//loop breaks when user enters 4

clearScreen();

cout<<"Wh do you want to do?"<< endl;

cout<<"1. Insert a name."<<endl;

cout<<"2. Delete a name."<<endl;

cout<<"3. View names."<<endl;

cout<<"4. Exit"<<endl;

cin>>choice;

switch(choice){

case 1:

cout<<"Enter name to be inserted:"<<endl;

getline(cin, data);

getline(cin, data);

hash.insertItem(data);

cout<<"press any key to continue:";

cin>>choice2;

break;

case 2:

cout<<"Enter name to be deleted:"<<endl;

getline(cin, data);

getline(cin, data);

hash.deleteItem(data);

cout<<"press any key to continue:";


cin>>choice2;

break;

case 3:

hash.displayHash();

cout<<"press any key to continue:";

cin>>choice2;

break;

You might also like