Josif_tepe

Untitled

Jan 11th, 2026
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.15 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. //
  4. // CONSTRUCTION: with no initializer
  5. //
  6. // ******************PUBLIC OPERATIONS*********************
  7. // void insert( x )       --> Insert x
  8. // void remove( x )       --> Remove x
  9. // Comparable find( x )   --> Return item that matches x
  10. // Comparable findMin( )  --> Return smallest item
  11. // Comparable findMax( )  --> Return largest item
  12. // boolean isEmpty( )     --> Return true if empty; else false
  13. // void makeEmpty( )      --> Remove all items
  14. // void printTree( )      --> Print tree in sorted order
  15. /**
  16.  * Implements an unbalanced binary search tree.
  17.  * Note that all "matching" is based on the compareTo method.
  18.  * @author Mark Allen Weiss
  19.  */
  20. class BinarySearchTree<E extends Comparable<E>> {
  21.  
  22.     /** The tree root. */
  23.     private BNode<E> root;
  24.  
  25.     /**
  26.      * Construct the tree.
  27.      */
  28.     public BinarySearchTree() {
  29.         root = null;
  30.     }
  31.  
  32.     /**
  33.      * Insert into the tree; duplicates are ignored.
  34.      * @param x the item to insert.
  35.      */
  36.     public void insert(E x) {
  37.         root = insert(x, root);
  38.     }
  39.  
  40.     /**
  41.      * Remove from the tree. Nothing is done if x is not found.
  42.      * @param x the item to remove.
  43.      */
  44.     public void remove(E x) {
  45.         root = remove(x, root);
  46.     }
  47.  
  48.     /**
  49.      * Find the smallest item in the tree.
  50.      * @return smallest item or null if empty.
  51.      */
  52.     public E findMin() {
  53.         return elementAt(findMin(root));
  54.     }
  55.  
  56.     /**
  57.      * Find the largest item in the tree.
  58.      * @return the largest item of null if empty.
  59.      */
  60.     public E findMax() {
  61.         return elementAt(findMax(root));
  62.     }
  63.  
  64.     /**
  65.      * Find an item in the tree.
  66.      * @param x the item to search for.
  67.      * @return the matching item or null if not found.
  68.      */
  69.     public BNode<E> find(E x) {
  70.         return find(x, root);
  71.     }
  72.  
  73.     /**
  74.      * Make the tree logically empty.
  75.      */
  76.     public void makeEmpty() {
  77.         root = null;
  78.     }
  79.  
  80.     /**
  81.      * Test if the tree is logically empty.
  82.      * @return true if empty, false otherwise.
  83.      */
  84.     public boolean isEmpty() {
  85.         return root == null;
  86.     }
  87.  
  88.     /**
  89.      * Print the tree contents in sorted order.
  90.      */
  91.     public void printTree() {
  92.         if (isEmpty()) {
  93.             System.out.println("Empty tree");
  94.         } else {
  95.             printTree(root);
  96.         }
  97.     }
  98.  
  99.     /**
  100.      * Internal method to get element field.
  101.      * @param t the node.
  102.      * @return the element field or null if t is null.
  103.      */
  104.     private E elementAt(BNode<E> t) {
  105.         if (t == null)
  106.             return null;
  107.         return t.info;
  108.     }
  109.  
  110.     /**
  111.      * Internal method to insert into a subtree.
  112.      * @param x the item to insert.
  113.      * @param t the node that roots the tree.
  114.      * @return the new root.
  115.      */
  116.     private BNode<E> insert(E x, BNode<E> t) {
  117.         if (t == null) {
  118.             t = new BNode<E>(x, null, null);
  119.         } else if (x.compareTo(t.info) < 0) {
  120.             t.left = insert(x, t.left);
  121.         } else if (x.compareTo(t.info) > 0) {
  122.             t.right = insert(x, t.right);
  123.         } else;  // Duplicate; do nothing
  124.         return t;
  125.     }
  126.  
  127.     /**
  128.      * Internal method to remove from a subtree.
  129.      * @param x the item to remove.
  130.      * @param t the node that roots the tree.
  131.      * @return the new root.
  132.      */
  133.     @SuppressWarnings({"raw", "unchecked"})
  134.     private BNode<E> remove(Comparable x, BNode<E> t) {
  135.         if (t == null)
  136.             return t;   // Item not found; do nothing
  137.  
  138.         if (x.compareTo(t.info) < 0) {
  139.             t.left = remove(x, t.left);
  140.         } else if (x.compareTo(t.info) > 0) {
  141.             t.right = remove(x, t.right);
  142.         } else if (t.left != null && t.right != null) { // Two children
  143.             t.info = findMin(t.right).info;
  144.             t.right = remove(t.info, t.right);
  145.         } else {
  146.             if (t.left != null)
  147.                 return t.left;
  148.             else
  149.                 return t.right;
  150.         }
  151.         return t;
  152.     }
  153.  
  154.     /**
  155.      * Internal method to find the smallest item in a subtree.
  156.      * @param t the node that roots the tree.
  157.      * @return node containing the smallest item.
  158.      */
  159.     private BNode<E> findMin(BNode<E> t) {
  160.  
  161.         /*
  162.         if (t == null) {
  163.             return null;
  164.         } else if (t.left == null) {
  165.             return t;
  166.         }
  167.         return findMin(t.left);
  168.     */
  169.  
  170.         if(t==null) return null;
  171.  
  172.  
  173.         if(t.left!= null) return findMin(t.left);
  174.  
  175.         return t;
  176.  
  177.     }
  178.  
  179.  
  180.     /**
  181.      * Internal method to find the largest item in a subtree.
  182.      * @param t the node that roots the tree.
  183.      * @return node containing the largest item.
  184.      */
  185.     private BNode<E> findMax(BNode<E> t) {
  186.         if (t == null) {
  187.             return null;
  188.         } else if (t.right == null) {
  189.             return t;
  190.         }
  191.         return findMax(t.right);
  192.     }
  193.  
  194.     /**
  195.      * Internal method to find an item in a subtree.
  196.      * @param x is item to search for.
  197.      * @param t the node that roots the tree.
  198.      * @return node containing the matched item.
  199.      */
  200.     private BNode<E> find(E x, BNode<E> t) {
  201.         if (t == null)
  202.             return null;
  203.  
  204.         if (x.compareTo(t.info) < 0) {
  205.             return find(x, t.left);
  206.         } else if (x.compareTo(t.info) > 0) {
  207.             return find(x, t.right);
  208.         } else {
  209.             return t;    // Match
  210.         }
  211.     }
  212.  
  213.     /**
  214.      * Internal method to print a subtree in sorted order.
  215.      * @param t the node that roots the tree.
  216.      */
  217.     private void printTree(BNode<E> t) {
  218.         if (t != null) {
  219.             printTree(t.left);
  220.             System.out.println(t.info);
  221.             printTree(t.right);
  222.         }
  223.     }
  224.  
  225.     public void printTreeWithIndent() {
  226.         printTreeWithIndent(root, 0);
  227.     }
  228.  
  229.     private void printTreeWithIndent(BNode<E> t, int indent) {
  230.         if (t != null) {
  231.             printTreeWithIndent(t.left, indent+1);
  232.             for (int i=0;i<indent;i++)System.out.print("   ");
  233.             System.out.println(t.info);
  234.             printTreeWithIndent(t.right, indent+1);
  235.         }
  236.     }
  237.  
  238.     public BNode<E> getRoot() {
  239.         return root;
  240.     }
  241.  
  242.     public int size() {
  243.         return size(root);
  244.     }
  245.  
  246.     private int size(BNode<E> node) {
  247.         if(node == null) {
  248.             return 0;
  249.         }
  250.  
  251.         return 1 + size(node.left) + size(node.right);
  252.     }
  253.  
  254.     public E findKthSmallest(int k) {
  255.         if(k <= 0 || k > size()) {
  256.             return null;
  257.         }
  258.  
  259.         BNode<E> res = new BNode<E>(null);
  260.         int[] cnt = {0};
  261.         inorderSearch(root, k, cnt, res);
  262.  
  263.         return res.info;
  264.  
  265.     }
  266.     private void inorderSearch(BNode<E> node, int k, int[] cnt, BNode<E> result) {
  267.         if(node == null || result.info != null) {
  268.             return;
  269.         }
  270.         inorderSearch(node.left, k, cnt, result);
  271.  
  272.         cnt[0]++;
  273.         if(cnt[0] == k) {
  274.             result.info = node.info;
  275.             return;
  276.         }
  277.  
  278.         if(result.info == null) {
  279.             inorderSearch(node.right, k, cnt, result);
  280.         }
  281.     }
  282.  
  283. }
Advertisement
Add Comment
Please, Sign In to add comment