0% found this document useful (0 votes)
20 views6 pages

Ahsan (FA23 BAI 020)

The document contains two Java programs for data structures. The first program implements a Max Heap, allowing insertion and printing of the heap at each step. The second program implements a Binary Tree with functionalities for insertion, searching, deletion, and level order traversal.

Uploaded by

ahsan.763786
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)
20 views6 pages

Ahsan (FA23 BAI 020)

The document contains two Java programs for data structures. The first program implements a Max Heap, allowing insertion and printing of the heap at each step. The second program implements a Binary Tree with functionalities for insertion, searching, deletion, and level order traversal.

Uploaded by

ahsan.763786
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
You are on page 1/ 6

DS LAB TERMINAL

Submitted to: Mam Fouzia

Name : Malik Ahsan Hayat


Reg #: FA23-BAI-020
Question no 1:
Code:
package DSLabTerminalQ1;

class Max_Heap {
public int[] heap;
public int size;
public int maxsize;

public Max_Heap(int maxsize) {


this.maxsize = maxsize;
this.size = 0;
heap = new int[maxsize];
}

public int parent(int pos) {


return (pos - 1) / 2;
}

public int left(int i) {


return 2 * i + 1;
}

public int right(int pos) {


return 2 * pos + 2;
}

private void swap(int i, int j) {


int temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}

public void insert(int key) {


if (size == maxsize) {
System.out.println("Heap is full");
return;
}

heap[size] = key;
int current = size;
size++;

while (current > 0 && heap[current] > heap[parent(current)]) {


swap(current, parent(current));
current = parent(current);
}

printHeap();
}

public void printHeap() {


System.out.print("Step " + size + ": ");
for (int i = 0; i < size; i++) {
System.out.print(heap[i] + " ");
}
System.out.println();
}
}

public class DSLabTerminalQ1 {


public static void main(String[] args) {
Max_Heap heap = new Max_Heap(15);

int[] elements = {21, 27, 22, 23, 45, 35, 4, 19, 42};

System.out.println("Building Max Heap step-by-step:");


for (int value : elements) {
heap.insert(value);
}
}
}

OUTPUT:
Question no 2:
Code:
package DSLabTerminalQ2;

import java.util.LinkedList;
import java.util.Queue;

class BinaryNode {
int value;
BinaryNode left, right;

public BinaryNode(int value) {


this.value = value;
left = null;
right = null;
}
}

class BinaryTree {
BinaryNode root;

public BinaryNode insertNode(BinaryNode node, int value) {


if (node == null) {
return new BinaryNode(value);
}
if (value < node.value) {
node.left = insertNode(node.left, value);
} else if (value > node.value) {
node.right = insertNode(node.right, value);
}
return node;
}

public boolean searchNode(BinaryNode node, int value) {


if (node == null) return false;
if (value == node.value) return true;
return (value < node.value) ? searchNode(node.left, value) :
searchNode(node.right, value);
}

public BinaryNode deleteNode(BinaryNode node, int value) {


if (node == null) return null;

if (value < node.value) {


node.left = deleteNode(node.left, value);
} else if (value > node.value) {
node.right = deleteNode(node.right, value);
} else {
if (node.left == null && node.right == null) {
return null;
}

else if (node.left == null) {


return node.right;
} else if (node.right == null) {
return node.left;
}

else {
BinaryNode minNode = getMin(node.right);
node.value = minNode.value;
node.right = deleteNode(node.right, minNode.value);
}
}
return node;
}

public BinaryNode getMin(BinaryNode node) {


while (node.left != null) {
node = node.left;
}
return node;
}

public void levelOrderTraversal(BinaryNode node) {


if (node == null) return;
Queue<BinaryNode> queue = new LinkedList<>();
queue.add(node);

while (!queue.isEmpty()) {
BinaryNode current = queue.remove();
System.out.print(current.value + " ");
if (current.left != null) queue.add(current.left);
if (current.right != null) queue.add(current.right);
}
System.out.println();
}
}

public class DSLabTerminalQ2 {


public static void main(String[] args) {
BinaryTree tree = new BinaryTree();

int[] values = {5, 2, 12, 1, 3, 9, 21, 19, 25};


for (int val : values) {
tree.root = tree.insertNode(tree.root, val);
}

System.out.print("Initial Tree (Level Order): ");


tree.levelOrderTraversal(tree.root);
System.out.println("Search 9: " + tree.searchNode(tree.root, 9));
System.out.println("Search 15: " + tree.searchNode(tree.root, 15));

tree.root = tree.deleteNode(tree.root, 1);


tree.root = tree.deleteNode(tree.root, 21);
tree.root = tree.deleteNode(tree.root, 12);

System.out.print("Tree After Deletions: ");


tree.levelOrderTraversal(tree.root);
}
}

OUTPUT:

You might also like