Manioc

AVLuto

Jul 10th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. package adt.avltree;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import adt.bst.BSTImpl;
  6. import adt.bst.BSTNode;
  7. import adt.bt.Util;
  8.  
  9. /**
  10.  *
  11.  * Performs consistency validations within a AVL Tree instance
  12.  *
  13.  * @author Claudio Campelo
  14.  *
  15.  * @param <T>
  16.  */
  17. public class AVLTreeImpl<T extends Comparable<T>> extends BSTImpl<T> implements
  18.         AVLTree<T> {
  19.  
  20.     // TODO Do not forget: you must override the methods insert and remove
  21.     // conveniently.
  22.  
  23.     // AUXILIARY
  24.     protected int calculateBalance(BSTNode<T> node) {
  25.         return height((BSTNode<T>)node.getLeft())-height((BSTNode) node.getRight());
  26.     }
  27.    
  28.     protected void toLeft(BSTNode<T> node) {
  29.         BSTNode<T> center = Util.leftRotation(node);
  30.         if(node.equals(this.root)) this.root = center;
  31.     }
  32.    
  33.     protected void toRight(BSTNode<T> node) {
  34.         BSTNode<T> center = Util.rightRotation(node);
  35.         if(node.equals(this.root)) this.root = center;
  36.     }
  37.     // AUXILIARY
  38.     protected void rebalance(BSTNode<T> node) {
  39.         if(node != null) {
  40.             int balance = this.calculateBalance(node);
  41.            
  42.             if(balance > 1) {
  43.                 int childBalance = calculateBalance((BSTNode<T>)node.getLeft());
  44.                 if(childBalance > 0) {
  45.                     toRight(node);
  46.                 }else {
  47.                     toLeft((BSTNode<T>)node.getLeft());
  48.                     toRight(node);
  49.                 }
  50.             }
  51.             if(balance < -1) {
  52.                 int childBalance = calculateBalance((BSTNode<T>)node.getRight());
  53.                 if(childBalance < 0) {
  54.                     toLeft(node);
  55.                 }else {
  56.                     toRight((BSTNode<T>)node.getRight());
  57.                     toLeft(node);
  58.                 }
  59.             }
  60.            
  61.         }
  62.        
  63.     }
  64.  
  65.     // AUXILIARY
  66.     protected void rebalanceUp(BSTNode<T> node) {
  67.         if(node != null) {
  68.             rebalance(node);
  69.             rebalanceUp((BSTNode<T>)node.getParent());
  70.         }
  71.     }
  72.    
  73.     @Override
  74.     public void insert(T element) {
  75.         super.insert(element);
  76.         BSTNode<T> node = search(element);
  77.         rebalanceUp(node);
  78.     }
  79.    
  80.     @Override
  81.     public void remove(T element) {
  82.        if (element != null) {
  83.           BSTNode<T> no = search(element);
  84.           remove(no);
  85.        }
  86.     }
  87.  
  88.     private void remove(BSTNode<T> no) {
  89.        if(!no.isEmpty() && !no.equals(new BSTNode<T>())) {
  90.           if(no.isLeaf()) {
  91.               super.replace(no, new BSTNode());
  92.               rebalanceUp((BSTNode<T>)no.getParent());
  93.           }else if(no.getRight().isEmpty()) {
  94.               super.replace(no,(BSTNode<T>) no.getLeft());
  95.               this.rebalanceUp((BSTNode<T>)no.getParent());
  96.           }else if(no.getLeft().isEmpty()) {     
  97.               super.replace(no, (BSTNode<T>) no.getRight());
  98.               rebalanceUp((BSTNode<T>)no.getParent());
  99.           }else {
  100.               BSTNode<T> sucess = sucessor(no.getData());
  101.               no.setData(sucess.getData());
  102.               remove(sucess);
  103.           }
  104.        }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment