Data Structure Program Notes (1)
Data Structure Program Notes (1)
6. AVL TREE
7. GRAPH TRAVERSAL
9. SORTING
Program:1
#include <iostream>
class ArrayList {
private:
int* array;
int capacity;
int current;
public:
ArrayList() {
capacity = 1;
current = 0;
if (current == capacity) {
temp[i] = array[i];
delete[] array;
capacity *= 2;
array = temp;
array[current] = data;
current++;
add(data);
} else {
array[index] = data;
return array[index];
void pop() {
current--;
int size() {
return current;
int getCapacity() {
return capacity;
void print() {
~ArrayList() {
delete[] array;
};
int main() {
ArrayList list;
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.print();
cout << "Size of the list: " << list.size() << endl;
cout << "Capacity of the list: " << list.getCapacity() << endl;
list.pop();
list.print();
cout << "Size after popping: " << list.size() << endl;
cout << "Capacity after popping: " << list.getCapacity() << endl;
return 0;
}
Output:
class Node
public:
int data;
Node *next;
};
new_node->data = data;
new_node->next = *head;
*head = new_node;
if (*head == NULL)
return;
}
// move head to next node
*head = (*head)->next;
delete (temp);
node = node->next;
int main ()
display (head);
dequeue (&head);
display (head);
return 0;
}
Output:
Elements of the queue: 1 2 3 4 5
#include <string.h>
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
char associativity(char c) {
if (c == '^')
return 'R';
return 'L'; // Default to left-associative
}
result[resultIndex++] = c;
}
// If the scanned character is an ‘(‘, push it to the stack.
else if (c == '(') {
stack[++stackIndex] = c;
}
// If the scanned character is an ‘)’, pop and add to the output string from the stack
// until an ‘(‘ is encountered.
else if (c == ')') {
while (stackIndex >= 0 && stack[stackIndex] != '(') {
result[resultIndex++] = stack[stackIndex--];
}
stackIndex--; // Pop '('
}
// If an operator is scanned
else {
while (stackIndex >= 0 && (prec(s[i]) < prec(stack[stackIndex]) ||
prec(s[i]) == prec(stack[stackIndex]) &&
associativity(s[i]) == 'L')) {
result[resultIndex++] = stack[stackIndex--];
}
stack[++stackIndex] = c;
}
}
result[resultIndex] = '\0';
printf("%s\n", result);
}
// Driver code
int main() {
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
// Function call
infixToPostfix(exp);
return 0;
}
Output:
abcd^e-fgh*+^*+i-
Program:4
#include <iostream>
#include <queue>
#include <vector>
while (!pq.empty()) {
pq.pop();
int main() {
priority_queue<int> maxHeap;
maxHeap.push(10);
maxHeap.push(30);
maxHeap.push(20);
maxHeap.push(5);
maxHeap.push(1);
printPriorityQueue(maxHeap);
maxHeap.pop();
cout << "Elements in the max-heap after removing the top element: ";
printPriorityQueue(maxHeap);
return 0;}
output:
Elements in the max-heap (priority queue): 30 20 10 5 1
Top element: 30
struct Node {
int key;
Node* left;
Node* right;
Node(int item) {
key = item;
left = right = NULL;
}
};
// is present at root
if (root == NULL || root->key == key)
return root;
return 0;
}
Output:
Not Found
Found
Program:6
// C++ program to insert a node in AVL tree
#include<bits/stdc++.h>
class Node
public:
int key;
Node *left;
Node *right;
int height;
};
if (N == NULL)
return 0;
return N->height;
// of two integers
node->key = key;
node->left = NULL;
node->right = NULL;
// added at leaf
return(node);
Node *x = y->left;
// 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 x;
}
Node *y = x->right;
// 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 y;
if (N == NULL)
return 0;
}
// Recursive function to insert a key
if (node == NULL)
return(newNode(key));
return node;
node->height = 1 + max(height(node->left),
height(node->right));
unbalanced */
return leftRotate(node);
node->left = leftRotate(node->left);
return rightRotate(node);
node->right = rightRotate(node->right);
return leftRotate(node);
return node;
// of every node
if(root != NULL)
{
preOrder(root->left);
preOrder(root->right);
// Driver Code
int main()
preOrder(root);
return 0;
}
Output:
#include <list>
class Graph
list<int> *adjacencyList;
public:
};
Graph::Graph(int numberOfVertices)
this->numberOfVertices = numberOfVertices;
adjacencyList[v].push_back(w);
adjacencyList[w].push_back(v);
visited[i] = false;
// Create a queue for bfsTraversal
list<int> queue;
visited[vertex] = true;
queue.push_back(vertex);
list<int>::iterator i;
while(!queue.empty())
vertex = queue.front();
queue.pop_front();
// If a adjacent vertex has not been visited, then mark it visited and enqueue it
if (!visited[*i])
visited[*i] = true;
queue.push_back(*i);
int main()
Graph graph(7);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 1);
graph.addEdge(2, 5);
graph.addEdge(3, 6);
graph.bfsTraversal(1);
return 0;
}
Output:
Breadth First Traversal from 1 is :
1034265
Program:8
1.Linear search
#include <iostream>
if (arr[i] == key) {
int main() {
int key = 1;
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
return 0;
2.Binary search
#include <iostream>
if (arr[mid] == key) {
return mid;
left = mid + 1;
right = mid - 1;
int main() {
int key = 5;
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
return 0;
}
Output:1
Element found at index 3
Output:2
Element found at index 4
Program:9
1.Bubble sort
#include <iostream>
int main() {
bubbleSort(arr, size);
printArray(arr, size);
return 0;
}
2.Selection sort:
#include <iostream>
int minIdx = i;
minIdx = j;
swap(arr[minIdx], arr[i]);
int main() {
selectionSort(arr, size);
printArray(arr, size);
return 0;
}
3.Quick sort:
#include <iostream>
int t = *a;
*a = *b;
*b = t;
i++;
swap(&arr[i], &arr[j]);
return (i + 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
printArray(arr, size);
return 0;
}
Output:1
Sorted array using Bubble Sort: 11 12 22 25 34 64 90
Output:2
Sorted array using Selection Sort: 11 12 22 25 64
Output:3
Sorted array using Quick Sort: 1 5 7 8 9 10