Advertisement
Samuel_Berkat_Hulu

dsdsds

Jun 9th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class sd here.
  4.  *
  5.  * Samuel Berkat Hulu
  6.  * 5025201055
  7.  * @version 5.0 Tugas Sturktur Data 09-Juni-2021
  8.  */
  9. class BST_class {
  10.     class Node {
  11.         int key;
  12.         Node left, right;
  13.    
  14.         public Node(int data){
  15.             key = data;
  16.             left = right = null;
  17.         }
  18.     }
  19.    
  20.     Node root;
  21.     BST_class(){
  22.         root = null;
  23.     }
  24.  
  25.     void deleteKey(int key) {
  26.         root = delete_Recursive(root, key);
  27.     }
  28.  
  29.     Node delete_Recursive(Node root, int key)  {
  30.  
  31.         if (root == null)  return root;
  32.    
  33.         if (key < root.key)  
  34.             root.left = delete_Recursive(root.left, key);
  35.         else if (key > root.key)
  36.             root.right = delete_Recursive(root.right, key);
  37.         else  {
  38.  
  39.             if (root.left == null)
  40.                 return root.right;
  41.             else if (root.right == null)
  42.                 return root.left;
  43.    
  44.             root.key = minValue(root.right);
  45.    
  46.             root.right = delete_Recursive(root.right, root.key);
  47.         }
  48.         return root;
  49.     }
  50.    
  51.     int minValue(Node root)  {
  52.  
  53.         int minval = root.key;
  54.  
  55.         while (root.left != null)  {
  56.             minval = root.left.key;
  57.             root = root.left;
  58.         }
  59.         return minval;
  60.     }
  61.  
  62.     void insert(int key)  {
  63.         root = insert_Recursive(root, key);
  64.     }
  65.  
  66.     Node insert_Recursive(Node root, int key) {
  67.  
  68.         if (root == null) {
  69.             root = new Node(key);
  70.             return root;
  71.         }
  72.  
  73.         if (key < root.key)    
  74.             root.left = insert_Recursive(root.left, key);
  75.         else if (key > root.key)  
  76.             root.right = insert_Recursive(root.right, key);
  77.  
  78.         return root;
  79.     }
  80.  
  81.  
  82.     void inorder() {
  83.         inorder_Recursive(root);
  84.     }
  85.    
  86.  
  87.     void inorder_Recursive(Node root) {
  88.         if (root != null) {
  89.             inorder_Recursive(root.left);
  90.             System.out.print(root.key + " ");
  91.             inorder_Recursive(root.right);
  92.         }
  93.     }
  94.      
  95.     boolean search(int key)  {
  96.         root = search_Recursive(root, key);
  97.         if (root!= null)
  98.             return true;
  99.         else
  100.             return false;
  101.     }
  102.    
  103.  
  104.     Node search_Recursive(Node root, int key)  {
  105.  
  106.         if (root==null || root.key==key)
  107.             return root;
  108.  
  109.         if (root.key > key)
  110.             return search_Recursive(root.left, key);
  111.  
  112.         return search_Recursive(root.right, key);
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement