Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.99 KB | None | 0 0
  1. Бинарно пребарувачко дрво Problem 3 (2 / 5)
  2. Да се напише рекурзивна функција за наоѓање на висината (V) на даден елемент од бинарно пребарувачко дрво (листовите имаат висина 1), а потоа да се испечати бројот на елементите кои се на длабочина V ( коренот има длабочина 0).
  3.  
  4. Најнапред се внесува бројот на темињата, па темињата, па вредноста на дадениот елемент од дрвото за кој треба да се пресмета висината. На излез се печати висината на елементот, а потоа во нов ред бројот на темиња на пресметаната длабочина.
  5.  
  6. Име на класата во Java: BinarnoDrvo
  7.  
  8.  
  9. import java.io.IOException;
  10. import java.io.BufferedReader;
  11. import java.io.InputStreamReader;
  12. import java.util.Comparator;
  13. class BNode<E extends Comparable<E>>{
  14.     public E info;
  15.     public BNode<E> left;
  16.     public BNode<E> right;
  17.    
  18.     public BNode(E info) {
  19.         this.info = info;
  20.         left = null;
  21.         right = null;
  22.     }
  23.    
  24.     public BNode(E info, BNode<E> left, BNode<E> right) {
  25.         this.info = info;
  26.         this.left = left;
  27.         this.right = right;
  28.     }
  29. }
  30. class BinarySearchTree<E extends Comparable<E>>{
  31.     private BNode<E> root;
  32.     public BinarySearchTree() {
  33.         root = null;
  34.     }
  35.     public BNode<E> getRoot(){
  36.         return root;
  37.     }
  38.     private BNode<E> insert(E x, BNode<E>t){
  39.         //System.out.print(" -1- ");
  40.         if(t == null)
  41.         {
  42.             t = new BNode<E>(x,null,null);
  43.         }else if(x.compareTo(t.info) < 0) {
  44.             t.left = insert(x, t.left);
  45.         }else if(x.compareTo(t.info) > 0) {
  46.             t.right = insert(x, t.right);
  47.         }else;// Duplicate: do nothing
  48.         //System.out.println(t.info);
  49.         return t;
  50.     }
  51.     public void insert(E x) {
  52.         root = insert(x, root);
  53.     }
  54.     private BNode<E>findMin(BNode<E> t){
  55.         if(t == null) {
  56.             return null;
  57.         }else if(t.left == null) {
  58.             return t;
  59.         }
  60.         return findMin(t.left);
  61.     }
  62.    
  63.     private BNode<E> findMax(BNode<E> t){
  64.         if(t == null)
  65.             return null;
  66.         else if(t.right == null)
  67.             return t;
  68.         return findMax(t.right);
  69.     }
  70.     private BNode<E> find(E x, BNode<E> t){
  71.         if(t == null)
  72.             return null;
  73.         if(x.compareTo(t.info) < 0)
  74.             return find(x, t.left);
  75.         else if(x.compareTo(t.info) > 0)
  76.         {
  77.             return find(x, t.right);
  78.         }
  79.         else
  80.             return t;//equals =  finded !!
  81.     }
  82.     public BNode<E> find(E x)
  83.     {
  84.         return find(x, root);
  85.     }
  86.     private BNode<E> remove(Comparable x, BNode<E> t)
  87.     {
  88.         if(t == null)
  89.             return t;
  90.         if(x.compareTo(t.info) < 0)
  91.             t.left = remove(x, t.left);
  92.         else if(x.compareTo(t.info) > 0)
  93.         {
  94.             t.right = remove(x, t.right);
  95.         }
  96.         else if(t.left != null&&t.right != null)
  97.         {
  98.             t.info = findMin(t.right).info;
  99.             t.right = remove(t.info, t.right);
  100.         }
  101.         else
  102.             if(t.left != null)
  103.                 return t.left;
  104.             else
  105.                 return t.right;
  106.         return t;
  107.     }
  108.     public void remove(E x) {
  109.         root = remove(x, root);
  110.     }
  111.     public int height()
  112.     {
  113.         BNode<Integer>tmp = (BNode<Integer>)this.root;
  114.         return findHeight(tmp);
  115.     }
  116.     public int heightOf(E elem) {
  117.         BNode node = find(elem, root);
  118.         return findHeight(node);
  119.     }
  120.     public int findDepth(int level)
  121.     {
  122.         BNode<Integer>tmp = (BNode<Integer>)this.root;
  123.         return findDepth(tmp,0,level);
  124.     }
  125.     public int findDepth(BNode<Integer> node,int current, int entered)
  126.     {
  127.         if(node==null )
  128.         {
  129.             return 0;
  130.         }
  131.         if(current==entered)
  132.             return 1;
  133.         return findDepth(node.left, current + 1, entered) + findDepth(node.right, current + 1, entered);
  134.        
  135.     }
  136.     public int findHeight(BNode<Integer>node)
  137.     {
  138.         if(node==null)
  139.         {
  140.             return 0;
  141.         }
  142.         else {
  143.             int leftHeight = findHeight(node.left);
  144.             int rightHeight  = findHeight(node.right);
  145.             if(leftHeight > rightHeight)
  146.                 return(leftHeight + 1);
  147.             else
  148.                 return (rightHeight + 1);
  149.         }
  150.        
  151.     }
  152.     public void preorder()
  153.     {
  154.         System.out.print("PREORDER ");
  155.         BNode<Integer> tmp = (BNode<Integer>)root;
  156.         preorderR(tmp);
  157.     }
  158.     public void preorderR(BNode<Integer> n) {
  159.         if (n==null)
  160.             return;
  161.         System.out.print(n.info + " ");
  162.         //System.out.println(n.left.info);
  163.         preorderR(n.left);
  164.         preorderR(n.right);
  165.     }  
  166. }
  167.  
  168. public class BinarnoDrvo {
  169.     public static void main(String[] args)throws Exception{
  170.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  171.         BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();
  172.         int N = Integer.parseInt(br.readLine());
  173.         //BNode<Integer> array[] = new BNode<Integer>[N]();
  174.         int array[] = new int[N];
  175.         for(int i = 0; i<N; i++)
  176.         {
  177.             int z = Integer.parseInt(br.readLine());
  178.             //BNode<Integer> vnes = new BNode<Integer>(z,null,null);
  179.             tree.insert(z);
  180.             array[i] = z;
  181.         }
  182.         int baran = Integer.parseInt(br.readLine());
  183.         int level = tree.heightOf(baran);
  184.         System.out.println(level);
  185.         System.out.println(tree.findDepth(level));
  186.        
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement