Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Binary Tree
Original file line number Diff line number Diff line change
@@ -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");
}
}
35 changes: 35 additions & 0 deletions Bubblesort
Original file line number Diff line number Diff line change
@@ -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] + " ");
}

}
}
94 changes: 94 additions & 0 deletions Circular_LS
Original file line number Diff line number Diff line change
@@ -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();
}
}
113 changes: 113 additions & 0 deletions Single_linkedlist
Original file line number Diff line number Diff line change
@@ -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();
}
}