Untitled
BST bst = new BST(); bst.create_tree(68); bst.in_order(); class BST{ private class Node{ int data; Node Right,Left; Node(int value) { this.data = value; } } private Node root; public void create_tree(int value) { if(root==null) { root = new Node(value); return; } Node temp = root; while(true) { if(value<temp.data) { if(temp.Left==null) { temp.Left = new Node(value); break; } temp = temp.Left; } else { if(temp.Right==null) { temp.Right = new Node(value); break; } temp = temp.Right; } } } public void in_order() { tr_in_order(root); } private void tr_in_order(Node root) { if(root==null) return; tr_in_order(root.Left); tr_in_order(root.Right); System.out.print(" "+root.data); } } // Sorting Merge: public static void main(String[] args) { int[] arr = new int[]{50, 9, 5, 10, 52, 2}; division(arr, 0, arr.length - 1); int[] var2 = arr; int var3 = arr.length; for(int var4 = 0; var4 < var3; ++var4) { int a = var2[var4]; System.out.print(" " + a); } System.out.println(""); } public static void division(int[] arr, int Start, int End) { if (Start < End) { int mid = (Start + End) / 2; division(arr, Start, mid); division(arr, mid + 1, End); combined(arr, Start, mid, End); } } public static void combined(int[] arr, int start, int mid, int end) { int array1_idx = start; int array2_idx = mid + 1; int[] combine = new int[end - start + 1]; int i = 0; while(array1_idx <= mid && array2_idx <= end) { if (arr[array1_idx] > arr[array2_idx]) { combine[i++] = arr[array2_idx++]; } else { combine[i++] = arr[array1_idx++]; } } while(array1_idx <= mid) { combine[i++] = arr[array1_idx++]; } while(array2_idx <= end) { combine[i++] = arr[array2_idx++]; } int k = 0; for(int j = start; k < combine.length; ++j) { arr[j] = combine[k]; ++k; } } } // Bubble public static int[] Bubble_sort(int[] arr) { for(int i=0;i<arr.length;i++) { boolean b = false; for (int j=0;j<arr.length-1-i;j++) { if(arr[j]>arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; b=true; } } if(b==false) break; } return arr; } // Selection: public static int[] Selection_Sort(int[] arr) { for(int i=0;i<arr.length;i++) { int min = i; for(int j=i+1;j<arr.length;j++) { if(arr[min]>arr[j]) min = j; } int temp =arr[i]; arr[i] = arr[min]; arr[min] = temp; } return arr; } //with String public static String[] selection_str_sort(String[] arr) { for(int i=0;i<arr.length-1;i++) { for(int j=i+1;j<arr.length;j++) { int a = arr[i].charAt(0); int b = arr[j].charAt(0); if(a>b) { String temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return arr; } // public static void main(String[] args) { // int[] arr = new int[]{64,25,12,22,11}; // Sorting.print_arr(Sorting.selection_sort(arr)); // Sorting.print_arr(Sorting.selection_str_sort(Name)); // Insertion: public static int[] insertion_sort(int[] arr) { for (int i = 1; i < arr.length; i++) { int key = arr[i]; // The element to be inserted int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } return arr; } // Queue public class Queue { int front, rear; int size = 0; Patient[] queue; Queue(int size) { queue = new Patient[size]; rear = -1; front = 0 ; } public void sortQueue() { // using bubble sort int n = size; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (queue[j].Priority < queue[j + 1].Priority) { Patient temp = queue[j]; queue[j] = queue[j + 1]; queue[j + 1] = temp; } } } } public void enqueue(Patient patient) { if (size == queue.length) { System.out.println("No space left in Queue"); return; } rear = ( rear + 1) % queue.length; queue[rear] = patient; size++; sortQueue(); } public Patient dequeue() { if (size == 0) { System.out.println("Queue is empty"); return null; } Patient toReturn = queue[front]; front = ( front + 1) % queue.length; size--; return toReturn; } public void displayNextPatient() { int temp = front; System.out.println("Current Patient: " + queue[temp].Name); temp++; System.out.println("Next Patient: " + queue[temp].Name); } } public void delete_node(int value) { root = delete_recursively(root, value); } private Node delete_recursively(Node root, int value) { if (root == null) { System.out.println("Value not found in the tree."); return null; // Node not found } if (value < root.data) { root.Left = delete_recursively(root.Left, value); } else if (value > root.data) { root.Right = delete_recursively(root.Right, value); } else { // Node found // Case 1: Node is a leaf if (root.Left == null && root.Right == null) { return null; } if (root.Left == null) { return root.Right; // Replace with the right child } else if (root.Right == null) { return root.Left; // Replace with the left child } Node successor = find_min(root.Right); root.data = successor.data; // Replace the node's value with the successor's value root.Right = delete_recursively(root.Right, successor.data); // Delete the successor } return root; } private Node find_min(Node root) { while (root.Left != null) { root = root.Left; } return root; } public class Main { public static void main(String[] args) { Queue priorityQueue = new Queue(10); priorityQueue.enqueue(new Patient("Ali", "High Fever", 4, "General")); // Searching public class Searching { private static int array[] = new int[1],count=0; public static void linear_search(int[] arr,int data) { for(int i=0;i<arr.length;i++) { if(arr[i]==data) System.out.print(" ->( "+i+" : "+arr[i]+" ) "); } } private static void add(int value) { if(count==array.length) { int[] temp = new int[array.length+1]; for(int i=0;i<array.length;i++) { temp[i] = array[i]; } array = temp; } array[count++] = value; } public static int[] index_return(int[] arr,int data) { for(int i=0;i<arr.length;i++) { if(arr[i]==data) add(i); } return array; } public static void string_Index(String[] name, char letter){ for (int i = 0; i < name.length; i++) { for (int j = 0; j < name[i].length(); j++) { if (name[i].charAt(j) == letter) System.out.printf(" "+ j); } System.out.println(); } } public static void binary_search(int[] arr,int data) { int f,l,m; f = 0; l = arr.length-1; while (f<=l) { m=(f+l)/2; if(arr[m]==data) { System.out.println("Value is at : " + m + " index"); break; } else if(arr[m]>data) l=m-1; else f=m+1; } } } // dLL public class DoublyLinkedList { Node head,tail; DoublyLinkedList() { head = null; tail = null; } public boolean isEmpty() { if(head==null) return true; else return false; } public void create_node_first(int data) { Node newNode = new Node(); newNode.data = data; if(isEmpty()) tail = newNode; else head.prev = newNode; newNode.next = head; head = newNode; } public void create_node_last(int data) { Node newNode = new Node(); newNode.data = data; if(isEmpty()) head = newNode; else tail.next = newNode; newNode.prev = tail; tail = newNode; } public void print_h_to_t() { Node current = head; while (current!=null) { System.out.print(" ->"+current.data); current = current.next; } System.out.println(); } public void print_t_to_h() { Node current = tail; while (current!=null) { System.out.print(" ->"+current.data); current = current.prev; } System.out.println(); } public int max() { Node current = tail; int max = current.data; while (current!=null) { if(max< current.data) max = current.data; current = current.prev; } return max; } public int min() { Node current = tail; int min = current.data; while (current!=null) { if(min> current.data) min = current.data; current = current.prev; } return min; } public int sum() { Node current = tail; int sum = 0; while (current!=null) { sum += current.data; current = current.prev; } return sum; } public int average() { Node current = tail; int n = 0; while (current!=null) { n++; current = current.prev; } return (int)sum()/n; } public void compare(DoublyLinkedList d1,DoublyLinkedList d2) { Node current_d1 = d1.head; Node current_d2; while (current_d1!=null) { current_d2 = d2.head; while (current_d2!=null) { if(current_d1.data==current_d2.data) { System.out.print(" ->" + current_d1.data); break; } current_d2 = current_d2.next; } current_d1 = current_d1.next; } System.out.println(); } public void search(int value) { Node current = tail; int n = 0; while (current!=null) { n++; if(value==current.data) { System.out.print(" "+current.data+" value is at node "+n); break; } current = current.prev; } System.out.println(); } public void replace(int search,int replace) { Node current = tail; while (current!=null) { if(search==current.data) { current.data = replace; } current = current.prev; } System.out.println(); print_t_to_h(); } } // Stack public class Stack { private int top,maxsize,stack[]; private String expression; Stack(String exp) { maxsize = exp.length(); this.expression = exp; top = -1; stack = new int[maxsize]; } public boolean isFull() { return (top==maxsize-1); } public boolean isEmpty() { return (top==-1); } public void push(int data) { if(!isFull()) { stack[++top] = data; } else { System.out.println("Stack is full"); } } public int pop() { if(!isEmpty()) { return stack[top--]; } else { return -1; } } public char[] convert() { char[] temp = new char[expression.length()]; for(int i=0;i<temp.length;i++) { temp[i] = expression.charAt(i); } return temp; } public void calculate() { char[] exp = convert(); for(char e:exp) { int a = e; if(a == 37 || a==42 || a==43 || a==45 || a==47) { int i=pop(); int j=pop(); char op = (char)a; switch (op) { case '*': push(j*i); break; case '+': push(j+i); break; case '-': push(j-i); break; case '/': push(j/i); break; case '%': push(j%i); break; } } else { a -=48; push(a); } } System.out.println("Result is : "+stack[top]); } } // Dynamic Array public class DynamicArray { private int count; private int[] array; DynamicArray() { count = 0; array = new int[3]; } public int size() { return count; } public void print() { if(count>0) { for(int i=0;i<count;i++) { System.out.println(" "+array[i]); } System.out.println(); } else { System.out.println("Empty Array...!"); } } public void insert(int item) { if(count==array.length) { int[] temp = new int[array.length*2]; for(int i=0;i<array.length;i++) { temp[i] = array[i]; } array = temp; } array[count] = item; count = count+1; } } public class DynamicArray { private int count; private int[] array; DynamicArray() { count = 0; array = new int[3]; } public int size() { return count; } public void print() { if(count>0) { for(int i=0;i<count;i++) { System.out.println(" "+array[i]); } System.out.println(); } else { System.out.println("Empty Array...!"); } } public void insert(int item) { if(count==array.length) { int[] temp = new int[array.length*2]; for(int i=0;i<array.length;i++) { temp[i] = array[i]; } array = temp; } array[count] = item; count = count+1; } } // Reverse public void reverse(int[] arr) { System.out.println(); System.out.printf("Reversed Array : "); int[] temp = new int[arr.length]; int count = arr.length - 1; for (int i = 0; i < arr.length; i++) { temp[count] = arr[i]; count--; } arr = temp; for (int i = 0; i < arr.length; i++) { System.out.printf("%d ", arr[i]); } } public void distinct(int[] arr) { System.out.printf("Distinct elements of your array: "); int[] distinctArray = new int[arr.length]; int count = 0; for (int i = 0; i < arr.length; i++) { boolean isDistinct = true; for (int j = 0; j < count; j++) { if (arr[i] == distinctArray[j]) { isDistinct = false; break; } } if (isDistinct) { distinctArray[count] = arr[i]; count++; } } for (int i = 0; i < count; i++) { System.out.printf("%d ", distinctArray[i]); } System.out.println(); } public void replace(int number, int replace) { boolean found = false; for (int i = 0; i < count; i++) { if (array[i] == number) { array[i] = replace; found = true; } } if (found) { System.out.println("Number was replaced"); } else { System.out.println("Number was not found for replacement"); } } // public class Sorting { public static void selection_sort(Information[] Information) { for(int i=0;i<Information.length-1;i++){ int min = i; for (int j=i+1;j<Information.length;j++){ if(Information[i].ticket>Information[j].ticket){ min = j; } } Information temp = Information[i]; Information[i] = Information[min]; Information[min] = temp; } } public static void linear_search(Information[] Information, int Value){ for(int i=0;i<Information.length;i++){ if(Value==Information[i].ticket){ System.out.println("Ticket #" + Information[i].ticket + " found"); break; } } System.out.println("Ticket not found"); } } { String name; int ticket; public Information(String name, int ticket) { this.name = name; this.ticket = ticket; } } public class Main { public static void main(String[] args) { Information Info[] = new Information[5]; Info[0] = new Information("Heyo",5); Info[1] = new Information("Heyod",10); Info[2] = new Information("Bruh",1); Info[3] = new Information("Faizoo",100); Info[4] = new Information("sleepy",6); for (int i = 0; i < Info.length; i++) { System.out.println("Name: "+Info[i].name+" Ticket: " + Info[i].ticket); } Sorting.selection_sort(Info); System.out.println("Post sorting: "); for (int i = 0; i < Info.length; i++) { System.out.println("Name: "+Info[i].name+" Ticket: " + Info[i].ticket); } Sorting.linear_search(Info,100); Sorting.linear_search(Info,3); } }
Leave a Comment