Advertisement
Guest User

TournamentHeap

a guest
Nov 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. package ejercicio3;
  2.  
  3.  
  4. import java.util.ArrayList;
  5.  
  6. /**
  7.  *
  8.  * @author Jorge Fraga Neiro : jorge.fragan
  9.  *                  Alexandre Gosende Monteagudo : a.gosende
  10.  *  @param <E>
  11.  */
  12. public class TournamentHeap <E> {
  13.     ArrayList <E> heap;
  14.  
  15.     public void TournamentHeap () {
  16.         heap = new ArrayList<>();
  17.     }
  18.    
  19.     public E Root() {
  20.         return heap.get(0);
  21.     }
  22.    
  23.     public void add(E e) {
  24.         heap.add(e);
  25.     }
  26.    
  27.     public int indexOf(E e) {
  28.         return heap.indexOf(e);
  29.     }
  30.    
  31.     public int parentIndex (E e) {
  32.         int i = heap.indexOf(e);
  33.        
  34.         if (i == -1) {
  35.             throw new IllegalArgumentException("Element is not a part of the bintree!");
  36.         } else if (i == 0) {
  37.             throw new IllegalArgumentException("Element is the root of the bintree!");
  38.         } else {
  39.             i = ((i-1)%2);
  40.         }
  41.         return i;
  42.     }
  43.    
  44.     public boolean isLeaf (E e) {
  45.         int i = heap.indexOf(e);
  46.         int size = heap.size();
  47.        
  48.         i = i * 2;
  49.         if ((i  + 1) > size) {
  50.             return true;
  51.         } else return false;
  52.     }
  53.    
  54.     public int leftsonIndex(E e) {
  55.         int i = heap.indexOf(e);
  56.        
  57.         if (isLeaf(e)) {
  58.             throw new IllegalArgumentException("Element is a leaf, so he does not have a left son!");
  59.         } else {
  60.             return (i * 2 + 1);
  61.         }
  62.     }
  63.    
  64.     public int rightsonIndex(E e) {
  65.         int i = heap.indexOf(e);
  66.        
  67.         i = i*2+2;
  68.         if (i < heap.size()) {
  69.             return i;
  70.         } else {
  71.             throw new IllegalArgumentException("Element does not have a right son!");
  72.         }
  73.     }
  74.    
  75.     public boolean contains(E e) {
  76.         return heap.contains(e);
  77.     }
  78.    
  79.     public int size() {
  80.         return heap.size();
  81.     }
  82.    
  83.     public void clear() {
  84.         heap.clear();
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement