Filip_Markoski

[ADSx] - Inorder Successor

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