Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ejercicio3;
- import java.util.ArrayList;
- /**
- *
- * @author Jorge Fraga Neiro : jorge.fragan
- * Alexandre Gosende Monteagudo : a.gosende
- * @param <E>
- */
- public class TournamentHeap <E> {
- ArrayList <E> heap;
- public void TournamentHeap () {
- heap = new ArrayList<>();
- }
- public E Root() {
- return heap.get(0);
- }
- public void add(E e) {
- heap.add(e);
- }
- public int indexOf(E e) {
- return heap.indexOf(e);
- }
- public int parentIndex (E e) {
- int i = heap.indexOf(e);
- if (i == -1) {
- throw new IllegalArgumentException("Element is not a part of the bintree!");
- } else if (i == 0) {
- throw new IllegalArgumentException("Element is the root of the bintree!");
- } else {
- i = ((i-1)%2);
- }
- return i;
- }
- public boolean isLeaf (E e) {
- int i = heap.indexOf(e);
- int size = heap.size();
- i = i * 2;
- if ((i + 1) > size) {
- return true;
- } else return false;
- }
- public int leftsonIndex(E e) {
- int i = heap.indexOf(e);
- if (isLeaf(e)) {
- throw new IllegalArgumentException("Element is a leaf, so he does not have a left son!");
- } else {
- return (i * 2 + 1);
- }
- }
- public int rightsonIndex(E e) {
- int i = heap.indexOf(e);
- i = i*2+2;
- if (i < heap.size()) {
- return i;
- } else {
- throw new IllegalArgumentException("Element does not have a right son!");
- }
- }
- public boolean contains(E e) {
- return heap.contains(e);
- }
- public int size() {
- return heap.size();
- }
- public void clear() {
- heap.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement