Posts

Showing posts from February, 2022

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

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