Untitled
unknown
plain_text
9 months ago
21 kB
7
Indexable
BST bst = new BST();
bst.create_tree(68);
bst.in_order();
public class BST {
private Node root;
public void create_root(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 search_node(int value) {
if (root == null) System.out.println("BST is empty");
while(root != null) {
if (root.data == value) {
System.out.println("Found!");
return;
}
if (value > root.data) {
root = root.Right;
} else if (value < root.data) {
root = root.Left;
}
}
System.out.println("Not found!");
}
public Node delete_node(Node root, int value) {
if(root == null) return null;
if (value < root.data) {
root.Left = delete_node(root.Left, value);
} else if (value > root.data) {
root.Right = delete_node(root.Right, value);
} else {
if (root.Left == null) {
return root.Right;
} else if (root.Right == null) {
return root.Left;
}
}
// case 3: Node with two children
root.data = findMin(root.Right);
root.Right = delete_node(root.Right, value);
return root;
}
public int findMin(Node node) {
while (node.Left != null) {
node = node.Left;
}
return node.data;
}
public void postorder() {
print_post_order(root);
}
public void print_in_order(Node node) {
if ( node != null) {
print_in_order(node.Left);
System.out.println(node.data + " ");
print_in_order(node.Right);
}
}
public void print_pre_order(Node node) {
if ( node != null) {
System.out.println(node.data + " ");
print_pre_order(node.Left);
print_pre_order(node.Right);
}
}
public void print_post_order(Node node) {
if ( node != null) {
print_post_order(node.Left);
print_post_order(node.Right);
System.out.println(node.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 Patient {
String Name;
String Condition;
int Priority;
String Illness;
Patient(String Name, String Condition, int Priority, String illness) {
this.Name = Name;
this.Condition = Condition;
this.Illness = illness;
this.Priority = Priority;
}
}
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 class Main {
public static void main(String[] args) {
Queue priorityQueue = new Queue(10);
priorityQueue.enqueue(new Patient("Ali", "High Fever", 4, "General"));
// Array Question
public class Main {
public static void main(String[] args) {
Information[] arr = {
new Information("Ali", 5),
new Information("Fatima", 2),
new Information("Ahmed", 8),
new Information("Sara", 1)
};
arr = Sorting.bubble_Sort(arr);
System.out.println("Sorted Information:");
for (Information info : arr) {
System.out.println("Name: " + info.getName() + ", Ticket: " + info.getTicket());
}
Sorting sorting = new Sorting();
int searchTicket = 8;
System.out.println("\nSearching for ticket " + searchTicket + "...");
sorting.linear_search(arr, searchTicket);
}
}
public class Information {
private String name;
private int ticket;
Information(String name, int ticket){
this.name = name;
this.ticket = ticket;
}
public int getTicket() {
return ticket;
}
public String getName() {
return name;
}
}
public class Sorting {
public static Information[] bubble_Sort(Information[] 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].getTicket() > arr[j + 1].getTicket()) {
Information temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
b = true;
}
}
if (b == false) {
break;
}
}
return arr;
}
public void linear_search(Information[] arr, int data){
for(int i = 1; i < arr.length; i++){
if(data == arr[i].getTicket()){
System.out.println("Item Found!");
}
System.out.println("Item Not Found!");
}
}
}
// 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");
}
}Editor is loading...
Leave a Comment