Advertisement
Guest User

Untitled

a guest
May 4th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3.  
  4.  
  5. public class Drzewo {
  6.     public Elementy _root;
  7.  
  8.     public Drzewo() {
  9.         this._root = null;
  10.     }
  11.  
  12.     public void queueBFS(Elementy v) {
  13.         Queue<Elementy> Q = new LinkedList<>();
  14.         Q.add(v);
  15.         while (!Q.isEmpty()) {
  16.             v = Q.peek();
  17.             Q.poll();
  18.  
  19.             System.out.println(v.data +" "+v.lista);
  20.  
  21.             if (v.left != null) Q.add(v.left);
  22.             if (v.right != null) Q.add(v.right);
  23.         }
  24.         System.out.print("\n");
  25.     }
  26.  
  27.    
  28.    
  29.    
  30.    
  31.    
  32.    
  33.    
  34.    
  35.    
  36.    
  37.     //dodawanie do drzewa
  38.    
  39.     public void insertBST(Elementy w) {
  40.         Elementy parent = null;
  41.         Elementy node = _root;
  42.  
  43.         while (node != null) {
  44.             parent = node;
  45.             node = w.compareTo(parent) <= 0 ? node.left : node.right;
  46.         }
  47.  
  48.         w.up = parent;
  49.  
  50.         if (parent == null) {
  51.                 _root = w;
  52.         }
  53.         else if (w.compareTo(parent) < 0) {
  54.             parent.left = w;
  55.         }
  56.         else {
  57.             parent.right = w;
  58.         }
  59.         return;
  60.     }
  61.  
  62.    public void inOrder(Elementy v) {
  63.         if (v != null) {
  64.             inOrder(v.left);
  65.             System.out.println(v.data + " ");
  66.             inOrder(v.right);
  67.         }
  68.     }
  69.  
  70.     public void wyswietl(Elementy v) {
  71.         if (v != null) {
  72.             wyswietl(v.left);
  73.  
  74.            // System.out.print(v.data + " ");
  75.             for (Integer i: v.lista)
  76.               //  System.out.print(i + ", ");
  77.            // System.out.print("\n");
  78.  
  79.             wyswietl(v.right);
  80.         }
  81.     }
  82.  
  83.     public Elementy get_root() {
  84.         return _root;
  85.     }
  86.  
  87.     public void set_root(Elementy _root) {
  88.         this._root = _root;
  89.     }
  90.  
  91.  
  92.  
  93.     public int maxDepth(Elementy node) {
  94.         if (node == null) {
  95.             return 0;
  96.         }
  97.         return Math.max(maxDepth(node.left),maxDepth(node.right))+1;
  98.     }
  99.  
  100.     public int minDepth(Elementy node) {
  101.         if (node == null) {
  102.             return 0;
  103.         }
  104.         return Math.min(maxDepth(node.left),maxDepth(node.right))+1;
  105.     }
  106.  
  107.     public boolean isBinaryTreeBalanced(Elementy root) {
  108.         if (root == null) {
  109.             throw new IllegalArgumentException(
  110.                     "Musi istniec korzen drzewa");
  111.         }
  112.         return this.maxDepth(root) - this.minDepth(root) <= 1;
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement