Advertisement
PedroPauloFO

Métodos dúvida Roteiro 12

Apr 21st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. AVLTree>>>>>>>>>>>>>>>>>>>>>>>>
  2.  
  3.  @Override
  4.     public void insert(T value) {
  5.         recursiveInsert(this.root, value);
  6.     };
  7.    
  8.     private void recursiveInsert(BSTNode<T> node, T element) {
  9.         if (node.isEmpty()) {
  10.             // cria um node sentinela cujo apontador pai é o node dado
  11.             BSTNode<T> nil = new BSTNode<T>();
  12.             nil.setParent(node);
  13.            
  14.             node.setData(element);
  15.             node.setLeft(nil);
  16.             node.setRight(nil);
  17.            
  18.         } else {
  19.             if (element.compareTo(node.getData()) < 0) recursiveInsert((BSTNode<T>) node.getLeft(), element);
  20.             else if (element.compareTo(node.getData()) >= 0) recursiveInsert((BSTNode<T>) node.getRight(), element);
  21.            
  22.             rebalance(node);
  23.         }
  24.     }
  25.  
  26.  //AUXILIARY
  27.     protected int calculateBalance(BSTNode<T> node) {
  28.         return height((BSTNode<T>) node.getLeft()) - height((BSTNode<T>) node.getRight());
  29.     }
  30.    
  31.     //AUXILIARY
  32.     protected void rebalance(BSTNode<T> node) {
  33.         int balance = calculateBalance(node);
  34.        
  35.         // se o nó pesa pra esquerda
  36.         if (balance > 1) {
  37.             int leftBalance = calculateBalance((BSTNode<T>) node.getLeft());
  38.            
  39.             if (leftBalance >= 0) rightRotation(node);
  40.            
  41.             else if (leftBalance < 0) {
  42.                 leftRotation((BSTNode<T>) node.getLeft());
  43.                 rightRotation(node);
  44.             }
  45.            
  46.         // se o nó pesa pra direita  
  47.         } else if (balance < 1) {
  48.             int rightBalance = calculateBalance((BSTNode<T>) node.getRight());
  49.            
  50.             if (rightBalance <= 0) leftRotation(node);
  51.            
  52.             else if (rightBalance > 0) {
  53.                 rightRotation((BSTNode<T>) node.getRight());
  54.                 leftRotation(node);
  55.             }
  56.         }
  57.     }
  58.  
  59.  
  60.  
  61. BST>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  62.  
  63. /**
  64.      * This method is already implemented using recursion. You must understand how it work and
  65.      * use similar idea with the other methods.
  66.      */
  67.     @Override
  68.     public int size() {
  69.         return size(root);
  70.     }
  71.     private int size(BSTNode<T> node){
  72.         int result = 0;
  73.         //base case means doing nothing (return 0)
  74.         if(!node.isEmpty()){ //inductive case
  75.             result = 1 + size((BSTNode<T>)node.getLeft()) + size((BSTNode<T>)node.getRight());
  76.         }
  77.         return result;
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement