Tuesday 31 March 2020

General Algorithm to insert a node into B tree


General Algorithm to insert a node into B tree
1.      Find the node into which new key should be inserted by doing search using binary search logic
2.      if the node is not full, insert the key into the node using an appropriate sorting algorithm
3.      if the node is full, split the node into two and push the key upward into the parent node
4.      if the parent node is also full, follow the same procedure until either some space is found in a previously existing node or a new root node is created

Friday 6 March 2020

Infix to Postfix Conversion in java

Step 1: Start
Step 2: Read the infix expression
Step 3: Scan every character of infix expression,
IF scanned character is
3.1 ( , then  push it on the stack
3.2 operand (whether a digit or a character) , add it to postfix expression
3.3  ) , then
a. Repeatedly pop from stack and add it to the postfix expression until  ( .
b. Discard the ( . That is, remove the ( from stack and do not add it to the postfix
3.4 operator, then
a. Repeatedly pop from stack and add each operator (popped from the stack) to the postfix expression which has the same precedence or a higher precedence
b. Push the operator to the stack

Step 4: Repeatedly pop from the stack and add it to the postfix expression until the stack is empty
Step 5: Stop


import java.util.Scanner;

class SNode
{
    char data;
    SNode next;
}
class Stack
{
    SNode top=null;
   public void push(char val)
    {
        SNode newnode = new SNode();
        newnode.data=val;
        if(top == null)
            top=newnode;
        else
        {
            newnode.next=top;
            top=newnode;
        }   
    }
    public char pop()
    {
            char temp=top.data;
            top=top.next;
            return temp;
    }
}

public class PostfixConversionDemo
{
    public static int getPriority(char ch)
    {
        if(ch == '*' || ch == '/' || ch == '%')
                return 2;
        if(ch == '-' || ch == '+')
            return 1;
        return 0;
    }
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        Stack ob = new Stack();
        System.out.println("Enter infix experession");
        StringBuffer infix=new StringBuffer(sc.next());
        StringBuffer postfix=new StringBuffer();
        char temp;
        for(int i=0;i<infix.length();i++)
        {
            char ch=infix.charAt(i);
            if(ch == '(')
                ob.push(ch);
            else if(ch == ')')
            {
                while( ob.top.data != '(')
                {
                     postfix.append(ob.pop());
                }
           
                ob.pop();
            }
            else if(ch == '*' ||ch == '/' ||ch == '+' ||ch == '-'|| ch == '%' )
            {
                while(ob.top != null && getPriority(ob.top.data) >=  getPriority(ch))
                {
                    postfix.append(ob.pop());                   
                }
                ob.push(ch);
            }
            else
                postfix.append(ch);
        }
        while(ob.top != null)
            postfix.append(ob.pop());
        System.out.println(postfix);
    }
}

Tuesday 3 March 2020

Single Linked List Program in Java

import java.util.Scanner;
class SLLNode
{
    int data;
    SLLNode next;
}
class SLL
{
    SLLNode head=null,tail=null;
    public void create(int val)
    {
        SLLNode newnode = new SLLNode();
        newnode.data=val;
        newnode.next=null;
        if(head == null)
            head=tail=newnode;
        else
        {
            tail.next=newnode;
            tail=newnode;
        }
    }
    public void display()
    {
        if(head == null)
             System.out.println("no SLL");
        else
        {
            SLLNode c=head;
            while(c != null)
            {
                System.out.print(c.data+"->");
                c=c.next;
            }
        }
    }
    public void insertBeg(int val)
    {
        SLLNode newnode = new SLLNode();
        newnode.data=val;
        newnode.next=null;
        if(head == null)
            head=tail=newnode;
        else
        {
            newnode.next=head;
            head=newnode;
        }
       
    }
    public void insertMiddle(int val,int pos)
    {
        SLLNode newnode = new SLLNode();
        newnode.data=val;
        newnode.next=null;
        SLLNode c = head;
        for(int i=1;i<pos-1;i++)
            c=c.next;
        newnode.next=c.next;
        c.next=newnode;
       
    }
    public void delEnd()
    {
        SLLNode c = head;
        while(c.next != null)
            c=c.next;
        System.out.println("deleted element = "+c.data);
        c.next=null;
    }
    public void delMiddle(int pos)
    {
        SLLNode c = head;
        for(int i=1;i<pos-1;i++)
            c=c.next;
        System.out.println("deleted element = "+c.next.data);
        c.next=c.next.next;
    }
    public void delBeg()
    {
        if(head != null)
        {
            System.out.println("deleted element = "+head.data);
            head=head.next;
        }
    }
}

public class SLLDemo
{
    public static void main(String args[])
    {
      SLL ob = new SLL();
      Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("1. InsertEnd/Create 2.InserBeg 3. InsertMiddle");
            System.out.println("4. DeleteEnd  5.DeleteBeg   6. DeleteMiddle");
            System.out.println("7. Display 8.Exit");
            int choice=sc.nextInt();
            switch(choice)
            {
                case 1:
                case 2:
                case 3:
                    System.out.println("");
                    int choice1=sc.nextInt();
                    System.out.println("Enter data to be inserted");
                    int val = sc.nextInt();
                    if(choice == 1)
                        ob.create(val);
                    else if(choice == 2)
                        ob.insertBeg(val);
                    else
                    {
                        System.out.println("Enter position");
                        int pos=sc.nextInt();
                        ob.insertMiddle(val,pos);
                    }
                    break;
                case 4:   ob.delEnd();
                            break;
                case 5:   ob.delBeg();
                            break;
                case 6: 
                        System.out.println("Enter position");
                        int pos=sc.nextInt();
                        ob.delMiddle(pos);
                        break;
                case 7:
                        ob.display();
                        break;
                default:
                        System.exit(0);
            }
        }
    }
}

===========================================
import java.util.Scanner;
class SLLNode
{
    int data;
    SLLNode next;
}
class SLL
{
    SLLNode head=null,tail=null;
    public void create(int val)
    {
        SLLNode newnode = new SLLNode();
        newnode.data=val;
        newnode.next=null;
        if(head == null)
            head=tail=newnode;
        else
        {
            tail.next=newnode;
            tail=newnode;
        }
    }
    public void display()
    {
        if(head == null)
             System.out.println("no SLL");
        else
        {
            SLLNode c=head;
            while(c != null)
            {
                System.out.print(c.data+"->");
                c=c.next;
            }
        }
    }
    public void insertBeg(int val)
    {
        SLLNode newnode = new SLLNode();
        newnode.data=val;
        newnode.next=null;
        if(head == null)
            head=tail=newnode;
        else
        {
            newnode.next=head;
            head=newnode;
        }
     
    }
    public void insertMiddle(int val,int pos)
    {
        SLLNode newnode = new SLLNode();
        newnode.data=val;
        newnode.next=null;
        SLLNode c = head;
        for(int i=1;i<pos-1;i++)
            c=c.next;
        newnode.next=c.next;
        c.next=newnode;
     
    }

}

public class SLLDemo
{
    public static void main(String args[])
    {
      SLL ob = new SLL();
      Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("1. Insert 2. Display 3.Exit");
            int choice=sc.nextInt();
            switch(choice)
            {
                case 1:
                    System.out.println("1. InsertAtEnd 2.InsertAtBeg 3. InsertAtMiddle");
                    int choice1=sc.nextInt();
                    System.out.println("Enter data to be inserted");
                    int val = sc.nextInt();
                    switch(choice1)
                    {
                        case 1:   ob.create(val);
                                    break;
                        case 2:   ob.insertBeg(val);
                                    break;
                        case 3:   System.out.println("Enter position");
                                    int pos=sc.nextInt();
                                    ob.insertMiddle(val,pos);
                                    break;
                    }
                    break;
                case 2:   ob.display();
                            break;
                default:
                        System.exit(0);
            }
        }
    }
}