Pages

20 June 2013
Tree Implementation Output
Output:
PreOrder
23
19
1
2
45
34
56
77
100
inOrder
1
2
19
23
34
45
56
77
100
postOrder
2
1
19
34
100
77
56
45
23
Read more ...

Tree Implementation Using Java

20 June 2013
package Data_structure;

class TNode{
    TNode left,right;
    int data;
    TNode(int value){
        data=value;
    }
}

public class TreeImplementation {
    TNode root;
    TreeImplementation(){
       
    }
    //insert Elements
    public void insert(int tdata){
        TNode newNode=new TNode(tdata);
        TNode current=root;
       
        if(root==null)root=newNode;
        else{
            while(true){
                if(current.data>tdata){
                    if(current.left==null){
                        current.left=newNode;
                        break;
                    }
                    current=current.left;
                }
               
                if(current.data<tdata){
                    if(current.right==null){
                        current.right=newNode;
                        break;
                    }
                    current=current.right;
                }
            }
        }
    }
   
   
   
    public void preOrder(TNode tem){
        if(tem!=null){
            System.out.println(tem.data);
            preOrder(tem.left);
            preOrder(tem.right);
        }
        else return;
    }

    public void inOrder(TNode tem){
        if(tem!=null){
            inOrder(tem.left);
            System.out.println(tem.data);
            inOrder(tem.right);
        }
        else return;
    }
   
    public void postOrder(TNode tem){
        if(tem!=null){
            postOrder(tem.left);
            postOrder(tem.right);
            System.out.println(tem.data);
        }
        else return;
    }
   
     public static void main(String [] shan){
         TreeImplementation bst=new TreeImplementation();
         bst.insert(23);
         bst.insert(19);
         bst.insert(1);
         bst.insert(45);
         bst.insert(56);
         bst.insert(34);
         bst.insert(2);
         bst.insert(77);
         bst.insert(100);
       
         System.out.println("PreOrder");
         bst.preOrder(bst.root);
       
         System.out.println("inOrder");
         bst.inOrder(bst.root);
       
         System.out.println("postOrder");
         bst.postOrder(bst.root);
     }
}
 
Input:
23 19 1 45 56 34 2 77 100

OutPut:
Read more ...

Simple Linked List Program Using Java

19 June 2013

import java.util.*;

class Node{
    int data;
    Node next;
    Node(int d){
        data=d;
        next=null;
    }
}

public class Linked_list {
  public static void main(String [] shan){
      Scanner input=new Scanner(System.in);
      int data,ch,size=0;
      Node head=null;
      Node tail=null;
     
      while(true){
          System.out.println("           1.Insert   2.Delete   3.Display  4.Exit  ");
          ch=input.nextInt();
          switch(ch){
          case 1:
          {
              System.out.println("Enter an Element.");
              data=input.nextInt();
              Node newNode=new Node(data);
             
              if(head==null){
                  head=newNode;
                  tail=head;
              }
              else{
                  tail.next=newNode;//Inserting Element
                  tail=newNode;
              }
              size++;
              System.out.println("Insertion SuccessFul :)");
              break;
          }
         
          case 2:
          {
              if(size>0){
              System.out.println(" If U want to delete something Enter element");
              data=input.nextInt();
              if(head.data==data)
                  head=head.next;
             
              else
              {
                  tail=head;
                  Node tem=head.next;
                  while(tem!=null){
                      if(tem.data==data){
                          tail.next=tem.next;
                          size--;
                          break;
                         
                      }
                      tail=tem;
                      tem=tem.next;
                  }
              }
              System.out.println("Ok..");
              }
             
              else
             
                  System.out.println("LinkedList is Empty");
              break;
          }
          case 3:
          {
              if(size>0){
                  System.out.println("Wait.......");
                  for(Node tem=head;tem!=null;tem=tem.next){
                      System.out.print(tem.data );
                      System.out.println();
                  }
              }
              else
                  System.out.println("LinkedList is Empty");
              break;
          }
          default:
              System.exit(0);
          }
      }
  }

}

Read more ...