0% found this document useful (0 votes)
161 views10 pages

CS301-Assignment 2 100% Coorect Solution Spring 2025 by M.junaid Qazi

This document outlines the instructions and code for Assignment No. 2 in CS301 - Data Structure, focusing on implementing an AVL tree in C++. It includes guidelines for submission, a sample code structure, and methods to insert, delete, and traverse nodes in the AVL tree. The assignment emphasizes originality in solutions and provides contact information for queries.

Uploaded by

Abdul Moeez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views10 pages

CS301-Assignment 2 100% Coorect Solution Spring 2025 by M.junaid Qazi

This document outlines the instructions and code for Assignment No. 2 in CS301 - Data Structure, focusing on implementing an AVL tree in C++. It includes guidelines for submission, a sample code structure, and methods to insert, delete, and traverse nodes in the AVL tree. The assignment emphasizes originality in solutions and provides contact information for queries.

Uploaded by

Abdul Moeez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS301 – Data Structure Total Marks = 20

Assignment No.2 Solution Deadline


Semester: Spring 2025 th
23 of Jun 2025

Please carefully read the following instructions before attempting the assignment Solution.

NOTE
Don't copy-paste the same answer.
Make sure you can make some changes to your solution file before submitting copy
paste solution will be marked zero.
If you found any mistake then correct yourself and inform me.
Before submitting an Assignment GDB checks your assignment requirement file.

For any query, feel free to Contact at


WhatsApp: +923074960034

Provide by M.JUNAID QAZI

Code:

#include <iostream>
#include <cstddef>
using namespace std;

class Node {
public:
int key;
Node* left;
Node* right;
int height;
CONTACT ON WHATSAPP
+923074960034
Node(int k) : key(k), left(NULL), right(NULL), height(1) {}
};

class AVL {
private:
Node* root;

int height(Node* node) {


return node ? node->height : 0;
}

int getBalance(Node* node) {


return height(node->left) - height(node->right);
}

void updateHeight(Node* node) {


node->height = 1 + max(height(node->left), height(node->right));
}

Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T = x->right;

x->right = y;
y->left = T;

CONTACT ON WHATSAPP
+923074960034
updateHeight(y);
updateHeight(x);

return x;
}

Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T = y->left;

y->left = x;
x->right = T;

updateHeight(x);
updateHeight(y);

return y;
}

Node* minValueNode(Node* node) {


while (node->left) node = node->left;
return node;
}

public:

CONTACT ON WHATSAPP
+923074960034
AVL() : root(NULL) {}

/************** To be implemented by Student ***************/

// TODO 1: Implement insertNode Method


Node* insertNode(Node* node, int key) {
if (node == NULL)
return new Node(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; // No duplicates allowed

updateHeight(node);

int balance = getBalance(node);

// Left Left Case


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

// Right Right Case


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

CONTACT ON WHATSAPP
+923074960034
return leftRotate(node);

// Left Right Case


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

// Right Left Case


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

return node;
}

// TODO 2: Implement deleteNode Method


Node* deleteNode(Node* node, int key) {
if (node == NULL)
return node;

if (key < node->key)


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

CONTACT ON WHATSAPP
+923074960034
else {
// Node with one or no child
if (!node->left || !node->right) {
Node* temp = node->left ? node->left : node->right;

if (!temp) {
temp = node;
node = NULL;
} else
*node = *temp;

delete temp;
} else {
Node* temp = minValueNode(node->right);
node->key = temp->key;
node->right = deleteNode(node->right, temp->key);
}
}

if (node == NULL)
return node;

updateHeight(node);

int balance = getBalance(node);

CONTACT ON WHATSAPP
+923074960034
// Left Left Case
if (balance > 1 && getBalance(node->left) >= 0)
return rightRotate(node);

// Left Right Case


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

// Right Right Case


if (balance < -1 && getBalance(node->right) <= 0)
return leftRotate(node);

// Right Left Case


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

return node;
}

// TODO 3: Implement inOrder Method


void inOrder(Node* node) {
if (node) {

CONTACT ON WHATSAPP
+923074960034
inOrder(node->left);
cout << node->key << " ";
inOrder(node->right);
}
}

/**********************************************************/

// Helper to call from main()


void insert(int key) { root = insertNode(root, key); }
void deleteKey(int key) { root = deleteNode(root, key); }
void inOrder() { inOrder(root); cout << endl; }
};

int main() {
// A) Hard-code the student ID
string studentID = "BC190409022"; // ? Replace this with your actual VUID

// Extract digits and combine


int idLastDigit = studentID[studentID.size() - 1] - '0';

// Display header & extract id last digit


cout << "---------------- XYZ Company System (" << studentID << ")---------------" <<
endl;
cout << "Extracted Last Digits is:\n" << idLastDigit << "\n\n";

CONTACT ON WHATSAPP
+923074960034
AVL tree;
int arr[] = {10, 20, 30, 40, 50};
// Creating AVL tree with initial nodes
for (int i = 0; i < 5; i++) {
tree.insert(arr[i]);
}

cout << "Inorder Traversal before Insertion:";


tree.inOrder();
cout << endl;

// Inserting digit to AVL


cout << "Inserting " << idLastDigit << " into AVL." << endl;
tree.insert(idLastDigit);
cout << "Inorder Traversal after Insertion: ";
tree.inOrder();
cout << endl;

// Removing digit form AVL


cout << "Deleting " << idLastDigit << " from AVL." << endl;
tree.deleteKey(idLastDigit);
cout << "Inorder AVL after deletion: ";
tree.inOrder();

return 0;
}

CONTACT ON WHATSAPP
+923074960034
OUTPUT:

Every Assignment/GDB is change due to unique Student ID so don’t copy


That is truly perfect step by step idea solution get help easily.

Wish you the very best of Luck!

CONTACT ON WHATSAPP
+923074960034

You might also like