Crazy

Бинарно пребарувачко дрво

Dec 19th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class BinarnoDrvo {
  4.  
  5.     public static void main(String[] args) {
  6.        
  7.         Scanner scan = new Scanner(System.in);
  8.        
  9.         int n = scan.nextInt();
  10.        
  11.         BinarySearchTree<Integer> tree = new BinarySearchTree<>();
  12.        
  13.         for (int i=0; i<n; ++i)
  14.             tree.insert(scan.nextInt());
  15.        
  16.         int depth = scan.nextInt();
  17.         int height = tree.height(depth);
  18.         System.out.println(height);
  19.         System.out.println(tree.nodesOnDepth(height));
  20.     }
  21.    
  22.    
  23. }
  24.  
  25. class BinarySearchTree<E extends Comparable<E>> {
  26.    
  27.     /** The tree root. */
  28.     private BNode<E> root;
  29.    
  30.     /**
  31.      * Construct the tree.
  32.      */
  33.     public BinarySearchTree() {
  34.         root = null;
  35.     }
  36.    
  37.     public void insert(E x) {
  38.         root = insert(x, root);
  39.     }
  40.    
  41.     /**
  42.      * Internal method to insert into a subtree.
  43.      * @param x the item to insert.
  44.      * @param t the node that roots the tree.
  45.      * @return the new root.
  46.      */
  47.     private BNode<E> insert(E x, BNode<E> t) {
  48.         if (t == null) {
  49.             t = new BNode<E>(x, null, null);
  50.         } else if (x.compareTo(t.info) < 0) {
  51.             t.left = insert(x, t.left);
  52.         } else if (x.compareTo(t.info) > 0) {
  53.             t.right = insert(x, t.right);
  54.         } else;  // Duplicate; do nothing
  55.         return t;
  56.     }
  57.    
  58.        
  59.     /**
  60.      * Find an item in the tree.
  61.      * @param x the item to search for.
  62.      * @return the matching item or null if not found.
  63.      */
  64.     public BNode<E> find(E x) {
  65.         return find(x, root);
  66.     }
  67.    
  68.     /**
  69.      * Internal method to find an item in a subtree.
  70.      * @param x is item to search for.
  71.      * @param t the node that roots the tree.
  72.      * @return node containing the matched item.
  73.      */
  74.     private BNode<E> find(E x, BNode<E> t) {
  75.         if (t == null)
  76.             return null;
  77.        
  78.         if (x.compareTo(t.info) < 0) {
  79.             return find(x, t.left);
  80.         } else if (x.compareTo(t.info) > 0) {
  81.             return find(x, t.right);
  82.         } else {
  83.             return t;    // Match
  84.         }
  85.     }
  86.    
  87.    
  88.     public int height(E number) {
  89.         return height(find(number));
  90.     }
  91.    
  92.     public int height(BNode<E> node) {
  93.         if (node == null)
  94.             return 0;
  95.         if (node.left == null && node.right == null)
  96.             return 1;
  97.         return Math.max(height(node.left), height(node.right)) + 1;
  98.     }
  99.    
  100.     public int nodesOnDepth(int number) {
  101.         return nodesOnDepth(root,number,0);
  102.     }
  103.    
  104.     private int nodesOnDepth(BNode<E> rt, int number,int depth) {
  105.        
  106.         if (rt != null && depth == number)
  107.             return 1;
  108.         if (rt == null)
  109.             return 0;
  110.         return nodesOnDepth(rt.left,number,depth+1) + nodesOnDepth(rt.right,number,depth+1);
  111.     }
  112.    
  113. }
  114.  
  115. class BNode<E extends Comparable<E>> {
  116.    
  117.     public E info;
  118.     public BNode<E> left;
  119.     public BNode<E> right;
  120.    
  121.     public BNode(E info) {
  122.         this.info = info;
  123.         left = null;
  124.         right = null;
  125.     }
  126.    
  127.     public BNode(E info, BNode<E> left, BNode<E> right) {
  128.         this.info = info;
  129.         this.left = left;
  130.         this.right = right;
  131.     }
  132.    
  133. }
Advertisement
Add Comment
Please, Sign In to add comment