mvCode

BinarnoDrvo, Бинарно Пребарувачко Дрво, AIPS

Dec 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.62 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.Random;
  6. //class package binarysearchtree;
  7.  
  8. class BNode<E extends Comparable<E>> {
  9.  
  10.     public E info;
  11.     public BNode<E> left;
  12.     public BNode<E> right;
  13.  
  14.     public BNode(E info) {
  15.         this.info = info;
  16.         left = null;
  17.         right = null;
  18.     }
  19.  
  20.     public BNode(E info, BNode<E> left, BNode<E> right) {
  21.         this.info = info;
  22.         this.left = left;
  23.         this.right = right;
  24.     }
  25.  
  26. }
  27.  
  28.  
  29. // BinarySearchTree class
  30.  
  31.  
  32. //
  33. // CONSTRUCTION: with no initializer
  34. //
  35. // ******************PUBLIC OPERATIONS*********************
  36. // void insert( x )       --> Insert x
  37. // void remove( x )       --> Remove x
  38. // Comparable find( x )   --> Return item that matches x
  39. // Comparable findMin( )  --> Return smallest item
  40. // Comparable findMax( )  --> Return largest item
  41. // boolean isEmpty( )     --> Return true if empty; else false
  42. // void makeEmpty( )      --> Remove all items
  43. // void printTree( )      --> Print tree in sorted order
  44. /**
  45.  * Implements an unbalanced binary search tree.
  46.  * Note that all "matching" is based on the compareTo method.
  47.  * @author Mark Allen Weiss
  48.  */
  49. class BinarySearchTree<E extends Comparable<E>> {
  50.  
  51.     /**
  52.      * The tree root.
  53.      */
  54.     private BNode<E> root;
  55.  
  56.     /**
  57.      * Construct the tree.
  58.      */
  59.     public BinarySearchTree() {
  60.         root = null;
  61.     }
  62.  
  63.     /**
  64.      * Insert into the tree; duplicates are ignored.
  65.      *
  66.      * @param x the item to insert.
  67.      */
  68.     public void insert(E x) {
  69.         root = insert(x, root);
  70.     }
  71.  
  72.     /**
  73.      * Remove from the tree. Nothing is done if x is not found.
  74.      *
  75.      * @param x the item to remove.
  76.      */
  77.     public void remove(E x) {
  78.         root = remove(x, root);
  79.     }
  80.  
  81.     /**
  82.      * Find the smallest item in the tree.
  83.      *
  84.      * @return smallest item or null if empty.
  85.      */
  86.     public E findMin() {
  87.         return elementAt(findMin(root));
  88.     }
  89.  
  90.     /**
  91.      * Find the largest item in the tree.
  92.      *
  93.      * @return the largest item of null if empty.
  94.      */
  95.     public E findMax() {
  96.         return elementAt(findMax(root));
  97.     }
  98.  
  99.     /**
  100.      * Find an item in the tree.
  101.      *
  102.      * @param x the item to search for.
  103.      * @return the matching item or null if not found.
  104.      */
  105.     public BNode<E> find(E x) {
  106.         return find(x, root);
  107.     }
  108.  
  109.     /**
  110.      * Make the tree logically empty.
  111.      */
  112.     public void makeEmpty() {
  113.         root = null;
  114.     }
  115.  
  116.     /**
  117.      * Test if the tree is logically empty.
  118.      *
  119.      * @return true if empty, false otherwise.
  120.      */
  121.     public boolean isEmpty() {
  122.         return root == null;
  123.     }
  124.  
  125.     /**
  126.      * Print the tree contents in sorted order.
  127.      */
  128.     public void printTree() {
  129.         if (isEmpty()) {
  130.             System.out.println("Empty tree");
  131.         } else {
  132.             printTree(root);
  133.         }
  134.     }
  135.  
  136.     /**
  137.      * Internal method to get element field.
  138.      *
  139.      * @param t the node.
  140.      * @return the element field or null if t is null.
  141.      */
  142.     private E elementAt(BNode<E> t) {
  143.         if (t == null)
  144.             return null;
  145.         return t.info;
  146.     }
  147.  
  148.     /**
  149.      * Internal method to insert into a subtree.
  150.      *
  151.      * @param x the item to insert.
  152.      * @param t the node that roots the tree.
  153.      * @return the new root.
  154.      */
  155.     private BNode<E> insert(E x, BNode<E> t) {
  156.         if (t == null) {
  157.             t = new BNode<E>(x, null, null);
  158.         } else if (x.compareTo(t.info) < 0) {
  159.             t.left = insert(x, t.left);
  160.         } else if (x.compareTo(t.info) > 0) {
  161.             t.right = insert(x, t.right);
  162.         } else ;  // Duplicate; do nothing
  163.         return t;
  164.     }
  165.  
  166.     /**
  167.      * Internal method to remove from a subtree.
  168.      *
  169.      * @param x the item to remove.
  170.      * @param t the node that roots the tree.
  171.      * @return the new root.
  172.      */
  173.     private BNode<E> remove(Comparable x, BNode<E> t) {
  174.         if (t == null)
  175.             return t;   // Item not found; do nothing
  176.  
  177.         if (x.compareTo(t.info) < 0) {
  178.             t.left = remove(x, t.left);
  179.         } else if (x.compareTo(t.info) > 0) {
  180.             t.right = remove(x, t.right);
  181.         } else if (t.left != null&&t.right != null) { // Two children
  182.             t.info = findMin(t.right).info;
  183.             t.right = remove(t.info, t.right);
  184.         } else {
  185.             if (t.left != null)
  186.                 return t.left;
  187.             else
  188.                 return t.right;
  189.         }
  190.         return t;
  191.     }
  192.  
  193.     /**
  194.      * Internal method to find the smallest item in a subtree.
  195.      *
  196.      * @param t the node that roots the tree.
  197.      * @return node containing the smallest item.
  198.      */
  199.     private BNode<E> findMin(BNode<E> t) {
  200.         if (t == null) {
  201.             return null;
  202.         } else if (t.left == null) {
  203.             return t;
  204.         }
  205.         return findMin(t.left);
  206.     }
  207.  
  208.     /**
  209.      * Internal method to find the largest item in a subtree.
  210.      *
  211.      * @param t the node that roots the tree.
  212.      * @return node containing the largest item.
  213.      */
  214.     private BNode<E> findMax(BNode<E> t) {
  215.         if (t == null) {
  216.             return null;
  217.         } else if (t.right == null) {
  218.             return t;
  219.         }
  220.         return findMax(t.right);
  221.     }
  222.  
  223.     /**
  224.      * Internal method to find an item in a subtree.
  225.      *
  226.      * @param x is item to search for.
  227.      * @param t the node that roots the tree.
  228.      * @return node containing the matched item.
  229.      */
  230.     private BNode<E> find(E x, BNode<E> t) {
  231.         if (t == null)
  232.             return null;
  233.  
  234.         if (x.compareTo(t.info) < 0) {
  235.             return find(x, t.left);
  236.         } else if (x.compareTo(t.info) > 0) {
  237.             return find(x, t.right);
  238.         } else {
  239.             return t;    // Match
  240.         }
  241.     }
  242.  
  243.     /**
  244.      * Internal method to print a subtree in sorted order.
  245.      *
  246.      * @param t the node that roots the tree.
  247.      */
  248.     private void printTree(BNode<E> t) {
  249.         if (t != null) {
  250.             printTree(t.left);
  251.             System.out.println(t.info);
  252.             printTree(t.right);
  253.         }
  254.     }
  255.  
  256.     /////////////moj kod///////////////////
  257.  
  258.     public int findHeight( E value ) {
  259.  
  260.         return findHeight( find( value ));
  261.     }
  262.  
  263.     private int findHeight( BNode<E> tmp ) {
  264.  
  265.         if( tmp == null )
  266.             return 0;
  267.  
  268.         if( tmp.right == null && tmp.left == null )
  269.             return 1;
  270.  
  271.  
  272.         return Math.max(findHeight( tmp.left ), findHeight( tmp.right )) +1;
  273.  
  274.     }
  275.  
  276.     private int findDepth( BNode<E> node, BNode<E> tmp ) { // finds depth of a node
  277.  
  278.         if( tmp == null )
  279.             return 0;
  280.  
  281.         if( node == tmp ) // returns 0 because the root has depth 0;
  282.             return 0;
  283.  
  284.         if( node.info.compareTo( tmp.info ) > 0 )
  285.             return  findDepth( node, tmp.right ) +1;
  286.  
  287.         return  findDepth( node, tmp.left ) +1;
  288.     }
  289.  
  290.     private int findNodesAtDeph( int depth, BNode<E> tmp ) { // finds number of nodes at given depth
  291.  
  292.         if( tmp == null )
  293.             return 0;
  294.  
  295.         if( findDepth( tmp, root ) == depth )
  296.             return 1;
  297.  
  298.         return findNodesAtDeph( depth, tmp.left ) + findNodesAtDeph( depth, tmp.right );
  299.     }
  300.  
  301.     public int findNodesAtDepth( int depth ) {
  302.  
  303.         return findNodesAtDeph(depth, root);
  304.     }
  305.  
  306.  
  307.     /////////////Moj kod//////////////////
  308.  
  309.     // Test program
  310.     /*public static void main(String[] args) {
  311.         int i, j, k;
  312.  
  313.         Random r = new Random(System.currentTimeMillis());
  314.         BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>();
  315.  
  316.         for (i = 0; i < 1000; i++)
  317.             bst.insert(r.nextInt(1000000));
  318.  
  319.         bst.printTree();
  320.  
  321.     }*/
  322. }
  323.  
  324.  
  325.  
  326. ///////////////////////////////////////////////////////////////////
  327. public class BinarnoDrvo {
  328.     public static void main(String[] args) throws IOException {
  329.  
  330.         BufferedReader br = new BufferedReader( new InputStreamReader( System.in ));
  331.         BinarySearchTree<Integer> tree = new BinarySearchTree<>();
  332.  
  333.         int n = Integer.parseInt( br.readLine() ); // n is num of nodes
  334.  
  335.         for( int i=0; i<n; i++ ) {
  336.  
  337.             int val = Integer.parseInt( br.readLine() );
  338.             tree.insert( val );
  339.         }
  340.  
  341.         int value = Integer.parseInt( br.readLine() ); // value to find
  342.  
  343.         int number = tree.findHeight( value ); // height
  344.  
  345.         System.out.println( number );
  346.  
  347.         System.out.println( tree.findNodesAtDepth( number ) );
  348.  
  349.     }
  350.  
  351. }
Add Comment
Please, Sign In to add comment