0% found this document useful (0 votes)
8 views60 pages

A- 311

Uploaded by

bilalanwar1233
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)
8 views60 pages

A- 311

Uploaded by

bilalanwar1233
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/ 60

Muhammad Bilal

Roll No : 311
Write a program to check give number is positive/negtive even or odd.
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"enter a number:";
cin>>num;

if(num == 0)
{
cout<<"it is zero";
}
else if(num % 2==0)
{
if(num>0)
{
cout<<"positive even number";
}
else
{
cout<<"negative even number";
}
}
else{
if(num>0)
{
cout<<"postive odd number";
}
else{
cout<<"negative odd number";
}
}
}
Description :

Write a program to print numbers from 1 to 30 and skip numbers from 11 to 20.
#include<iostream>
using namespace std;
int main()
{
for(int i = 1;i<30;i++)
{
if(i>=11&& i<=20)
{
continue;
}
cout<<i<<endl;
}
}
output :

#include <iostream>

using namespace std;

void input(int array[], int size) {

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

cin >> array[i];

}
void display(int array[], int size) {

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

cout << "arr[" << i << "]" << " = " << array[i] << "\n";

cout << "\n";

int findElementIndex(int array[], int size, int element) {

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

if (array[i] == element)

return i; // Return the index if element is found

return -1; // Return -1 if element is not found

void deletion(int array[], int &size, int element) {

int index = findElementIndex(array, size, element);

if (index == -1) {

cout << "Element not found!";

} else {

for (int i = index; i < size - 1; i++)

array[i] = array[i + 1];

size--; // Decrement size after deletion

int main() {
int size;

cout << "Enter size of array: ";

cin >> size;

int arr[size]; // Variable Length Array (VLA) or use dynamic allocation

input(arr, size);

cout << "Given array: \n";

display(arr, size);

int element;

cout << "Enter element you want to delete: ";

cin >> element;

deletion(arr, size, element);

cout << "Array after deletion: \n";

display(arr, size);

return 0;

output :

Description :

#include <iostream>

using namespace std;

int linearSearch(int array[], int size, int element) {

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

if (array[i] == element) { // Fix comparison operator

return i; // Return the index if element is found

}
}

return -1; // Return -1 if element is not found

void input(int array[], int size) {

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

cin >> array[i];

void display(int array[], int size) {

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

cout << "arr[" << i << "] = " << array[i] << "\n";

cout << "\n";

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

const int capacity = 100; // Fix capacity declaration

int arr[capacity]; // Declare array with capacity

cout << "Enter elements of the array:\n";


input(arr, size);

cout << "Given array: \n";

display(arr, size);

int element;

cout << "Enter the element you want to search for: ";

cin >> element;

int result = linearSearch(arr, size, element); // Fix assignment and missing semicolon

if (result != -1) {

cout << "Element found at index " << result << ".\n"; // Fix incorrect string concatenation

} else {

cout << "Element not found in the array.\n";

return 0;

Description :

#include <iostream>

using namespace std;

// Function to take input into the array

void input(int array[], int size) {


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

cin >> array[i];

// Function to display elements of the array

void display(int array[], int size) {

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

cout << "arr[" << i << "] = " << array[i] << "\n";

cout << "\n";

// Function to insert an element at a specific index

int insert(int array[], int size, int element, int capacity, int index) {

if (size >= capacity) {

return -1; // if the array is already full, return -1

// Shift elements to the right, starting from the end

for (int i = size - 1; i >= index; i--) {

array[i + 1] = array[i]; // Move each element to the next index

array[index] = element; // Insert the element at the given index

return 1; // Return 1 indicating successful insertion

}
int main() {

int size;

cout << "Enter size of array: ";

cin >> size;

const int capacity = 100; // Define the capacity of the array

int arr[capacity]; // Declare the array

cout << "Enter elements of the array:\n";

input(arr, size);

cout << "Given array:\n";

display(arr, size);

int element, index;

cout << "Enter element you want to insert: ";

cin >> element;

cout << "Enter index at which you want to insert the element: ";

cin >> index;

// Insert element at the specified index

int result = insert(arr, size, element, capacity, index);

if (result == 1) {

size++; // Increase the size of the array after insertion


cout << "Array after insertion:\n";

display(arr, size);

} else {

cout << "Array is full, unable to insert element.\n";

return 0;

Output :

Description :

#include <iostream>

using namespace std;

// Function to take input into the array

void input(int array[], int size) {

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

cin >> array[i];

// Function to display elements of the array

void display(int array[], int size) {

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

cout << "arr[" << i << "] = " << array[i] << "\n";

}
cout << "\n";

// Function to find the index of an element

int findElementIndex(int array[], int size, int element) {

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

if (array[i] == element) { // Fix missing comparison operator

return i; // Return the index if element is found

return -1; // Return -1 if element is not found

// Function to update an element in the array

void updateElement(int array[], int size, int oldElement, int newElement) {

int index = findElementIndex(array, size, oldElement); // Call the find function

if (index == -1) { // If element is not found

cout << "Error: Element not found in the array.\n";

return;

// Update the element

array[index] = newElement;

cout << "Element updated successfully.\n";

}
int main() {

int size;

cout << "Enter size of array: ";

cin >> size;

const int capacity = 100; // Fixed array capacity definition

int arr[capacity]; // Declare the array

cout << "Enter elements of the array:\n";

input(arr, size);

cout << "Given array:\n";

display(arr, size);

int oldElement, newElement;

cout << "Enter the element you want to update: ";

cin >> oldElement;

cout << "Enter the new value: ";

cin >> newElement;

updateElement(arr, size, oldElement, newElement);

cout << "Array after update: \n";

display(arr, size);
return 0;

Output :

Description :

#include <iostream>

using namespace std;

int stack[100], choice = 0, n, top = -1;

void push();

void pop();

void show();

int main() {

cout << "Enter the number of elements in the stack: ";

cin >> n;

cout << "********* Stack operations using array *********" << endl;

cout << "----------------------------------------------" << endl;

// Loop for stack operations until user chooses to exit

while (choice != 4) {
cout << "\nChoose one from the below options..." << endl;

cout << "1. Push\n2. Pop\n3. Show\n4. Exit" << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

push();

break;

case 2:
pop();

break;

case 3:

show();

break;

case 4:

cout << "Exiting..." << endl;

break;

default:

cout << "Please enter a valid choice" << endl;

void push() {

int val;

if (top = = n - 1) {

cout << "\nOverflow" << endl; // Stack overflow if top reaches n-1

} else {

cout << "Enter the value: ";

cin >> val;

top = top + 1;

stack[top] = val;

void pop() {

if (top = = -1) {

cout << "Underflow" << endl; // Stack underflow if top is -1

} else {

top = top - 1; // Decrement top to pop the element


}

void show() {

if (top = = -1) {

cout << "Stack is empty" << endl; // If stack is empty

} else {

cout << "Stack elements are: " << endl;

for (int i = top; i >= 0; i--) {

cout << stack[i] << endl; // Display stack elements from top to bottom

Output :

Description :

Array
#include <iostream>
using namespace std;

int main() {
int Arr[5] = {18, 30, 15, 70, 12}; // Declare and initialize the array
int i;

cout << "Elements of the array are:" << endl; // Use cout for output
for(i = 0; i < 5; i++) {
cout << "Arr[" << i << "] = " << Arr[i] << ", "; // Use cout for array element printing
}

return 0; // Indicate successful program execution


}

output :
description :
#include <iostream>
using namespace std;

int main() {
int arr[20] = {18, 30, 15, 70, 12}; // Initialize array
int i, x, pos, n = 5;

// Display array elements before insertion


cout << "Array elements before insertion:" << endl;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;

x = 50; // Element to be inserted


pos = 4; // Position at which to insert
n++; // Increment the array size

// Shift elements to the right to make space for new element


for (i = n - 1; i >= pos; i--)
arr[i] = arr[i - 1];

arr[pos - 1] = x; // Insert the new element at the desired position

// Display array elements after insertion


cout << "Array elements after insertion:" << endl;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;

return 0; // Return success


}

Output :

Description :

Insertion in the Queue :


#include <iostream>
using namespace std;
void insert(int queue[], int max, int &front, int &rear, int item)
{
if (rear + 1 == max)
{
cout << "Overflow" << endl;
}
else
{
if (front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear + 1;
}
queue[rear] = item;
}
}
int main() {
const int max = 5;
int queue[max];
int front = -1, rear = -1;
// Example insertions
insert(queue, max, front, rear, 10);
insert(queue, max, front, rear, 20);
insert(queue, max, front, rear, 30);
// Display the queue contents
cout << "Queue contents: ";
for (int i = front; i <= rear; i++) {
cout << queue[i] << " ";
}
cout << endl;
return 0;
}

output :

Deletion in the queue :


#include <iostream>
using namespace std;
int deleteItem(int queue[], int max, int &front, int &rear)
{
int y;

if (front == -1 || front > rear)


{
cout << "Underflow" << endl;
return -1
}
else
{
y = queue[front];
if (front == rear)
{
front = rear = -1;
}
else
{
front = front + 1;
}
return y;
}
}
int main() {
const int max = 5;
int queue[max];
int front = -1, rear = -1;
front = 0;
rear = 2;
queue[0] = 10;
queue[1] = 20;
queue[2] = 30;
int deletedItem = deleteItem(queue, max, front, rear);
if (deletedItem != -1) {
cout << "Deleted item: " << deletedItem << endl;
}
cout << "Queue contents after deletion: ";
for (int i = front; i <= rear && front != -1; i++) {
cout << queue[i] << " ";
}
cout << endl;
return 0;
}

output :

Description :

Circular queue :
#include <iostream>
#define max 6
using namespace std;
int queue[max
int front = -1;
int rear = -1;
if (front == -1 && rear == -1) {

front = 0;
rear = 0;
queue[rear] = element;
}
else if ((rear + 1) % max == front) {
cout << "Queue is overflow.." << endl;
}
else {
rear = (rear + 1) % max;
queue[rear] = element;
}
void dequeue() {
if (front == -1 && rear == -1) {
cout << "\nQueue is underflow.." << endl;
}
else if (front == rear) {

cout << "\nThe dequeued element is " << queue[front] << endl;
front = -1;
rear = -1;
}
else {
cout << "\nThe dequeued element is " << queue[front] << endl;
front = (front + 1) % max;
}
}

void display() {
if (front == -1 && rear == -1) {
cout << "\nQueue is empty.." << endl;
}
else {
cout << "\nElements in the Queue are: ";
int i = front;
while (i != rear) {
cout << queue[i] << ", ";
i = (i + 1) % max;
}
cout << queue[rear] << endl;
}
}
int main() {
int choice = 1, x;
while (choice < 4 && choice != 0) {
cout << "\nPress 1: Insert an element";
cout << "\nPress 2: Delete an element";
cout << "\nPress 3: Display the elements";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the element to be inserted: ";
cin >> x;
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
cout << "Exiting program." << endl;
choice = 0;
break;
}
}
return 0;
}

Stack Operations

Push ():
#include <iostream>
using namespace std;
int top = -1
int stack[100];
void push(int val, int n) {
if (top = = n - 1) {
cout << "\nOverflow" << endl;
} else {
top = top + 1;
stack[top] = val;
}
}
int main() {
int n = 5;
push(10, n);
push(20, n);
push(30, n);
push(40, n);
push(50, n);
return 0; }

POP():
#include <iostream>
using namespace std;
int top = -1;
int stack[100];
int pop() {
if (top = = -1) {
cout << "Underflow" << endl;
return 0;
} else {
return stack[top--];
}
}
int main() {
stack[++top] = 10;
stack[++top] = 20;
cout << "Popped element: " << pop() << endl;
cout << "Popped element: " << pop() << endl;
cout << "Popped element: " << pop() << endl;
}
output :

Mix operation ():


#include <iostream>
using namespace std;
int stack[100], choice = 0, n, top = -1;
void push();
void pop();
void show();
int main() {
cout << "Enter the number of elements in the stack: ";
cin >> n;
cout << "********* Stack operations using array *********" << endl;
cout << "----------------------------------------------" << endl;
while (choice != 4) {
cout << "\nChoose one from the below options..." << endl;
cout << "1. Push\n2. Pop\n3. Show\n4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
cout << "Exiting..." << endl;
break;
default:
cout << "Please enter a valid choice" << endl;
}
}
}
void push() {
int val;
if (top = = n - 1) {
cout << "\nOverflow" << endl;
} else {
cout << "Enter the value: ";
cin >> val;
top = top + 1;
stack[top] = val;
}
}
void pop() {
if (top = = -1) {
cout << "Underflow" << endl;
} else {
top = top - 1;
}
}
void show() {
if (top = = -1) {
cout << "Stack is empty" << endl;
} else {
cout << "Stack elements are: " << endl;
for (int i = top; i >= 0; i--) {
cout << stack[i] << endl; }
}
}
output :

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

// Define the structure for a linked list node


struct Node {
string data;
Node* next;
};

// Function to create a new node


Node* createNode(const string& data) {
Node* newNode = new Node();
if (newNode == nullptr) {
cerr << "Memory allocation failed." << endl;
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = nullptr;
return newNode;
}

// Function to insert a new node at the beginning of the list


void insertAtBeginning(Node*& head, const string& data) {
Node* newNode = createNode(data);
newNode->next = head;
head = newNode;
}
// Function to insert a new node at the end of the list
void insertAtEnd(Node*& head, const string& data) {
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to delete a node with the given data


void deleteNode(Node*& head, const string& data) {
if (head == nullptr) {
cout << "List is empty. Nothing to delete." << endl;
return;
}

if (head->data == data) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* temp = head;
while (temp->next != nullptr && temp->next->data != data) {
temp = temp->next;
}

if (temp->next == nullptr) {
cout << "Node with data \"" << data << "\" not found." << endl;
return;
}

Node* nodeToDelete = temp->next;


temp->next = temp->next->next;
delete nodeToDelete;
}

// Function to print the linked list


void printList(Node* head) {
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}

while (head != nullptr) {


cout << head->data << " ";
head = head->next;
}
cout << endl;
}

// Function to clean up the entire list


void deleteList(Node*& head) {
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
}

int main() {
Node* head = nullptr;

insertAtEnd(head, "Apple");
insertAtEnd(head, "Banana");
insertAtEnd(head, "Cherry");

cout << "Linked List: ";


printList(head);

insertAtBeginning(head, "Apricot");

cout << "After inserting Apricot at the beginning: ";


printList(head);
deleteNode(head, "Banana");

cout << "After deleting Banana: ";


printList(head);

// Clean up the list


deleteList(head);

cout << "After deleting the entire list: ";


printList(head);

}
Output :

#include <iostream>
#include <string>

using namespace std;

// Define the structure for a doubly linked list node


struct Node {
string data;
Node* next;
Node* prev;
};
// Function to create a new node
Node* createNode(const string& data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = nullptr;
newNode->prev = nullptr;
return newNode;
}

// Function to insert a new node at the beginning of the list


void insertAtBeginning(Node*& head, Node*& tail, const string& data) {
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
tail = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
}

// Function to insert a new node at the end of the list


void insertAtEnd(Node*& head, Node*& tail, const string& data) {
Node* newNode = createNode(data);
if (tail == nullptr) {
head = newNode;
tail = newNode;
} else {
newNode->prev = tail;
tail->next = newNode;
tail = newNode;
}
}

// Function to delete a node with the given data


void deleteNode(Node*& head, Node*& tail, const string& data) {
if (head == nullptr) return;

if (head->data == data) {
Node* temp = head;
head = head->next;
if (head != nullptr) {
head->prev = nullptr;
} else {
tail = nullptr;
}
delete temp;
return;
}

Node* temp = head;


while (temp->next != nullptr) {
if (temp->next->data == data) {
Node* nodeToDelete = temp->next;
temp->next = temp->next->next;
if (temp->next != nullptr) {
temp->next->prev = temp;
} else {
tail = temp;
}
delete nodeToDelete;
return;
}
temp = temp->next;
}
}

// Function to print the doubly linked list in forward direction


void printForward(Node* head) {
while (head != nullptr) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
}

// Function to print the doubly linked list in backward direction


void printBackward(Node* tail) {
while (tail != nullptr) {
cout << tail->data << " ";
tail = tail->prev;
}
cout << endl;
}

int main() {
Node* head = nullptr;
Node* tail = nullptr;

insertAtEnd(head, tail, "Apple");


insertAtEnd(head, tail, "Banana");
insertAtEnd(head, tail, "Cherry");

cout << "Linked List (Forward): ";


printForward(head);

cout << "Linked List (Backward): ";


printBackward(tail);

insertAtBeginning(head, tail, "Apricot");

cout << "After inserting Apricot at the beginning (Forward): ";


printForward(head);

cout << "After inserting Apricot at the beginning (Backward): ";


printBackward(tail);
deleteNode(head, tail, "Banana");

cout << "After deleting Banana (Forward): ";


printForward(head);

cout << "After deleting Banana (Backward): ";


printBackward(tail);

return 0;
}

Assignmnet 6
In order :
Code :
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = NULL;
right = NULL;
}
};

// Function to perform in-order traversal of the binary tree


void inOrderTraversal(Node* root) {
if (root == NULL) return;

// Visit the left subtree


inOrderTraversal(root->left);

// Visit the root node


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

// Visit the right subtree


inOrderTraversal(root->right);
}

// Helper function to insert nodes in the binary tree


Node* insertNode(Node* root, int value) {
if (root == NULL) return new Node(value);

if (value < root->data) {


root->left = insertNode(root->left, value);
} else {
root->right = insertNode(root->right, value);
}
return root;
}

int main() {
// Create a binary tree
Node* root = NULL;
root = insertNode(root, 50);
root = insertNode(root, 30);
root = insertNode(root, 70);
root = insertNode(root, 20);
root = insertNode(root, 40);
root = insertNode(root, 60);
root = insertNode(root, 80);

// Perform in-order traversal


cout << "In-order Traversal of the Binary Tree: ";
inOrderTraversal(root);
cout << endl;

return 0;
}
Output:

Post order :
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = NULL;
right = NULL;
}
};

// Function to perform post-order traversal of the binary tree


void postOrderTraversal(Node* root) {
if (root == NULL) return;

// Visit the left subtree


postOrderTraversal(root->left);

// Visit the right subtree


postOrderTraversal(root->right);

// Visit the root node


cout << root->data << " ";
}
// Helper function to insert nodes in the binary tree
Node* insertNode(Node* root, int value) {
if (root == NULL) return new Node(value);

if (value < root->data) {


root->left = insertNode(root->left, value);
} else {
root->right = insertNode(root->right, value);
}

return root;
}

int main() {
// Create a binary tree
Node* root = NULL;
root = insertNode(root, 50);
root = insertNode(root, 30);
root = insertNode(root, 70);
root = insertNode(root, 20);
root = insertNode(root, 40);
root = insertNode(root, 60);
root = insertNode(root, 80);

// Perform post-order traversal


cout << "Post-order Traversal of the Binary Tree: ";
postOrderTraversal(root);
cout << endl;

return 0;
}
Output :

Pre order :

Code :
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = NULL;
right = NULL;
}
};

// Function to perform pre-order traversal of the binary tree


void preOrderTraversal(Node* root) {
if (root == NULL) return;

// Visit the root node


cout << root->data << " ";
// Visit the left subtree
preOrderTraversal(root->left);

// Visit the right subtree


preOrderTraversal(root->right);
}

// Helper function to insert nodes in the binary tree


Node* insertNode(Node* root, int value) {
if (root == NULL) return new Node(value);

if (value < root->data) {


root->left = insertNode(root->left, value);
} else {
root->right = insertNode(root->right, value);
}

return root;
}

int main() {
// Create a binary tree
Node* root = NULL;
root = insertNode(root, 50);
root = insertNode(root, 30);
root = insertNode(root, 70);
root = insertNode(root, 20);
root = insertNode(root, 40);
root = insertNode(root, 60);
root = insertNode(root, 80);

// Perform pre-order traversal


cout << "Pre-order Traversal of the Binary Tree: ";
preOrderTraversal(root);
cout << endl;

return 0;
}
Output :

Assigmnet 6
Assignment 7
Write a code for insertion in binary tree.
#include <bits/stdc++.h>
using namespace std;

// AVL tree node structure


struct Node {
int key;
Node* left;
Node* right;
int height;
};

// Function to create a new node


Node* createNode(int key) {
Node* node = new Node();
node->key = key;
node->left = nullptr;
node->right = nullptr;
node->height = 1; // New node is initially added at leaf
return node;
}

// Function to get the height of the tree


int height(Node* N) {
return (N == nullptr) ? 0 : N->height;
}

// Function to perform a right rotation


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

// 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;
// Return new root
return x;
}

// Function to perform a left rotation


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

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

// Return new root


return y;
}

// Function to get the balance factor of a node


int getBalance(Node* N) {
return (N == nullptr) ? 0 : height(N->left) - height(N->right);
}

// Function to insert a key into the AVL tree


Node* insert(Node* node, int key) {
if (node == nullptr)
return createNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else
return node; // Duplicate keys are not allowed

// Update height of the current node


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

// Get the balance factor to check if the node is balanced


int balance = getBalance(node);

// Perform rotations to balance the tree

// Left Left Case


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

// Right Right Case


if (balance < -1 && key > node->right->key)
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;
}

// Function to print the tree in preorder traversal


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

// Driver code
int main() {
Node* root = nullptr;
// Insert keys into the AVL tree
root = insert(root, 9);
root = insert(root, 5);
root = insert(root, 10);
root = insert(root, 0);
root = insert(root, 6);
root = insert(root, 11);
root = insert(root, -1);
root = insert(root, 1);
root = insert(root, 2);

cout << "Preorder traversal of the constructed AVL tree is:\n";


preOrder(root);

return 0;
}
output :

Write a code for searching in binary search tree.


#include <bits/stdc++.h>
using namespace std;

// An AVL tree node


struct Node {
int key;
Node *left;
Node *right;
int height;
};

// Function to create a new node


Node* createNode(int key) {
Node* node = new Node();
node->key = key;
node->left = nullptr;
node->right = nullptr;
node->height = 1; // New node is initially added at leaf
return node;
}

// Function to get the height of the tree


int height(Node *N) {
return (N == nullptr) ? 0 : N->height;
}

// Function to perform a right rotation


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

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

// Return new root


return x;
}

// Function to perform a left rotation


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

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

// Return new root


return y;
}
// Function to get the balance factor of a node
int getBalance(Node *N) {
return (N == nullptr) ? 0 : height(N->left) - height(N->right);
}

// Function to insert a key into the AVL tree


Node* insert(Node* node, int key) {
// Perform the normal BST insertion
if (node == nullptr)
return createNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Equal keys not allowed
return node;

// Update height of this ancestor node


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

// Get the balance factor


int balance = getBalance(node);

// Perform rotations to balance the tree


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

// Function to find the node with the minimum key value


Node* minValueNode(Node* node) {
Node* current = node;
while (current->left != nullptr)
current = current->left;
return current;
}

// Function to delete a node from the AVL tree


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;
free(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(height(root->left), height(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;
}

// Function to print the tree in preorder traversal


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

// Driver code
int main() {
Node *root = nullptr;

root = insert(root, 9);


root = insert(root, 5);
root = insert(root, 10);
root = insert(root, 0);
root = insert(root, 6);
root = insert(root, 11);
root = insert(root, -1);
root = insert(root, 1);
root = insert(root, 2);

cout << "Preorder traversal of the constructed AVL tree is \n";


preOrder(root);

root = deleteNode(root, 10);

cout << "\nPreorder traversal after deletion of 10 \n";


preOrder(root);
return 0;
}

ouput :

write a code for deletion in avl tree :


code
#include <bits/stdc++.h>
using namespace std;

// AVL tree node structure


struct Node {
int key;
Node *left;
Node *right;
int height;
};

// Function to create a new node


Node* createNode(int key) {
Node* node = new Node();
node->key = key;
node->left = nullptr;
node->right = nullptr;
node->height = 1; // New node is initially added at leaf
return node;
}
// Function to get the height of the tree
int height(Node* N) {
return (N == nullptr) ? 0 : N->height;
}

// Function to perform a right rotation


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

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

// Return new root


return x;
}

// Function to perform a left rotation


Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
// 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;

// Return new root


return y;
}

// Function to get the balance factor of a node


int getBalance(Node* N) {
return (N == nullptr) ? 0 : height(N->left) - height(N->right);
}

// Function to find the node with the smallest key in a tree


Node* minValueNode(Node* node) {
Node* current = node;
while (current->left != nullptr)
current = current->left;
return current;
}

// Function to insert a key into the AVL tree


Node* insert(Node* node, int key) {
if (node == nullptr)
return createNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else
return node; // Duplicate keys are not allowed

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


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

// Function to delete a node from the AVL tree


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;
free(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(height(root->left), height(root->right));


int balance = getBalance(root);

// Left Left Case


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

// Left Right Case


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

// Right Right Case


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

// Right Left Case


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

return root;
}

// Function to print the tree in preorder traversal


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

// Driver code
int main() {
Node* root = nullptr;

root = insert(root, 9);


root = insert(root, 5);
root = insert(root, 10);
root = insert(root, 0);
root = insert(root, 6);
root = insert(root, 11);
root = insert(root, -1);
root = insert(root, 1);
root = insert(root, 2);

cout << "Preorder traversal of the constructed AVL tree is:\n";


preOrder(root);

root = deleteNode(root, 10);

cout << "\nPreorder traversal after deletion of 10:\n";


preOrder(root);

return 0;
}
output :

You might also like