From b7e763b8a8b26c1381044ca087abb6270b7cd3b3 Mon Sep 17 00:00:00 2001 From: ChinmayRath2 <109583971+ChinmayRath2@users.noreply.github.com> Date: Fri, 28 Oct 2022 11:40:42 +0530 Subject: [PATCH 1/4] Binary Tree --- Binary Tree | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Binary Tree diff --git a/Binary Tree b/Binary Tree new file mode 100644 index 0000000..506be48 --- /dev/null +++ b/Binary Tree @@ -0,0 +1,68 @@ +public class SearchBinaryTree { + + //Represent a node of binary tree + public static class Node{ + int data; + Node left; + Node right; + + public Node(int data){ + //Assign data to the new node, set left and right children to null + this.data = data; + this.left = null; + this.right = null; + } + } + + //Represent the root of binary tree + public Node root; + + public static boolean flag = false; + + public SearchBinaryTree(){ + root = null; + } + + //searchNode() will search for the particular node in the binary tree + public void searchNode(Node temp, int value){ + //Check whether tree is empty + if(root == null){ + System.out.println("Tree is empty"); + } + else{ + //If value is found in the given binary tree then, set the flag to true + if(temp.data == value){ + flag = true; + return; + } + //Search in left subtree + if(flag == false && temp.left != null){ + searchNode(temp.left, value); + } + //Search in right subtree + if(flag == false && temp.right != null){ + searchNode(temp.right, value); + } + } + } + + public static void main(String[] args) { + + SearchBinaryTree bt = new SearchBinaryTree(); + //Add nodes to the binary tree + bt.root = new Node(1); + bt.root.left = new Node(2); + bt.root.right = new Node(3); + bt.root.left.left = new Node(4); + bt.root.right.left = new Node(5); + bt.root.right.right = new Node(6); + + //Search for node 5 in the binary tree + bt.searchNode(bt.root, 5); + + if(flag) + System.out.println("Element is present in the binary tree"); + else + System.out.println("Element is not present in the binary tree"); + } + } From 1d6df577142f4957dffa9e13faa74d51cd333614 Mon Sep 17 00:00:00 2001 From: ChinmayRath2 <109583971+ChinmayRath2@users.noreply.github.com> Date: Fri, 28 Oct 2022 11:46:06 +0530 Subject: [PATCH 2/4] Single_linkedlist --- Single_linkedlist | 113 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 Single_linkedlist diff --git a/Single_linkedlist b/Single_linkedlist new file mode 100644 index 0000000..95550d6 --- /dev/null +++ b/Single_linkedlist @@ -0,0 +1,113 @@ +public class InsertMid { + + //Represent a node of the singly linked list + class Node{ + int data; + Node next; + + public Node(int data) { + this.data = data; + this.next = null; + } + } + + public int size; + //Represent the head and tail of the singly linked list + public Node head = null; + public Node tail = null; + + //addNode() will add a new node to the list + public void addNode(int data) { + //Create a new node + Node newNode = new Node(data); + + //Checks if the list is empty + if(head == null) { + //If list is empty, both head and tail will point to new node + head = newNode; + tail = newNode; + } + else { + //newNode will be added after tail such that tail's next will point to newNode + tail.next = newNode; + //newNode will become new tail of the list + tail = newNode; + } + //Size will count the number of nodes present in the list + size++; + } + + //This function will add the new node at the middle of the list. + public void addInMid(int data){ + //Create a new node + Node newNode = new Node(data); + + //Checks if the list is empty + if(head == null) { + //If list is empty, both head and tail would point to new node + head = newNode; + tail = newNode; + } + else { + Node temp, current; + //Store the mid position of the list + int count = (size % 2 == 0) ? (size/2) : ((size+1)/2); + //Node temp will point to head + temp = head; + current = null; + + //Traverse through the list till the middle of the list is reached + for(int i = 0; i < count; i++) { + //Node current will point to temp + current = temp; + //Node temp will point to node next to it. + temp = temp.next; + } + + //current will point to new node + current.next = newNode; + //new node will point to temp + newNode.next = temp; + } + size++; + } + + //display() will display all the nodes present in the list + public void display() { + //Node current will point to head + Node current = head; + if(head == null) { + System.out.println("List is empty"); + return; + } + + while(current != null) { + //Prints each node by incrementing pointer + System.out.print(current.data + " "); + current = current.next; + } + System.out.println(); + } + + public static void main(String[] args) { + + InsertMid sList = new InsertMid(); + + //Adds data to the list + sList.addNode(1); + sList.addNode(2); + + System.out.println("Original list: "); + sList.display(); + + //Inserting node '3' in the middle + sList.addInMid(3); + System.out.println( "Updated List: "); + sList.display(); + + //Inserting node '4' in the middle + sList.addInMid(4); + System.out.println("Updated List: "); + sList.display(); + } +} From 9b4786a61a70f1eef2c62defaed093a31f137473 Mon Sep 17 00:00:00 2001 From: ChinmayRath2 <109583971+ChinmayRath2@users.noreply.github.com> Date: Fri, 28 Oct 2022 11:49:23 +0530 Subject: [PATCH 3/4] Bubblesort --- Bubblesort | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Bubblesort diff --git a/Bubblesort b/Bubblesort new file mode 100644 index 0000000..436662e --- /dev/null +++ b/Bubblesort @@ -0,0 +1,35 @@ +public class BubbleSortExample { + static void bubbleSort(int[] arr) { + int n = arr.length; + int temp = 0; + for(int i=0; i < n; i++){ + for(int j=1; j < (n-i); j++){ + if(arr[j-1] > arr[j]){ + //swap elements + temp = arr[j-1]; + arr[j-1] = arr[j]; + arr[j] = temp; + } + + } + } + + } + public static void main(String[] args) { + int arr[] ={3,60,35,2,45,320,5}; + + System.out.println("Array Before Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + System.out.println(); + + bubbleSort(arr);//sorting array elements using bubble sort + + System.out.println("Array After Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + + } +} From 3cd675ba0d9897de0a214019e3d023c2a7286209 Mon Sep 17 00:00:00 2001 From: ChinmayRath2 <109583971+ChinmayRath2@users.noreply.github.com> Date: Fri, 28 Oct 2022 11:53:22 +0530 Subject: [PATCH 4/4] Circular_LS --- Circular_LS | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Circular_LS diff --git a/Circular_LS b/Circular_LS new file mode 100644 index 0000000..966dbee --- /dev/null +++ b/Circular_LS @@ -0,0 +1,94 @@ +public class RemoveDuplicate { +//Represents the node of list. +public class Node{ +int data; +Node next; +public Node(int data) { +this.data = data; +} +} +//Declaring head and tail pointer as null. +public Node head = null; +public Node tail = null; +//This function will add the new node at the end of the list. +public void add(int data){ +//Create new node +Node newNode = new Node(data); +//Checks if the list is empty. +if(head == null) { + //If list is empty, both head and tail would point to new node. +head = newNode; +tail = newNode; +newNode.next = head; +} +else { +//tail will point to new node. +tail.next = newNode; +//New node will become new tail. +tail = newNode; +//Since, it is circular linked list tail will points to head. +tail.next = head; +} +} +//Removes duplicate from the list +public void removeDuplicate() { +//Current will point to head +Node current = head, index = null, temp = null; +if(head == null) { +System.out.println("List is empty"); +} +else { +do{ +//Temp will point to previous node of index. +temp = current; +//Index will point to node next to current +index = current.next; +while(index != head) { +//If current node is equal to index data +if(current.data == index.data) { +//Here, index node is pointing to the node which is duplicate of current node +//Skips the duplicate node by pointing to next node +temp.next = index.next; +} +else { +//Temp will point to previous node of index. +temp = index; +} +index= index.next; +} +current =current.next; +}while(current.next != head); +} +} +//Displays all the nodes in the list +public void display() { +Node current = head; +if(head == null) { +System.out.println("List is empty"); +} +else { + do{ +//Prints each node by incrementing pointer. +System.out.print(" "+ current.data); +current = current.next; +}while(current != head); +System.out.println(); +} +} +public static void main(String[] args) { +RemoveDuplicate cl = new RemoveDuplicate(); +//Adds data to the list +cl.add(1); +cl.add(2); +cl.add(3); +cl.add(2); +cl.add(2); +cl.add(4); +System.out.println("Originals list: "); +cl.display(); +//Removes duplicate nodes +cl.removeDuplicate(); +System.out.println("List after removing duplicates: "); +cl.display(); +} +}