From 47e80b522571fc2d0f3fce2d0360593df8be0dce Mon Sep 17 00:00:00 2001 From: BhuvaKrishna <114555839+BhuvaKrishna@users.noreply.github.com> Date: Fri, 21 Oct 2022 13:41:01 +0530 Subject: [PATCH 1/8] AVLTree Program In Java --- AVLTreeProgram.java | 192 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 AVLTreeProgram.java diff --git a/AVLTreeProgram.java b/AVLTreeProgram.java new file mode 100644 index 0000000..0491737 --- /dev/null +++ b/AVLTreeProgram.java @@ -0,0 +1,192 @@ +// AVL tree implementation in Java + +// Create node +class Node { + int item, height; + Node left, right; + + Node(int d) { + item = d; + height = 1; + } +} + +// Tree class +class AVLTreeJava { + Node root; + + int height(Node N) { + if (N == null) + return 0; + return N.height; + } + + int max(int a, int b) { + return (a > b) ? a : b; + } + + Node rightRotate(Node y) { + Node x = y.left; + Node T2 = x.right; + x.right = y; + y.left = T2; + y.height = max(height(y.left), height(y.right)) + 1; + x.height = max(height(x.left), height(x.right)) + 1; + return x; + } + + Node leftRotate(Node x) { + Node y = x.right; + Node T2 = y.left; + y.left = x; + x.right = T2; + x.height = max(height(x.left), height(x.right)) + 1; + y.height = max(height(y.left), height(y.right)) + 1; + return y; + } + + // Get balance factor of a node + int getBalanceFactor(Node N) { + if (N == null) + return 0; + return height(N.left) - height(N.right); + } + + // Insert a node + Node insertNode(Node node, int item) { + + // Find the position and insert the node + if (node == null) + return (new Node(item)); + if (item < node.item) + node.left = insertNode(node.left, item); + else if (item > node.item) + node.right = insertNode(node.right, item); + else + return node; + + // Update the balance factor of each node + // And, balance the tree + node.height = 1 + max(height(node.left), height(node.right)); + int balanceFactor = getBalanceFactor(node); + if (balanceFactor > 1) { + if (item < node.left.item) { + return rightRotate(node); + } else if (item > node.left.item) { + node.left = leftRotate(node.left); + return rightRotate(node); + } + } + if (balanceFactor < -1) { + if (item > node.right.item) { + return leftRotate(node); + } else if (item < node.right.item) { + node.right = rightRotate(node.right); + return leftRotate(node); + } + } + return node; + } + + Node nodeWithMimumValue(Node node) { + Node current = node; + while (current.left != null) + current = current.left; + return current; + } + + // Delete a node + Node deleteNode(Node root, int item) { + + // Find the node to be deleted and remove it + if (root == null) + return root; + if (item < root.item) + root.left = deleteNode(root.left, item); + else if (item > root.item) + root.right = deleteNode(root.right, item); + else { + if ((root.left == null) || (root.right == null)) { + Node temp = null; + if (temp == root.left) + temp = root.right; + else + temp = root.left; + if (temp == null) { + temp = root; + root = null; + } else + root = temp; + } else { + Node temp = nodeWithMimumValue(root.right); + root.item = temp.item; + root.right = deleteNode(root.right, temp.item); + } + } + if (root == null) + return root; + + // Update the balance factor of each node and balance the tree + root.height = max(height(root.left), height(root.right)) + 1; + int balanceFactor = getBalanceFactor(root); + if (balanceFactor > 1) { + if (getBalanceFactor(root.left) >= 0) { + return rightRotate(root); + } else { + root.left = leftRotate(root.left); + return rightRotate(root); + } + } + if (balanceFactor < -1) { + if (getBalanceFactor(root.right) <= 0) { + return leftRotate(root); + } else { + root.right = rightRotate(root.right); + return leftRotate(root); + } + } + return root; + } + + void preOrder(Node node) { + if (node != null) { + System.out.print(node.item + " "); + preOrder(node.left); + preOrder(node.right); + } + } + + // Print the tree + private void printTree(Node currPtr, String indent, boolean last) { + if (currPtr != null) { + System.out.print(indent); + if (last) { + System.out.print("R----"); + indent += " "; + } else { + System.out.print("L----"); + indent += "| "; + } + System.out.println(currPtr.item); + printTree(currPtr.left, indent, false); + printTree(currPtr.right, indent, true); + } + } + + // Driver code + public static void main(String[] args) { + AVLTree tree = new AVLTree(); + tree.root = tree.insertNode(tree.root, 33); + tree.root = tree.insertNode(tree.root, 13); + tree.root = tree.insertNode(tree.root, 53); + tree.root = tree.insertNode(tree.root, 9); + tree.root = tree.insertNode(tree.root, 21); + tree.root = tree.insertNode(tree.root, 61); + tree.root = tree.insertNode(tree.root, 8); + tree.root = tree.insertNode(tree.root, 11); + tree.printTree(tree.root, "", true); + tree.root = tree.deleteNode(tree.root, 13); + System.out.println("After Deletion: "); + tree.printTree(tree.root, "", true); + } +} \ No newline at end of file From 8a7e63794f1e1048e00c8d824f578d4dcd74bffa Mon Sep 17 00:00:00 2001 From: rs2028 <114852198+rs2028@users.noreply.github.com> Date: Fri, 21 Oct 2022 14:39:40 +0530 Subject: [PATCH 2/8] Binary tree Code In Java --- BinaryTreeCode.java | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 BinaryTreeCode.java diff --git a/BinaryTreeCode.java b/BinaryTreeCode.java new file mode 100644 index 0000000..ef40744 --- /dev/null +++ b/BinaryTreeCode.java @@ -0,0 +1,59 @@ +class Node { + + int data; + Node left, right; + + Node(int d) { + data = d; + left = right = null; + } +} + +// Calculate height +class Height { + int height = 0; +} + +class BinaryTreeCode { + + Node root; + + // Check height balance + boolean checkHeightBalance(Node root, Height height) { + + // Check for emptiness + if (root == null) { + height.height = 0; + return true; + } + + Height leftHeighteight = new Height(), rightHeighteight = new Height(); + boolean l = checkHeightBalance(root.left, leftHeighteight); + boolean r = checkHeightBalance(root.right, rightHeighteight); + int leftHeight = leftHeighteight.height, rightHeight = rightHeighteight.height; + + height.height = (leftHeight > rightHeight ? leftHeight : rightHeight) + 1; + + if ((leftHeight - rightHeight >= 2) || (rightHeight - leftHeight >= 2)) + return false; + + else + return l && r; + } + + public static void main(String args[]) { + Height height = new Height(); + + BinaryTree tree = new BinaryTree(); + tree.root = new Node(1); + tree.root.left = new Node(2); + tree.root.right = new Node(3); + tree.root.left.left = new Node(4); + tree.root.left.right = new Node(5); + + if (tree.checkHeightBalance(tree.root, height)) + System.out.println("The tree is balanced"); + else + System.out.println("The tree is not balanced"); + } +} \ No newline at end of file From 2a912e65c3b83f02ad545c1f88f9116e8f49ed0c Mon Sep 17 00:00:00 2001 From: Yash Pansuriya <83862025+yash1603@users.noreply.github.com> Date: Fri, 21 Oct 2022 18:45:10 +0530 Subject: [PATCH 3/8] ReplaceASubstringInAString.java Program --- ReplaceASubstringInAString.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 ReplaceASubstringInAString.java diff --git a/ReplaceASubstringInAString.java b/ReplaceASubstringInAString.java new file mode 100644 index 0000000..fca73ca --- /dev/null +++ b/ReplaceASubstringInAString.java @@ -0,0 +1,15 @@ +import java.util.Scanner; +public class ReplaceASubstringInAString { +public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter a String : "); + String s1 = sc.nextLine(); + System.out.print("Enter the String to be replaced : "); + String oldString = sc.nextLine(); + System.out.print("Enter the new String : "); + String newString =sc.nextLine(); + + String replaceString = s1.replace(oldString, newString); + System.out.println("New String is :"+replaceString); + } +} From ced1e15bcf9f39e4611a5acb89a2f8ee47f6f586 Mon Sep 17 00:00:00 2001 From: rahulkacha <88592659+rahulkacha@users.noreply.github.com> Date: Fri, 28 Oct 2022 12:44:08 +0530 Subject: [PATCH 4/8] Update Hello.java --- Hello.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Hello.java b/Hello.java index 4e3f804..557db03 100644 --- a/Hello.java +++ b/Hello.java @@ -1 +1 @@ -Hello Wordld +Hello World From f3922a2cc119ca54a7b34fea63a9abc852b6ee25 Mon Sep 17 00:00:00 2001 From: Anushka Shelke <84145871+anushkashelke@users.noreply.github.com> Date: Fri, 28 Oct 2022 13:07:03 +0530 Subject: [PATCH 5/8] Create QuickSort.java Submitted Quick Sort in Java --- QuickSort.java | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 QuickSort.java diff --git a/QuickSort.java b/QuickSort.java new file mode 100644 index 0000000..1250b75 --- /dev/null +++ b/QuickSort.java @@ -0,0 +1,62 @@ +import java.util.Scanner; +class Sorting{ + void partition(int arr[],int start,int end){ + + int pivot=0 ,i=0,j=0; + if(startend) + break; + } + while(arr[j]>arr[pivot]) { + j--; + if(j Date: Fri, 28 Oct 2022 14:32:05 +0530 Subject: [PATCH 6/8] Calculator.java Calculator in java --- Calculator.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Calculator.java diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000..3983c27 --- /dev/null +++ b/Calculator.java @@ -0,0 +1,14 @@ +public class CalculatorSwitchCase +{ +//Ask input from user +switch(character) +{ +case '+'://addition operation +case '-'://subtraction operation +case '*'://multiplication operation +case '/'://division operation +case '%'://modulus operation +case '^'://power operation +} +//display output +} From 07ab94d97880ffbce610fd1349d261a6dec71994 Mon Sep 17 00:00:00 2001 From: Khushi Parmar <82652792+Khushikp@users.noreply.github.com> Date: Fri, 28 Oct 2022 19:40:11 +0530 Subject: [PATCH 7/8] Binary2Decimal.java Binary2Decimal Conversion using java --- Binary2Decimal.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Binary2Decimal.java diff --git a/Binary2Decimal.java b/Binary2Decimal.java new file mode 100644 index 0000000..cb197a0 --- /dev/null +++ b/Binary2Decimal.java @@ -0,0 +1,34 @@ +import java.util.Scanner; + +class Binary2Decimal +{ + public static int bin2Dec(String binaryString) throws NumberFormatException + { + int decimal = 0; + int strLength=binaryString.length(); + for (int i = 0; i < strLength; i++) + { + if (binaryString.charAt(i) < '0' || binaryString.charAt(i) > '1') + { + throw new NumberFormatException("Not a binary number..."); + } + + decimal += (binaryString.charAt(i)-'0') * Math.pow(2, strLength-1-i); + } + return decimal; + } + public static void main(String[] args) + { + Scanner input = new Scanner(System.in); + System.out.print("Enter Binary Value : "); + String str = input.nextLine(); + try + { + System.out.println("Value = " + bin2Dec(str)); + } + catch(NumberFormatException e) + { + System.out.println(e); + } + } +} From c97ff7d7d912eceab3fbf642c3caedbf273432e6 Mon Sep 17 00:00:00 2001 From: Tiwarinikkikumari <108176016+Tiwarinikkikumari@users.noreply.github.com> Date: Fri, 28 Oct 2022 21:20:13 +0530 Subject: [PATCH 8/8] palindrome.java Palindrome using java --- palindrome.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 palindrome.java diff --git a/palindrome.java b/palindrome.java new file mode 100644 index 0000000..dc3ad55 --- /dev/null +++ b/palindrome.java @@ -0,0 +1,17 @@ +class PalindromeExample{ + public static void main(String args[]){ + int r,sum=0,temp; + int n=454;//It is the number variable to be checked for palindrome + + temp=n; + while(n>0){ + r=n%10; //getting remainder + sum=(sum*10)+r; + n=n/10; + } + if(temp==sum) + System.out.println("palindrome number "); + else + System.out.println("not palindrome"); +} +}