Advertisement
Kame3

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

Dec 31st, 2020
1,328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. Бинарно пребарувачко дрво Problem 3 (0 / 0)
  2.  
  3. Да се напише рекурзивна функција за наоѓање на висината (V) на даден елемент од бинарно пребарувачко дрво (листовите имаат висина 1), а потоа да се испечати бројот на елементите кои се на длабочина V ( коренот има длабочина 0).
  4.  
  5. Најнапред се внесува бројот на темињата, па темињата, па вредноста на дадениот елемент од дрвото за кој треба да се пресмета висината. На излез се печати висината на елементот, а потоа во нов ред бројот на темиња на пресметаната длабочина.
  6.  
  7. Име на класата во Java: BinarnoDrvo
  8.  
  9.  
  10. import java.util.Scanner;
  11.  
  12. public class BinarnoDrvo {
  13.  
  14.     public static void main(String[] args) {
  15.        
  16.         Scanner scan = new Scanner(System.in);
  17.        
  18.         int n = scan.nextInt();
  19.        
  20.         BinarySearchTree<Integer> tree = new BinarySearchTree<>();
  21.        
  22.         for (int i=0; i<n; ++i)
  23.             tree.insert(scan.nextInt());
  24.        
  25.         int depth = scan.nextInt();
  26.         int height = tree.height(depth);
  27.         System.out.println(height);
  28.         System.out.println(tree.nodesOnDepth(height));
  29.     }
  30.    
  31.    
  32. }
  33.  
  34. class BinarySearchTree<E extends Comparable<E>> {
  35.    
  36.     /** The tree root. */
  37.     private BNode<E> root;
  38.    
  39.     /**
  40.      * Construct the tree.
  41.      */
  42.     public BinarySearchTree() {
  43.         root = null;
  44.     }
  45.    
  46.     public void insert(E x) {
  47.         root = insert(x, root);
  48.     }
  49.    
  50.     /**
  51.      * Internal method to insert into a subtree.
  52.      * @param x the item to insert.
  53.      * @param t the node that roots the tree.
  54.      * @return the new root.
  55.      */
  56.     private BNode<E> insert(E x, BNode<E> t) {
  57.         if (t == null) {
  58.             t = new BNode<E>(x, null, null);
  59.         } else if (x.compareTo(t.info) < 0) {
  60.             t.left = insert(x, t.left);
  61.         } else if (x.compareTo(t.info) > 0) {
  62.             t.right = insert(x, t.right);
  63.         } else;  // Duplicate; do nothing
  64.         return t;
  65.     }
  66.    
  67.        
  68.     /**
  69.      * Find an item in the tree.
  70.      * @param x the item to search for.
  71.      * @return the matching item or null if not found.
  72.      */
  73.     public BNode<E> find(E x) {
  74.         return find(x, root);
  75.     }
  76.    
  77.     /**
  78.      * Internal method to find an item in a subtree.
  79.      * @param x is item to search for.
  80.      * @param t the node that roots the tree.
  81.      * @return node containing the matched item.
  82.      */
  83.     private BNode<E> find(E x, BNode<E> t) {
  84.         if (t == null)
  85.             return null;
  86.        
  87.         if (x.compareTo(t.info) < 0) {
  88.             return find(x, t.left);
  89.         } else if (x.compareTo(t.info) > 0) {
  90.             return find(x, t.right);
  91.         } else {
  92.             return t;    // Match
  93.         }
  94.     }
  95.    
  96.    
  97.     public int height(E number) {
  98.         return height(find(number));
  99.     }
  100.    
  101.     public int height(BNode<E> node) {
  102.         if (node == null)
  103.             return 0;
  104.         if (node.left == null && node.right == null)
  105.             return 1;
  106.         return Math.max(height(node.left), height(node.right)) + 1;
  107.     }
  108.    
  109.     public int nodesOnDepth(int number) {
  110.         return nodesOnDepth(root,number,0);
  111.     }
  112.    
  113.     private int nodesOnDepth(BNode<E> rt, int number,int depth) {
  114.        
  115.         if (rt != null && depth == number)
  116.             return 1;
  117.         if (rt == null)
  118.             return 0;
  119.         return nodesOnDepth(rt.left,number,depth+1) + nodesOnDepth(rt.right,number,depth+1);
  120.     }
  121.    
  122. }
  123.  
  124. class BNode<E extends Comparable<E>> {
  125.    
  126.     public E info;
  127.     public BNode<E> left;
  128.     public BNode<E> right;
  129.    
  130.     public BNode(E info) {
  131.         this.info = info;
  132.         left = null;
  133.         right = null;
  134.     }
  135.    
  136.     public BNode(E info, BNode<E> left, BNode<E> right) {
  137.         this.info = info;
  138.         this.left = left;
  139.         this.right = right;
  140.     }
  141.    
  142. }
  143.  
  144.  
  145.  
  146. Sample input
  147.  
  148. 11
  149. 3
  150. 4
  151. 1
  152. 8
  153. 6
  154. 0
  155. 5
  156. 2
  157. 10
  158. 17
  159. 13
  160. 8
  161.  
  162. Sample output
  163.  
  164. 4
  165. 2
  166.  
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement