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);
            }
        }
    }
}

Friday, 14 February 2020

Validation whether a string has only lower and upper case alphabets using regular expressions in java

String st="abcd#adl#lavjkllads";
st.matches("[a-z]*[A-Z]*");

but this will not take spaces into consideration

SO WE USE st.matches("[a-z A-Z]*");

Example Program
Develop a program to create a class "Dictionary" that contains the following information a)Name of a person b)Mobile number. The setter method raise an exceptions when name has any other characters apart from lower and upper case alphabets and when mobile number is negative or does not have 10 digits. The parameterized constructor calls the setter and throws the Exceptions. The main() method of DictionaryDemo must handle the exceptions.

package parent;

import java.util.Scanner;

class InvalidName extends RuntimeException
{
InvalidName(String st)
{
super(st);
}
}
class InvalidMobile extends RuntimeException
{
InvalidMobile(String st)
{
super(st);
}
}
class Dictionary
{
private String name;
private long mobile;
Dictionary(String name, long mobile)
{
if(setName(name) == false)
throw new InvalidName("Not a valid name");
if(setMobile(mobile) == false)
throw new InvalidMobile("Not a valid mobile number");
}
public boolean setName(String name)
{
if(name.matches("[a-z A-Z]*"))
{
this.name = name;
return true;
}
return false;
}

public boolean setMobile(long mobile)
{

if(mobile >=1000000000L && mobile <= 9999999999L)
{
this.mobile=mobile;
return true;
}
return false;
}
}
public class DictionaryDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter name and mobile number");
try
{
Dictionary ob = new Dictionary(sc.next(),sc.nextLong());
}
catch(InvalidName e)
{
e.printStackTrace();
}
catch(InvalidMobile e)
{
e.printStackTrace();
}
}

}

Thursday, 6 February 2020

Constructor Chaining in java

public class Chaining {
private  double l,b,h;
public Chaining()
{
l=b=h=10;
}
public Chaining(double le, double br, double he)
{
this();
setDimensions(le,br,he);

}
public boolean setDimensions(double le, double br, double he)
{
if(le >= 0 && br>=0 && he>=0)
{
l=le;
b=br;
h=he;
return true;
}
return false;
}

public String toString()
{
String output="Length = "+l+" Breadth = "+b+" Height = "+h;
return output;
}
public static void main(String args[])
{
Chaining ob = new Chaining(-10,2,3);
System.out.println(ob);

}


}