Posts

Showing posts with the label Data Structures with java

avl tree implementation in java

Image
  avl tree implementation in java public class avlTree { class Node{ int data ; Node left ; Node right ; int height ; Node ( int data){ this . data =data ; this . left = null; this . right = null; this . height = 0 ; } } public int getheight (Node n){ if (n== null ) return - 1 ; int lh=getheight(n. left ) ; int rh=getheight(n. right ) ; int fh=Math. max (lh , rh)+ 1 ; return fh ; } public int getbalancefac (Node n){ if (n== null ) return 0 ; return getheight(n. left )-getheight(n. right ) ; } public Node rightRotate (Node a){ Node b=a. left ; Node br=b. right ; b. right =a ; a. left =br ; a. height =Math. max (getheight(a. left ) , getheight(a. right ))+ 1 ; b. height =Math. max (getheight(b. left ) , getheight(b. right ))+ 1 ; return b ; } ...

searching and deletion in binary search tree in java

Image
 searching and deletion in binary search tree java hindi public class binarysearch { class Node{ int data ; Node left ; Node right ; Node ( int data){ this . data =data ; this . left = null; this . right = null; } } public Node addtress (Node root ,int values){ if (root== null ){ return new Node(values) ; } if (values< root. data ){ root. left =addtress(root. left , values) ; } else { root. right =addtress(root. right , values) ; } return root ; } public void inorder (Node root){ if (root== null ) return; inorder(root. left ) ; System. out .print(root. data + " " ) ; inorder(root. right ) ; } public Node search (Node root ,int value){ if (root== null ) return null; if (root. data == value){ return root ; } if (r...

binary search tree implementation in java hindi

Image
  binary search tree implementation in java hindi public class binarysearch { class Node{ int data ; Node left ; Node right ; Node ( int data){ this . data =data ; this . left = null; this . right = null; } } public Node addtress (Node root ,int values){ if (root== null ){ return new Node(values) ; } if (values< root. data ){ root. left =addtress(root. left , values) ; } else { root. right =addtress(root. right , values) ; } return root ; } public void inorder (Node root){ if (root== null ) return; inorder(root. left ) ; System. out .print(root. data + " " ) ; inorder(root. right ) ; } public static void main (String[] args) { binarysearch bss= new binarysearch() ; Node root= null; root=bss.addtress(root , 10 ) ; root...

binary tree implementation and traversal in java

Image
 binary tree implementation and traversal in java binary tree preorder,postorder,inorder java code implemenattion by bramastra trick import java.util.Scanner ; public class binarytrees { class Node{ int data ; Node left ; Node right ; Node ( int data){ this . data =data ; this . left = null; this . right = null; } } Scanner sc = new Scanner(System. in ) ; public Node addtree (){ Node root= null; System. out .println( "enter data" ) ; int data= sc .nextInt() ; if (data==- 1 ){ return null; } root= new Node(data) ; System. out .println( "enter left child of root " + data) ; root. left =addtree() ; System. out .println( "enter right child of root " + data) ; root. right =addtree() ; return root ; } public void inorder (Node root){ if (root== null ) retur...

circular queue implementation java

Image
  circular queue implementation java public class circ_queue { int front =- 1 , rear =- 1 ; int size = 5 ; int [] arr = new int [ size ] ; public void enqueue ( int data){ if ( front ==( rear + 1 )% size ){ System. out .println( "overflow" ) ; } else { if ( front ==- 1 && rear ==- 1 ){ front ++ ; rear ++ ; arr [ rear ]=data ; } else { rear =( rear + 1 )% size ; arr [ rear ]=data ; } } } public void dequeue (){ if ( front ==- 1 && rear ==- 1 ){ System. out .println( "underflow" ) ; } else if ( front == rear ){ front =- 1 ; rear =- 1 ; } else { front =( front + 1 )% size ; } } public void display (){ if ( front ==- 1 && rear ==- 1 ){ System. ...

Queue implementation using linkedlist in java

Image
  Queue implementation using linkedlist in java public class queue_ll { class Node{ int data ; Node next ; Node ( int data){ this . data =data ; this . next = null; } } Node front = null; Node rear = null; public void enqueue ( int data){ Node toadd= new Node(data) ; if ( front == null ){ front =toadd ; rear =toadd ; } else { rear . next =toadd ; rear =toadd ; } } public void dequeue (){ if ( front == null ){ System. out .println( "underflow" ) ; } else { front = front . next ; } } public void display (){ Node temp= front ; while (temp!= null ){ System. out .println(temp. data ) ; temp=temp. next ; } } public static void main (String[] args) { queue_ll qll= new queue_ll() ; ...

Queue Implementation using array in Java

Image
 Queue implementation in Java hindi public class queue_ary { int front =- 1 , rear =- 1 ; int size = 5 ; int [] arr = new int [ size ] ; public void enqueue ( int data){ if ( rear ==( size - 1 )){ System. out .println( "overflow" ) ; } else { if ( front ==- 1 && rear ==- 1 ){ front ++ ; rear ++ ; arr [ rear ]=data ; } else { rear ++ ; arr [ rear ]=data ; } } } public void dequeue (){ if ( front ==- 1 && rear ==- 1 ){ System. out .println( "empty" ) ; } else { for ( int i= 0 ; i< rear ; i++){ arr [i]= arr [i+ 1 ] ; } rear -- ; } } public void display (){ if ( front ==- 1 && rear ==- 1 ){ System. out .println( "empty" ) ; ...

stack implementation using linked list in java

Image
  stack implementation using linked list in java public class linkstack { class Node{ int data ; Node next ; Node ( int data){ this . data =data ; this . next = null; } } Node head = null; int size ; public void push ( int data){ Node newnode= new Node(data) ; size ++ ; if ( head == null ){ head =newnode ; } else { newnode. next = head ; head =newnode ; } } public void pop (){ if ( head == null ){ System. out .println( "underflow" ) ; } else { size -- ; head = head . next ; } } public void display (){ Node temp= head ; if (temp== null ){ System. out .println( "empty" ) ; } while (temp!= null ){ System. out .println(temp. data ) ; temp=temp. next ; } ...

Implementation of stack using Array in java

Image
 stack array implementation in java public class stacks { int size ; int arr [] ; int top ; stacks ( int size){ this . size =size ; this . arr = new int [size] ; this . top =- 1 ; } public void push ( int data){ if ( top ==( size - 1 )){ System. out .println( "overflow" ) ; } else { top ++ ; arr [ top ]=data ; System. out .println( "push elem " + data) ; } } public void display (){ int temp= top ; for ( int i=temp ; temp>= 0 ; temp--){ System. out .println( arr [temp]) ; } } public void peek (){ if ( top ==- 1 ){ System. out .println( "empty stack" ) ; } else { System. out .println( " peek is " + arr [ top ]) ; } } public void pop (){ if ( top ==- 1 ){ System. out .println( "empty...

circular doubly linked list deletion at begining

Image
 first node delete circular doubly linked list public class cdll { class Node { int data ; Node prev ; Node next ; Node ( int data) { this . data = data ; this . next = null; this . prev = null; } } Node head = null; Node tail = null; public void toadd ( int data){ Node newnode= new Node(data) ; if ( head == null ){ head =newnode ; tail =newnode ; newnode. next = head ; newnode. prev = tail ; } else { tail . next =newnode ; newnode. prev = tail ; tail =newnode ; tail . next = head ; head . prev = tail ; } } public void dis (){ Node temp= head ; if ( head == null ){ System. out .println( "empty" ) ; } else { do { System. out .println(temp. data ) ; ...

circular doubly linked list deletion at end

Image
 circular doubly linked list deletion last node public class cdll { class Node { int data ; Node prev ; Node next ; Node ( int data) { this . data = data ; this . next = null; this . prev = null; } } Node head = null; Node tail = null; public void toadd ( int data){ Node newnode= new Node(data) ; if ( head == null ){ head =newnode ; tail =newnode ; newnode. next = head ; newnode. prev = tail ; } else { tail . next =newnode ; newnode. prev = tail ; tail =newnode ; tail . next = head ; head . prev = tail ; } } public void dis (){ Node temp= head ; if ( head == null ){ System. out .println( "empty" ) ; } else { do { System. out .println(temp. data ) ; ...

circular doubly linked list insertion at begining in java

Image
  circular doubly linked list insertion at begining in java public class cdll { class Node { int data ; Node prev ; Node next ; Node ( int data) { this . data = data ; this . next = null; this . prev = null; } } Node head = null; Node tail = null; public void toadd ( int data){ Node newnode= new Node(data) ; if ( head == null ){ head =newnode ; tail =newnode ; newnode. next = head ; newnode. prev = tail ; } else { tail . next =newnode ; newnode. prev = tail ; tail =newnode ; tail . next = head ; head . prev = tail ; } } public void dis (){ Node temp= head ; if ( head == null ){ System. out .println( "empty" ) ; } else { do { System. out .println(temp. data )...