Mitrezzz

[АПС] Лаб 8: Бинарни пребарувачки дрва Бинарно пребарувачко дрво

Dec 19th, 2018 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.92 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3.  
  4. class BNode<E extends Comparable<E>> {
  5.    
  6.     public E info;
  7.     public BNode<E> left;
  8.     public BNode<E> right;
  9.    
  10.     public BNode(E info) {
  11.         this.info = info;
  12.         left = null;
  13.         right = null;
  14.     }
  15.    
  16.     public BNode(E info, BNode<E> left, BNode<E> right) {
  17.         this.info = info;
  18.         this.left = left;
  19.         this.right = right;
  20.     }
  21.    
  22. }
  23.  
  24. class BinarySearchTree<E extends Comparable<E>> {
  25.    
  26.     /** The tree root. */
  27.     private BNode<E> root;
  28.    
  29.     /**
  30.      * Construct the tree.
  31.      */
  32.     public BinarySearchTree() {
  33.         root = null;
  34.     }
  35.  
  36.     /**
  37.      * Insert into the tree; duplicates are ignored.
  38.      * @param x the item to insert.
  39.      */
  40.     public void insert(E x) {
  41.         root = insert(x, root);
  42.     }
  43.  
  44.     /**
  45.      * Remove from the tree. Nothing is done if x is not found.
  46.      * @param x the item to remove.
  47.      */
  48.     public void remove(E x) {
  49.         root = remove(x, root);
  50.     }
  51.  
  52.     /**
  53.      * Find the smallest item in the tree.
  54.      * @return smallest item or null if empty.
  55.      */
  56.     public E findMin() {
  57.         return elementAt(findMin(root));
  58.     }
  59.  
  60.     /**
  61.      * Find the largest item in the tree.
  62.      * @return the largest item of null if empty.
  63.      */
  64.     public E findMax() {
  65.         return elementAt(findMax(root));
  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.      * Make the tree logically empty.
  79.      */
  80.     public void makeEmpty() {
  81.         root = null;
  82.     }
  83.  
  84.     /**
  85.      * Test if the tree is logically empty.
  86.      * @return true if empty, false otherwise.
  87.      */
  88.     public boolean isEmpty() {
  89.         return root == null;
  90.     }
  91.  
  92.     /**
  93.      * Print the tree contents in sorted order.
  94.      */
  95.     public void printTree() {
  96.         if (isEmpty()) {
  97.             System.out.println("Empty tree");
  98.         } else {
  99.             printTree(root);
  100.         }
  101.     }
  102.  
  103.     /**
  104.      * Internal method to get element field.
  105.      * @param t the node.
  106.      * @return the element field or null if t is null.
  107.      */
  108.     private E elementAt(BNode<E> t) {
  109.         if (t == null)
  110.             return null;
  111.         return t.info;
  112.     }
  113.  
  114.     /**
  115.      * Internal method to insert into a subtree.
  116.      * @param x the item to insert.
  117.      * @param t the node that roots the tree.
  118.      * @return the new root.
  119.      */
  120.     private BNode<E> insert(E x, BNode<E> t) {
  121.         if (t == null) {
  122.             t = new BNode<E>(x, null, null);
  123.         } else if (x.compareTo(t.info) < 0) {
  124.             t.left = insert(x, t.left);
  125.         } else if (x.compareTo(t.info) > 0) {
  126.             t.right = insert(x, t.right);
  127.         } else;  // Duplicate; do nothing
  128.         return t;
  129.     }
  130.  
  131.     /**
  132.      * Internal method to remove from a subtree.
  133.      * @param x the item to remove.
  134.      * @param t the node that roots the tree.
  135.      * @return the new root.
  136.      */
  137.     private BNode<E> remove(Comparable x, BNode<E> t) {
  138.         if (t == null)
  139.             return t;   // Item not found; do nothing
  140.        
  141.         if (x.compareTo(t.info) < 0) {
  142.             t.left = remove(x, t.left);
  143.         } else if (x.compareTo(t.info) > 0) {
  144.             t.right = remove(x, t.right);
  145.         } else if (t.left != null&&t.right != null) { // Two children
  146.             t.info = findMin(t.right).info;
  147.             t.right = remove(t.info, t.right);
  148.         } else {
  149.             if (t.left != null)
  150.                 return t.left;
  151.             else
  152.                 return t.right;
  153.         }
  154.         return t;
  155.     }
  156.  
  157.     /**
  158.      * Internal method to find the smallest item in a subtree.
  159.      * @param t the node that roots the tree.
  160.      * @return node containing the smallest item.
  161.      */
  162.     private BNode<E> findMin(BNode<E> t) {
  163.         if (t == null) {
  164.             return null;
  165.         } else if (t.left == null) {
  166.             return t;
  167.         }
  168.         return findMin(t.left);
  169.     }
  170.  
  171.     /**
  172.      * Internal method to find the largest item in a subtree.
  173.      * @param t the node that roots the tree.
  174.      * @return node containing the largest item.
  175.      */
  176.     private BNode<E> findMax(BNode<E> t) {
  177.         if (t == null) {
  178.             return null;
  179.         } else if (t.right == null) {
  180.             return t;
  181.         }
  182.         return findMax(t.right);
  183.     }
  184.  
  185.     /**
  186.      * Internal method to find an item in a subtree.
  187.      * @param x is item to search for.
  188.      * @param t the node that roots the tree.
  189.      * @return node containing the matched item.
  190.      */
  191.     private BNode<E> find(E x, BNode<E> t) {
  192.         if (t == null)
  193.             return null;
  194.        
  195.         if (x.compareTo(t.info) < 0) {
  196.             return find(x, t.left);
  197.         } else if (x.compareTo(t.info) > 0) {
  198.             return find(x, t.right);
  199.         } else {
  200.             return t;    // Match
  201.         }
  202.     }
  203.  
  204.     /**
  205.      * Internal method to print a subtree in sorted order.
  206.      * @param t the node that roots the tree.
  207.      */
  208.     private void printTree(BNode<E> t) {
  209.         if (t != null) {
  210.             printTree(t.left);
  211.             System.out.println(t.info);
  212.             printTree(t.right);
  213.         }
  214.     }
  215.    
  216.     public E getSuccessor(BNode<E> t, E x) {
  217.         if (x.compareTo(t.info) < 0) {
  218.             E tmp = findMax(t.left).info;
  219.             if (tmp.equals(x))
  220.                 return t.info;
  221.             else
  222.                 return getSuccessor(t.left, x);
  223.         } else if (x.compareTo(t.info) > 0) {
  224.             return getSuccessor(t.right, x);
  225.         } else {
  226.             return findMin(t.right).info;
  227.         }
  228.     }
  229.    
  230.     public E getSuccessor(E x) {
  231.         return getSuccessor(root, x);
  232.     }
  233.    
  234.    
  235.    
  236.    public  int broj_na_teminja_na_dlabocina_n_pom1(BNode<E> root, int k)
  237.    {
  238.        //so ovaa root pochnuva od k!=0
  239.         if(root==null)
  240.             return 0;
  241.         if(k==0)
  242.             return 1;
  243.         else
  244.             return broj_na_teminja_na_dlabocina_n_pom1(root.left, k-1) + broj_na_teminja_na_dlabocina_n_pom1(root.right,k-1);
  245.         }
  246.    public  int broj_na_teminja_na_dlabocina_n_pom2(BNode<E> root,int n, int k)
  247.    {
  248.        //so ovaa root pochnuva od k!=0
  249.         if(root==null)
  250.             return 0;
  251.         if(n==k)
  252.             return 1;
  253.         else
  254.             return broj_na_teminja_na_dlabocina_n_pom2(root.left, n+1, k) + broj_na_teminja_na_dlabocina_n_pom2(root.right,n+1,k);
  255.         }
  256.  
  257.        
  258.   public int broj_na_teminja_na_dlabocina_n1( int k)
  259.   {
  260.       return broj_na_teminja_na_dlabocina_n_pom1(this.root,k);
  261.   }
  262.   public int broj_na_teminja_na_dlabocina_n2( int k)
  263.   {
  264.       return broj_na_teminja_na_dlabocina_n_pom2(this.root,0,k);
  265.   }
  266.    
  267.    public   int calculateHeight(BNode<E> t)
  268.         {
  269.             if(t==null)
  270.                 return 0;
  271.             if(t.left==null&&t.right==null)
  272.                 return 1;
  273.             else
  274.                 return 1+Math.max(calculateHeight(t.left), calculateHeight(t.right));
  275.         }
  276.  
  277.    
  278.    
  279. }
  280.  
  281. public class BinarnoDrvo {
  282.    
  283.     public static void main(String[] args) throws Exception {
  284.         int i,j,k;
  285.        
  286.         BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();
  287.        
  288.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  289.         int N = Integer.parseInt(br.readLine());
  290.        
  291.         for (i=0;i<N;i++) {
  292.             int num = Integer.parseInt(br.readLine());
  293.             tree.insert(new Integer(num));
  294.         }
  295.        
  296.        
  297.         int x=Integer.parseInt(br.readLine());
  298.        
  299.        
  300.        
  301.         BNode<Integer> t=tree.find(x);
  302.         int rez=tree.calculateHeight(t);
  303.         System.out.println(rez);
  304.  
  305.         int kraj=tree.broj_na_teminja_na_dlabocina_n2(rez);
  306.         System.out.println(kraj);
  307.          br.close();
  308.        
  309.     }
  310.    
  311. }
Add Comment
Please, Sign In to add comment