Name: Muhammad Bilal
Roll no: FA21-BCS-110
DSA Lab Task
Submitted to
Sir Jazib E Nazar
Comsats University Islamabad,
Abbottabad Campus.
Max Heap
public class heap {
private int[] heap;
private int size;
private int capacity;
heap(int capacity) {
[Link] = capacity;
[Link] = new int[capacity];
[Link] = 0;
int getParentIndex(int index) {
return (index - 1) / 2;
int getLeftChildIndex(int index) {
return (2 * index) + 1;
int getRightChildIndex(int index) {
return (2 * index) + 2;
void swap(int i, int j) {
int temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
void heapifyUp(int index) {
int parentIndex = getParentIndex(index);
while (index > 0 && heap[index] > heap[parentIndex]) {
swap(index, parentIndex);
index = parentIndex;
parentIndex = getParentIndex(index);
void heapifyDown(int index) {
int maxIndex = index;
int leftChildIndex = getLeftChildIndex(index);
int rightChildIndex = getRightChildIndex(index);
if (leftChildIndex < size && heap[leftChildIndex] > heap[maxIndex]) {
maxIndex = leftChildIndex;
if (rightChildIndex < size && heap[rightChildIndex] > heap[maxIndex]) {
maxIndex = rightChildIndex;
if (index != maxIndex) {
swap(index, maxIndex);
heapifyDown(maxIndex);
void insert(int value) {
if (size == capacity) {
[Link]("Heap is full. Cannot insert more elements.");
return;
}
heap[size] = value;
size++;
heapifyUp(size - 1);
void buildMaxHeap(int[] array) {
if ([Link] > capacity) {
[Link]("Input array exceeds heap capacity.");
return;
size = [Link];
for (int i = 0; i < size; i++) {
heap[i] = array[i];
for (int i = (size / 2) - 1; i >= 0; i--) {
heapifyDown(i);
void printHeap() {
for (int i = 0; i < size; i++) {
[Link](heap[i] + " ");
[Link]();
public static void main(String[] args) {
heap heap=new heap(10);
[Link](4);
[Link](10);
[Link](3);
[Link](5);
[Link](1);
[Link]("Max Heap:");
[Link]();
Min Heap
public class heap {
private int[] heap;
private int size;
private int capacity;
public heap(int capacity) {
[Link] = capacity;
[Link] = new int[capacity];
[Link] = 0;
private int getParentIndex(int index) {
return (index - 1) / 2;
private int getLeftChildIndex(int index) {
return (2 * index) + 1;
private int getRightChildIndex(int index) {
return (2 * index) + 2;
private void swap(int i, int j) {
int temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
private void heapifyUp(int index) {
int parentIndex = getParentIndex(index);
while (index > 0 && heap[index] < heap[parentIndex]) {
swap(index, parentIndex);
index = parentIndex;
parentIndex = getParentIndex(index);
private void heapifyDown(int index) {
int minIndex = index;
int leftChildIndex = getLeftChildIndex(index);
int rightChildIndex = getRightChildIndex(index);
if (leftChildIndex < size && heap[leftChildIndex] < heap[minIndex]) {
minIndex = leftChildIndex;
if (rightChildIndex < size && heap[rightChildIndex] < heap[minIndex]) {
minIndex = rightChildIndex;
if (index != minIndex) {
swap(index, minIndex);
heapifyDown(minIndex);
public void insert(int value) {
if (size == capacity) {
[Link]("Heap is full. Cannot insert more elements.");
return;
heap[size] = value;
size++;
heapifyUp(size - 1);
public void buildMinHeap(int[] array) {
if ([Link] > capacity) {
[Link]("Input array exceeds heap capacity.");
return;
size = [Link];
for (int i = 0; i < size; i++) {
heap[i] = array[i];
for (int i = (size / 2) - 1; i >= 0; i--) {
heapifyDown(i);
public void printHeap() {
for (int i = 0; i < size; i++) {
[Link](heap[i] + " ");
[Link]();
public static void main(String[] args) {
heap heap = new heap(10);
[Link](4);
[Link](10);
[Link](3);
[Link](5);
[Link](1);
[Link]("Min Heap:");
[Link]();
AVL
public class avl {
class Node {
int value;
Node left;
Node right;
int height;
Node(int value) {
[Link] = value;
height = 1;
Node root;
int height(Node node) {
if (node == null) {
return 0;
return [Link];
int max(int a, int b) {
return (a > b) ? a : b;
Node rotateLeft(Node x) {
Node y = [Link];
Node T2 = [Link];
[Link] = x;
[Link] = T2;
[Link] = max(height([Link]), height([Link])) + 1;
[Link] = max(height([Link]), height([Link])) + 1;
return y;
Node rotateRight(Node y) {
Node x = [Link];
Node T2 = [Link];
[Link] = y;
[Link] = T2;
[Link] = max(height([Link]), height([Link])) + 1;
[Link] = max(height([Link]), height([Link])) + 1;
return x;
Node insert(Node node, int value) {
if (node == null) {
return new Node(value);
if (value < [Link]) {
[Link] = insert([Link], value);
} else if (value > [Link]) {
[Link] = insert([Link], value);
} else {
return node; // Duplicate values are not allowed in this implementation
[Link] = max(height([Link]), height([Link])) + 1;
int balance = getBalance(node);
// Left Left case
if (balance > 1 && value < [Link]) {
return rotateRight(node);
}
// Right Right case
if (balance < -1 && value > [Link]) {
return rotateLeft(node);
// Left Right case
if (balance > 1 && value > [Link]) {
[Link] = rotateLeft([Link]);
return rotateRight(node);
// Right Left case
if (balance < -1 && value < [Link]) {
[Link] = rotateRight([Link]);
return rotateLeft(node);
return node;
int getBalance(Node node) {
if (node == null) {
return 0;
return height([Link]) - height([Link]);
void preOrder(Node node) {
if (node != null) {
// Perform any operation here
[Link]([Link] + " ");
preOrder([Link]);
preOrder([Link]);
public static void main(String[] args) {
avl tree = new avl();
[Link] = [Link]([Link], 10);
[Link] = [Link]([Link], 20);
[Link] = [Link]([Link], 30);
[Link] = [Link]([Link], 40);
[Link] = [Link]([Link], 50);
[Link] = [Link]([Link], 25);
[Link]("Preorder traversal of constructed AVL tree:");
[Link]([Link]);