afiqakraam

mainBST

Jun 9th, 2021 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. class Main{
  2.     public static void main(String[] args)  {
  3.        //create a BST object
  4.         BST_class bst = new BST_class();
  5.         /* BST tree example
  6.               45
  7.            /     \
  8.           10      90
  9.          /  \    /  
  10.         7   12  50   */
  11.         //insert data into BST
  12.         bst.insert(45);
  13.         bst.insert(10);
  14.         bst.insert(7);
  15.         bst.insert(12);
  16.         bst.insert(90);
  17.         bst.insert(50);
  18.         //print the BST
  19.         System.out.println("The BST Created with input data(Left-root-right):");
  20.         bst.inorder();
  21.        
  22.         //delete leaf node  
  23.         System.out.println("\nThe BST after Delete 12(leaf node):");
  24.         bst.deleteKey(12);
  25.         bst.inorder();
  26.         //delete the node with one child
  27.         System.out.println("\nThe BST after Delete 90 (node with 1 child):");
  28.         bst.deleteKey(90);
  29.         bst.inorder();
  30.                  
  31.         //delete node with two children  
  32.         System.out.println("\nThe BST after Delete 45 (Node with two children):");
  33.         bst.deleteKey(45);
  34.         bst.inorder();
  35.         //search a key in the BST
  36.         boolean ret_val = bst.search (50);
  37.         System.out.println("\nKey 50 found in BST:" + ret_val );
  38.         ret_val = bst.search (12);
  39.         System.out.println("\nKey 12 found in BST:" + ret_val );
  40.      }
  41. }
Add Comment
Please, Sign In to add comment