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

Class Lab Exercise 4 PCP

The document contains Java code for implementing a Binary Search Tree (BST), a Max Heap, and a Min Heap. It includes methods for inserting nodes, searching for a node, and performing in-order traversal for the BST, as well as methods for inserting elements and printing the structure for both heaps. Each data structure is demonstrated with example usage in a main method.

Uploaded by

Ak-47
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)
6 views6 pages

Class Lab Exercise 4 PCP

The document contains Java code for implementing a Binary Search Tree (BST), a Max Heap, and a Min Heap. It includes methods for inserting nodes, searching for a node, and performing in-order traversal for the BST, as well as methods for inserting elements and printing the structure for both heaps. Each data structure is demonstrated with example usage in a main method.

Uploaded by

Ak-47
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

Shivam Arora(@01420358)

Class Lab Exercise 4


BST Code:

class Node {
String name;
int value;
Node left, right;
// Constructor to create a new node
Node(String name, int value) {
this.name = name;
this.value = value;
left = right = null;
}
}
class BinarySearchTree {
Node root;
// Method to insert a new node into the BST
void insert(String name, int value) {
root = insertRec(root, name, value);
}
// Recursive method to insert a new node in the correct position
Node insertRec(Node root, String name, int value) {
if (root == null) {
root = new Node(name, value);
return root;
}
if (name.compareTo(root.name) < 0) {
root.left = insertRec(root.left, name, value);
} else if (name.compareTo(root.name) > 0) {
root.right = insertRec(root.right, name, value);
}
return root;
}
// Method to search for a node by name
Node lookup(String name) {
return lookupRec(root, name);
}
// Recursive method to search for a node
Node lookupRec(Node root, String name) {
if (root == null || root.name.equals(name)) {
return root;
}
if (name.compareTo(root.name) < 0) {
return lookupRec(root.left, name);
}
return lookupRec(root.right, name);
}
// Method for in-order traversal of the BST
void inorder() {
inorderRec(root);
}
// Recursive in-order traversal to print the nodes in ascending order
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.name + " : " + root.value);
inorderRec(root.right);
}
}
}
public class BinaryTreeExample {
public static void main(String[] args) {
BinarySearchTree bst = new BinarySearchTree();
// Inserting nodes into the BST
bst.insert("Aacute", 0x00c1);
bst.insert("AElig", 0x00c6);
bst.insert("zeta", 0x03b6);
bst.insert("Beta", 0x0392);
bst.insert("Alpha", 0x0391);
// Searching for a specific node
String searchKey = "Aacute";
Node foundNode = bst.lookup(searchKey);
System.out.println(foundNode != null ? "Found: " + foundNode.name + " with value: " +
foundNode.value : "Not Found");
// Performing an in-order traversal
System.out.println("In-order Traversal of BST:");
bst.inorder();
}
}

BST Output:
Max Heap Code:

class MaxHeap {
private int[] heap;
private int size;
private int maxSize;
public MaxHeap(int maxSize) {
this.maxSize = maxSize;
this.size = 0;
heap = new int[this.maxSize + 1];
heap[0] = Integer.MAX_VALUE; // Sentinel for comparison
}
private int parent(int pos) { return pos / 2; }
private int leftChild(int pos) { return 2 * pos; }
private int rightChild(int pos) { return (2 * pos) + 1; }
private void swap(int pos1, int pos2) {
int temp = heap[pos1];
heap[pos1] = heap[pos2];
heap[pos2] = temp;
}
public void insert(int element) {
heap[++size] = element;
int current = size;
while (heap[current] > heap[parent(current)]) {
swap(current, parent(current));
current = parent(current);
}
}
public void print() {
for (int i = 1; i <= size / 2; i++) {
System.out.println("Parent: " + heap[i] +
" Left Child: " + heap[2 * i] +
" Right Child: " + heap[2 * i + 1]);
}
}
}
public class MaxHeapExample {
public static void main(String[] args) {
MaxHeap maxHeap = new MaxHeap(10);
maxHeap.insert(20);
maxHeap.insert(15);
maxHeap.insert(30);
maxHeap.insert(40);
maxHeap.insert(10);
maxHeap.insert(5);
maxHeap.print();
}
}
Max Heap Output:
Min Heap Code:

class MinHeap {
private int[] heap;
private int size;
private int maxSize;
public MinHeap(int maxSize) {
this.maxSize = maxSize;
this.size = 0;
heap = new int[this.maxSize + 1];
heap[0] = Integer.MIN_VALUE; // Sentinel for comparison
}
private int parent(int pos) { return pos / 2; }
private int leftChild(int pos) { return 2 * pos; }
private int rightChild(int pos) { return (2 * pos) + 1; }
private void swap(int pos1, int pos2) {
int temp = heap[pos1];
heap[pos1] = heap[pos2];
heap[pos2] = temp;
}
public void insert(int element) {
heap[++size] = element;
int current = size;
while (heap[current] < heap[parent(current)]) {
swap(current, parent(current));
current = parent(current);
}
}
public void print() {
for (int i = 1; i <= size / 2; i++) {
System.out.println("Parent: " + heap[i] +
" Left Child: " + heap[2 * i] +
" Right Child: " + heap[2 * i + 1]);
}
}
}
public class MinHeapExample {
public static void main(String[] args) {
MinHeap minHeap = new MinHeap(10);
minHeap.insert(20);
minHeap.insert(15);
minHeap.insert(30);
minHeap.insert(10);
minHeap.insert(5);
minHeap.print();
}
}

Min Heap Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy