Monday, January 25, 2016

Implementation of Binary Tree in Java

package BinaryTree;

public class BinaryTreeNode {

public int data;
public BinaryTreeNode left;
public BinaryTreeNode right;

public BinaryTreeNode(int data) {
this.data = data;
left = null;
right = null;
}

public int getData() {
return data;
}

public void setData(int data) {
this.data = data;
}

public BinaryTreeNode getLeft() {
return left;
}

public void setLeft(BinaryTreeNode left) {
this.left = left;
}

public BinaryTreeNode getRight() {
return right;
}

public void setRight(BinaryTreeNode right) {
this.right = right;
}

public String toString(BinaryTreeNode root) {
String result = "[";

while(root.getLeft()!=null){
result = result + root.getData();
}

while(root.getRight()!=null){
result = result + root.getData();
}

result+="]";
return result;
}

/* public String toString() {
String result = "";
result = result+"--" + data + "Left Child:-" + left + "Right Child:-" + right + "\n";
return result;
}*/
}




package BinaryTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;



public class testBinaryTree {
int diameter = 0;
int left, right;

public BinaryTreeNode insertBinartTree(BinaryTreeNode root, int data) {
if (root == null) {
return null;
}
Queue<BinaryTreeNode> q = new LinkedList<BinaryTreeNode>();
q.offer(root);
while (!q.isEmpty()) {
BinaryTreeNode tmp = q.poll();
/**
* here both if condition working parallel.
*/
if (tmp != null) {
if (tmp.getLeft() != null)
q.offer(tmp.getLeft());
else {
tmp.left = new BinaryTreeNode(data);
return root;
}
}
if (tmp.getRight() != null) {
q.offer(tmp.right);
} else {
tmp.right = new BinaryTreeNode(data);
return root;
}
}

return root;
}

}

Reverse Linked List Recursive in Java

package DataStructures;

public class LinkedListReverse {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

LinkedList linkedList = new LinkedList(5);
linkedList.next = new LinkedList(4);
linkedList.next.next = new LinkedList(3);
linkedList.next.next.next = new LinkedList(2);
linkedList.next.next.next.next = new LinkedList(1);
System.out.println("original linked list:-" + linkedList.toString());
linkedList = recursiveReverse(linkedList);
System.out.println("Recursively reverse:-" + linkedList.toString());

reverseListIterative(linkedList);

}

public static LinkedList recursiveReverse(LinkedList linkedList) {
if (linkedList == null || linkedList.next == null) {
return linkedList;
}
LinkedList remainingReverse = recursiveReverse(linkedList.next);
LinkedList current = remainingReverse;
while (current.next != null) {
current = current.next;
}
current.next = linkedList;
linkedList.next = null;
return remainingReverse;

}

}

Reverse Linked List Node In Java

    //Utility function to reverse the given list
      //Ex: Input : 1->2->3
      //Output : 3->2->1
      static ListNode reverse(ListNode list){
             if(list == null)
                   return null;

             ListNode current = list, previous = null, forward;
             while(current != null){
                    forward = current.next;
                    current.next = previous;
                    previous = current;
                    current = forward;
             }
             return previous;
      }

Add Two Linked List Data in Java




Input:
  First List: 5->6->3  // represents number 365
  Second List: 8->4->2 //  represents number 248
Output
  Resultant list: 3->1->6  // represents number 613

 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
      int carry =0;

       ListNode newHead = new ListNode(0);
       ListNode p1 = l1, p2 = l2, p3=newHead;

       while(p1 != null || p2 != null){
           if(p1 != null){
               carry += p1.getData();
               p1 = p1.next;
           }

           if(p2 != null){
               carry += p2.getData();
               p2 = p2.next;
           }

           p3.next = new ListNode(carry%10);
           p3 = p3.next;
           carry /= 10;
       }

       if(carry==1)
           p3.next=new ListNode(1);

       return newHead.next;
   }

Reverse K nodes in Java



Example:
Inputs:  1->2->3->4->5->6->7->8->NULL and k = 3 
Output:  3->2->1->6->5->4->8->7->NULL. 

Inputs:   1->2->3->4->5->6->7->8->NULL and k = 5
Output:  5->4->3->2->1->8->7->6->NULL. 



public ListNode reverseKnodes(ListNode head, int k) {
ListNode current = head;
ListNode prevTail = null;
ListNode prevCurrent = head;
while (current != null) {
int count = k;
ListNode tail = null;
while (current != null && count > 0) {
ListNode next = current.getNext();
current.setNext(tail);
tail = current;
current = next;
count--;
}
if (prevTail != null) {
prevTail.setNext(tail);
} else {
head = tail;
}
prevTail = prevCurrent;
prevCurrent = current;
}
return head;

}

Find Loop Exists in linkedList in Java








Image result for loop exists linked list java







public boolean findIfLoopExists(ListNode head) {
ListNode fastPrt = head;
ListNode slowPtr = head;
while (fastPrt != null && fastPrt.getNext() != null) {
fastPrt = fastPrt.getNext().getNext();
slowPtr = slowPtr.getNext();
if (slowPtr == fastPrt) {
return true;
} else {
return false;
}
}
return false;
}

Merge Two Sorted List in java



public ListNode mergeTwoSortedList(ListNode head1, ListNode head2){
ListNode head = new ListNode(0);
ListNode current = head;
while(head1!= null && head2!= null){
if(head.getData() <= head2.getData()){
current.next = head1;
head1 = head1.getNext();
}
else{
current.next = head2;
head2 = head2.getNext();
}
}
if(head1!= null){
current.next = head1;
}
else if(head2!= null){
current.next = head2;
}

return head.next;
}