Advertisement
xickoh

array binary tree

Jan 29th, 2021
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ed_ficha09;
  7.  
  8. import java.util.Iterator;
  9.  
  10. /**
  11.  *
  12.  * @author franc
  13.  */
  14. public class ArrayBinaryTree<T> implements BinaryTreeADT<T> {
  15.  
  16.     protected int count;
  17.     protected T[] tree;
  18.     private final int CAPACITY = 50;
  19.  
  20.     /**
  21.      * Creates an empty binary tree.
  22.      */
  23.     public ArrayBinaryTree() {
  24.         count = 0;
  25.         tree = (T[]) new Object[CAPACITY];
  26.     }
  27.  
  28.     /**
  29.      * Creates a binary tree with the specified element as its root.
  30.      *
  31.      * @param element the element which will become the root of the new tree
  32.      */
  33.     public ArrayBinaryTree(T element) {
  34.         count = 1;
  35.         tree = (T[]) new Object[CAPACITY];
  36.         tree[0] = element;
  37.     }
  38.  
  39.     /**
  40.      * Returns a reference to the specified target element if it is found in
  41.      * this binary tree. Throws a NoSuchElementException if the specified target
  42.      * element is not found in the binary tree.
  43.      *
  44.      * @param targetElement the element being sought in the tree
  45.      * @return true if the element is in the tree
  46.      * @throws ElementNotFoundException if an element not found exception occurs
  47.      */
  48.     public T find(T targetElement) throws ElementNotFoundException {
  49.         T temp = null;
  50.         boolean found = false;
  51.  
  52.         for (int ct = 0; ct < count && !found; ct++) {
  53.             if (targetElement.equals(tree[ct])) {
  54.                 found = true;
  55.                 temp = tree[ct];
  56.             }
  57.         }
  58.         if (!found) {
  59.             throw new ElementNotFoundException("binary tree");
  60.         }
  61.         return temp;
  62.     }
  63.  
  64.     @Override
  65.     public T getRoot() {
  66.         return tree[0];
  67.     }
  68.  
  69.     @Override
  70.     public boolean isEmpty() {
  71.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  72.     }
  73.  
  74.     @Override
  75.     public int size() {
  76.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  77.     }
  78.  
  79.     @Override
  80.     public boolean contains(T targetElement) throws ElementNotFoundException {
  81.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  82.     }
  83.  
  84.     @Override
  85.     public Iterator<T> iteratorInOrder() {
  86.         ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
  87.         inorder(0, templist);
  88.         return templist.iterator();
  89.     }
  90.  
  91.     protected void inorder(int node, ArrayUnorderedList<T> templist) {
  92.         if (node < tree.length) {
  93.             if (tree[node] != null) {
  94.                 inorder(node * 2 + 1, templist);
  95.                 templist.addToRear(tree[node]);
  96.                 inorder((node + 1) * 2, templist);
  97.             }
  98.         }
  99.     }
  100.  
  101.     @Override
  102.     public Iterator<T> iteratorPreOrder() {
  103.         ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
  104.         preorder(0, templist);
  105.         return templist.iterator();
  106.     }
  107.  
  108.     protected void preorder(int node, ArrayUnorderedList<T> templist) {
  109.         if (node < tree.length) {
  110.             if (tree[node] != null) {
  111.                 templist.addToRear(tree[node]);
  112.                 preorder(node * 2 + 1, templist);
  113.                 preorder((node + 1) * 2, templist);
  114.             }
  115.         }
  116.     }
  117.  
  118.     @Override
  119.     public Iterator<T> iteratorPostOrder() {
  120.         ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
  121.         postorder(0, templist);
  122.         return templist.iterator();
  123.     }
  124.  
  125.     protected void postorder(int node, ArrayUnorderedList<T> templist) {
  126.         if (node < tree.length) {
  127.             if (tree[node] != null) {
  128.                 preorder(node * 2 + 1, templist);
  129.                 preorder((node + 1) * 2, templist);
  130.                 templist.addToRear(tree[node]);
  131.             }
  132.         }
  133.     }
  134.  
  135.     @Override
  136.     public Iterator<T> iteratorLevelOrder() throws EmptyCollectionException {
  137.         QueueADT<Integer> values = new LinkedQueue<T>();
  138.         ArrayUnorderedList<Integer> results = new ArrayUnorderedList<Integer>();
  139.         int index = 0;
  140.  
  141.         values.enqueue(0);
  142.  
  143.         while (!(values.isEmpty())) {
  144.  
  145.             try {
  146.                 index = values.dequeue();
  147.  
  148.                 if (tree[index] != null) {
  149.                     results.addToRear(index);
  150.  
  151.                     if (this.tree[(index * 2) + 1] != null) {
  152.                         values.enqueue(index * 2 + 1);
  153.                     }
  154.  
  155.                     if (this.tree[(index + 1) * 2] != null) {
  156.                         values.enqueue((index + 1) * 2);
  157.                     }
  158.                 }
  159.             } catch (EmptyCollectionException ex) {
  160.                 System.out.println("Vazio!!!");
  161.             }
  162.  
  163.         }
  164.  
  165.         return results.iterator();
  166.     }
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement