Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 5.45 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package prohlidka;
  6.  
  7. import Enum.eTypProhl;
  8. import DatovaStruktury.AbstrBinTree;
  9. import java.util.Iterator;
  10.  
  11. /**
  12.  *
  13.  * @author Georgo
  14.  */
  15. public class BinarniVyhledavaciStrom<T extends Comparable<T>> implements IBinarniVyhledavaciStrom<T> {
  16.  
  17.     AbstrBinTree<PrvekBVS> strom = new AbstrBinTree<>();
  18.  
  19.     @Override
  20.     public void zrusBVS() {
  21.         strom.zrus();
  22.     }
  23.  
  24.     @Override
  25.     public boolean jePrazdy() {
  26.         return (strom.jePrazdny());
  27.     }
  28.  
  29.     @Override
  30.     public void vloz(T prvek) {
  31.         if (this.jePrazdy()) {
  32.             this.strom.vlozKoren(new PrvekBVS(prvek));
  33.         } else if (this.najdi(prvek) == null) {
  34.             PrvekBVS aktualni = this.strom.zpristupniAktualni();
  35.             if (prvek.compareTo(aktualni.data) > 0) {
  36.                 this.strom.vlozPravy(new PrvekBVS(prvek));
  37.             } else {
  38.                 this.strom.vlozLevy(new PrvekBVS(prvek));
  39.             }
  40.         }
  41.     }
  42.  
  43.     @Override
  44.     public T najdi(T prvek) {
  45.         PrvekBVS aktualni = this.strom.zpristupniKoren();
  46.         while (aktualni != null) {
  47.             if (prvek.compareTo(aktualni.data) == 0) {
  48.                 return aktualni.data;
  49.             }
  50.             if (prvek.compareTo(aktualni.data) > 0) {
  51.                 aktualni = this.strom.zpristupniPravehoSyna();
  52.             } else {
  53.                 aktualni = this.strom.zpristupniLevehoSyna();
  54.             }
  55.         }
  56.         return null;
  57.     }
  58.  
  59.     @Override
  60.     public T odeber(T prvek) {
  61.         if (jePrazdy()) {
  62.             return null;
  63.         }
  64.         PrvekBVS koren = this.strom.zpristupniKoren();
  65.         T vyhledej = this.najdi(prvek);
  66.         if (vyhledej == null) {
  67.             return null;
  68.         }
  69.         if (koren.data.compareTo(vyhledej) == 0) {
  70.             if (this.aktualniJeList()) {
  71.                 return this.strom.odeberKoren().data;
  72.             }
  73.         }
  74.         return this.odeberAktualni();
  75.     }
  76.  
  77.     private T odeberAktualni() {
  78.         if (this.aktualniJeList()) {
  79.             return this.odeberList();
  80.         }
  81.         PrvekBVS aktualni = this.strom.zpristupniAktualni(); // slouzi k prehozeni dat
  82.        
  83.         if (this.jePravyPotomek()) {
  84.             this.nejlevejsiPotomekPravehoPotomka();
  85.         } else {
  86.             this.nejpravejsiPotomekLeveho();
  87.         }
  88.         PrvekBVS nejnej = this.strom.zpristupniAktualni();
  89.  
  90.         T pom = aktualni.data;                  // prehozeni dat reference zustava stejna
  91.         aktualni.data = nejnej.data;            // ukazatel zustava stenjy dle zasad oop
  92.         nejnej.data = pom;
  93.  
  94.         return this.odeberAktualni();
  95.     }
  96.  
  97.     private boolean jePravyPotomek() {
  98.         if (this.strom.zpristupniPravehoSyna() != null) {
  99.             this.strom.zpristupniOtce();
  100.             return true;
  101.         }
  102.         return false;
  103.     }
  104.  
  105.     private boolean jeLevyPotomek() {
  106.         if (this.strom.zpristupniLevehoSyna() != null) {
  107.             this.strom.zpristupniOtce();
  108.             return true;
  109.         }
  110.         return false;
  111.     }
  112.  
  113.     private void nejpravejsiPotomekLeveho() {
  114.         this.strom.zpristupniLevehoSyna();
  115.         while (this.strom.zpristupniPravehoSyna() != null) {
  116.         }
  117.     }
  118.  
  119.     private void nejlevejsiPotomekPravehoPotomka() {
  120.         this.strom.zpristupniPravehoSyna();
  121.         while (this.strom.zpristupniLevehoSyna() != null) {
  122.         }
  123.     }
  124.  
  125.     private T odeberList() {
  126.         PrvekBVS aktualni = this.strom.zpristupniAktualni();
  127.         this.strom.zpristupniOtce();
  128.         if (this.strom.zpristupniLevehoSyna() != null) {
  129.             if (this.strom.zpristupniAktualni().data.compareTo(aktualni.data) == 0) {
  130.                 this.strom.zpristupniOtce();
  131.                 return this.strom.odeberLevehoSyna().data;
  132.             }
  133.             this.strom.zpristupniOtce();
  134.         }
  135.         return this.strom.odeberPravehoSyna().data;
  136.     }
  137.  
  138.     private boolean aktualniJeList() {
  139.         if (this.strom.zpristupniLevehoSyna() != null) {
  140.             this.strom.zpristupniOtce();
  141.             return false;
  142.         }
  143.         if (this.strom.zpristupniPravehoSyna() != null) {
  144.             this.strom.zpristupniOtce();
  145.             return false;
  146.         }
  147.         return true;
  148.     }
  149.  
  150.     @Override
  151.     public Iterator<T> vytvorIterBVS(final eTypProhl typ) {
  152.         return new Iterator() {
  153.  
  154.             Iterator<PrvekBVS> iterator = strom.vytvorIterator(typ); // vytvarim iterator s obalovou tridou prvek do ktereho ukladam  data z bin tree
  155.  
  156.             @Override
  157.             public boolean hasNext() {
  158.                 return this.iterator.hasNext();
  159.             }
  160.  
  161.             @Override
  162.             public T next() {
  163.                 PrvekBVS prvek = this.iterator.next(); // iterator bintree , vezmu prvek z bintree
  164.                 if (prvek != null) {
  165.                     return prvek.data; // genericke data , vracim data z prvku kterumu predavam z pravku bin tree
  166.                 } else {
  167.                     return null;
  168.                 }
  169.             }
  170.  
  171.             @Override
  172.             public void remove() {
  173.                 throw new UnsupportedOperationException("Not supported yet.");
  174.             }
  175.         };
  176.     }
  177.  
  178.     private class PrvekBVS { //obalova trida kvuli prohazovani uzlu v binstranomu
  179.  
  180.         T data;
  181.  
  182.         public PrvekBVS(T data) {
  183.             this.data = data;
  184.         }
  185.     }
  186. }