Filip_Markoski

[NP] Бинарно дрво за пребарување

Nov 26th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.24 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.util.TreeSet;
  4.  
  5. class BNode<T extends Comparable<T>> {
  6.     T data;
  7.     BNode<T> left;
  8.     BNode<T> right;
  9.  
  10.     public BNode(T data, BNode<T> left, BNode<T> right) {
  11.         this.data = data;
  12.         this.left = left;
  13.         this.right = right;
  14.     }
  15. }
  16.  
  17. class BinaryTreeSet<T extends Comparable<T>> {
  18.  
  19.     BNode<T> root;
  20.  
  21.     BinaryTreeSet() {
  22.         this.root = null;
  23.     }
  24.  
  25.     void addElement(T value) {
  26.         root = insert(value, root);
  27.     }
  28.  
  29.     BNode<T> insert(T value, BNode<T> node) {
  30.         if (node == null) {
  31.             node = new BNode<>(value, null, null);
  32.         } else if (value.compareTo(node.data) < 0) { // if value < data
  33.             node.left = insert(value, node.left); // recursive call
  34.         } else if (value.compareTo(node.data) > 0) {
  35.             node.right = insert(value, node.right);
  36.         } // If it's a duplicate we don't do anything
  37.         return node;
  38.     }
  39.  
  40.     boolean removeElement(T value) {
  41.         return remove(value, root) != null;
  42.     }
  43.  
  44.     BNode<T> remove(T value, BNode<T> node) {
  45.         if (node == null) {
  46.             return null;
  47.         }
  48.  
  49.         if (value.compareTo(node.data) < 0) {
  50.             node.left = remove(value, node.left);
  51.         } else if (value.compareTo(node.data) > 0) {
  52.             node.right = remove(value, node.right);
  53.         } else if (node.left != null && node.right != null) {
  54.             node.data = findMin(node.right).data;
  55.             node.right = remove(node.data, node.right);
  56.         } else {
  57.             if (node.left != null) {
  58.                 return node.left;
  59.             } else {
  60.                 return node.right;
  61.             }
  62.         }
  63.         return node;
  64.     }
  65.  
  66.     BNode<T> findMin(BNode<T> node) {
  67.         if (node == null) {
  68.             return null;
  69.         } else if (node.left == null) {
  70.             return node;
  71.         }
  72.         return findMin(node.left);
  73.     }
  74.  
  75.     boolean contains(T value) {
  76.         return find(value, root) != null;
  77.     }
  78.  
  79.     BNode<T> find(T value, BNode<T> node) {
  80.         if (node == null) {
  81.             return null;
  82.         }
  83.  
  84.         if (value.compareTo(node.data) == 0) {
  85.             return node;
  86.         } else if (value.compareTo(node.data) < 0) {
  87.             return find(value, node.left);
  88.         } else if (value.compareTo(node.data) > 0) {
  89.             return find(value, node.right);
  90.         }
  91.         // if value.equals(node.data)
  92.         return node;
  93.     }
  94.  
  95.     /*
  96.     * In-Order is
  97.     * Left Root Right
  98.     * */
  99.  
  100.     void inorder(BNode<T> node, StringBuffer sb) {
  101.         if (node != null) {
  102.             inorder(node.left, sb);
  103.             sb.append(node.data + " ");
  104.             inorder(node.right, sb);
  105.         }
  106.     }
  107.  
  108.     boolean isEmpty() {
  109.         return root == null;
  110.     }
  111.  
  112.     @Override
  113.     public String toString() {
  114.         if (isEmpty()) {
  115.             return "EMPTY";
  116.         }
  117.         StringBuffer sb = new StringBuffer();
  118.         inorder(root, sb);
  119.         sb.setLength(sb.length() - 1); // removes the last empty space
  120.         return sb.toString();
  121.     }
  122. }
  123.  
  124. public class BinaryTreeSetTest {
  125.  
  126.     public static void main(String[] args) {
  127.         Scanner jin = new Scanner(System.in);
  128.         int t = jin.nextInt();
  129.         if (t == 0) {
  130.             BinaryTreeSet<Integer> ts = new BinaryTreeSet<Integer>();
  131.             while (jin.hasNextInt()) {
  132.                 ts.addElement(jin.nextInt());
  133.             }
  134.             System.out.println(ts);
  135.         }
  136.         if (t == 1) {
  137.             BinaryTreeSet<String> ts = new BinaryTreeSet<String>();
  138.             while (true) {
  139.                 String next = jin.next();
  140.                 if (next.equals("stop")) break;
  141.                 ts.addElement(next);
  142.             }
  143.             System.out.println(ts);
  144.         }
  145.         if (t == 2) {
  146.             BinaryTreeSet<Long> ts = new BinaryTreeSet<Long>();
  147.             while (jin.hasNextLong()) {
  148.                 ts.addElement(jin.nextLong());
  149.             }
  150.             jin.next();
  151.             System.out.println(ts);
  152.             while (jin.hasNextLong()) {
  153.                 System.out.println(ts.contains(jin.nextLong()));
  154.             }
  155.             System.out.println(ts);
  156.         }
  157.         if (t == 3) {
  158.             BinaryTreeSet<String> ts = new BinaryTreeSet<String>();
  159.             int counter = 0;
  160.             while (true) {
  161.                 if (counter % 20 == 0) System.out.println(ts);
  162.                 ++counter;
  163.                 String next = jin.next();
  164.                 if (next.equals("stop")) break;
  165.                 if (next.equals("add")) {
  166.                     ts.addElement(jin.next());
  167.                 }
  168.                 if (next.equals("remove")) {
  169.                     ts.removeElement(jin.next());
  170.                 }
  171.                 if (next.equals("query")) {
  172.                     System.out.println(ts.contains(jin.next()));
  173.                 }
  174.             }
  175.             System.out.println(ts);
  176.         }
  177.         if (t == 4) {
  178.             BinaryTreeSet<Long> ts = new BinaryTreeSet<Long>();
  179.             TreeSet<Long> control_set = new TreeSet<Long>();
  180.             ArrayList<Long> all = new ArrayList<Long>();
  181.             all.add(5L);
  182.             int n = jin.nextInt();
  183.             boolean exact = true;
  184.             for (int i = 0; exact && i < n; ++i) {
  185.                 if (Math.random() < 0.4) {
  186.                     if (Math.random() < 0.6) {
  187.                         long to_add = (long) (Math.random() * 98746516548964156L);
  188.                         ts.addElement(to_add);
  189.                         control_set.add(to_add);
  190.                         all.add(to_add);
  191.                     } else {
  192.                         int add_idx = (int) (Math.random() * all.size());
  193.                         long to_add = all.get(add_idx);
  194.                         ts.addElement(to_add);
  195.                         control_set.add(to_add);
  196.                     }
  197.                 } else {
  198.                     if (Math.random() < 0.4) {
  199.                         if (Math.random() < 0.1) {
  200.                             long to_remove = (long) (Math.random() * 98746516548964156L);
  201.                             ts.removeElement(to_remove);
  202.                             control_set.remove(to_remove);
  203.                         } else {
  204.                             int remove_idx = (int) (Math.random() * all.size());
  205.                             long to_remove = all.get(remove_idx);
  206.                             ts.removeElement(to_remove);
  207.                             control_set.remove(to_remove);
  208.                         }
  209.                     } else {
  210.                         if (Math.random() < 0.3) {
  211.                             long to_query = (long) (Math.random() * 98746516548964156L);
  212.                             exact &= ts.contains(to_query) == control_set.contains(to_query);
  213.                         } else {
  214.                             int query_idx = (int) (Math.random() * all.size());
  215.                             long to_query = all.get(query_idx);
  216.                             exact &= ts.contains(to_query) == control_set.contains(to_query);
  217.                         }
  218.                     }
  219.                 }
  220.             }
  221.             System.out.println(exact);
  222.         }
  223.     }
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment