Class Lab Exercise 4 PCP
Class Lab Exercise 4 PCP
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();
}
}