- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package prohlidka;
- import Enum.eTypProhl;
- import DatovaStruktury.AbstrBinTree;
- import java.util.Iterator;
- /**
- *
- * @author Georgo
- */
- public class BinarniVyhledavaciStrom<T extends Comparable<T>> implements IBinarniVyhledavaciStrom<T> {
- AbstrBinTree<PrvekBVS> strom = new AbstrBinTree<>();
- @Override
- public void zrusBVS() {
- strom.zrus();
- }
- @Override
- public boolean jePrazdy() {
- return (strom.jePrazdny());
- }
- @Override
- public void vloz(T prvek) {
- if (this.jePrazdy()) {
- this.strom.vlozKoren(new PrvekBVS(prvek));
- } else if (this.najdi(prvek) == null) {
- PrvekBVS aktualni = this.strom.zpristupniAktualni();
- if (prvek.compareTo(aktualni.data) > 0) {
- this.strom.vlozPravy(new PrvekBVS(prvek));
- } else {
- this.strom.vlozLevy(new PrvekBVS(prvek));
- }
- }
- }
- @Override
- public T najdi(T prvek) {
- PrvekBVS aktualni = this.strom.zpristupniKoren();
- while (aktualni != null) {
- if (prvek.compareTo(aktualni.data) == 0) {
- return aktualni.data;
- }
- if (prvek.compareTo(aktualni.data) > 0) {
- aktualni = this.strom.zpristupniPravehoSyna();
- } else {
- aktualni = this.strom.zpristupniLevehoSyna();
- }
- }
- return null;
- }
- @Override
- public T odeber(T prvek) {
- if (jePrazdy()) {
- return null;
- }
- PrvekBVS koren = this.strom.zpristupniKoren();
- T vyhledej = this.najdi(prvek);
- if (vyhledej == null) {
- return null;
- }
- if (koren.data.compareTo(vyhledej) == 0) {
- if (this.aktualniJeList()) {
- return this.strom.odeberKoren().data;
- }
- }
- return this.odeberAktualni();
- }
- private T odeberAktualni() {
- if (this.aktualniJeList()) {
- return this.odeberList();
- }
- PrvekBVS aktualni = this.strom.zpristupniAktualni(); // slouzi k prehozeni dat
- if (this.jePravyPotomek()) {
- this.nejlevejsiPotomekPravehoPotomka();
- } else {
- this.nejpravejsiPotomekLeveho();
- }
- PrvekBVS nejnej = this.strom.zpristupniAktualni();
- T pom = aktualni.data; // prehozeni dat reference zustava stejna
- aktualni.data = nejnej.data; // ukazatel zustava stenjy dle zasad oop
- nejnej.data = pom;
- return this.odeberAktualni();
- }
- private boolean jePravyPotomek() {
- if (this.strom.zpristupniPravehoSyna() != null) {
- this.strom.zpristupniOtce();
- return true;
- }
- return false;
- }
- private boolean jeLevyPotomek() {
- if (this.strom.zpristupniLevehoSyna() != null) {
- this.strom.zpristupniOtce();
- return true;
- }
- return false;
- }
- private void nejpravejsiPotomekLeveho() {
- this.strom.zpristupniLevehoSyna();
- while (this.strom.zpristupniPravehoSyna() != null) {
- }
- }
- private void nejlevejsiPotomekPravehoPotomka() {
- this.strom.zpristupniPravehoSyna();
- while (this.strom.zpristupniLevehoSyna() != null) {
- }
- }
- private T odeberList() {
- PrvekBVS aktualni = this.strom.zpristupniAktualni();
- this.strom.zpristupniOtce();
- if (this.strom.zpristupniLevehoSyna() != null) {
- if (this.strom.zpristupniAktualni().data.compareTo(aktualni.data) == 0) {
- this.strom.zpristupniOtce();
- return this.strom.odeberLevehoSyna().data;
- }
- this.strom.zpristupniOtce();
- }
- return this.strom.odeberPravehoSyna().data;
- }
- private boolean aktualniJeList() {
- if (this.strom.zpristupniLevehoSyna() != null) {
- this.strom.zpristupniOtce();
- return false;
- }
- if (this.strom.zpristupniPravehoSyna() != null) {
- this.strom.zpristupniOtce();
- return false;
- }
- return true;
- }
- @Override
- public Iterator<T> vytvorIterBVS(final eTypProhl typ) {
- return new Iterator() {
- Iterator<PrvekBVS> iterator = strom.vytvorIterator(typ); // vytvarim iterator s obalovou tridou prvek do ktereho ukladam data z bin tree
- @Override
- public boolean hasNext() {
- return this.iterator.hasNext();
- }
- @Override
- public T next() {
- PrvekBVS prvek = this.iterator.next(); // iterator bintree , vezmu prvek z bintree
- if (prvek != null) {
- return prvek.data; // genericke data , vracim data z prvku kterumu predavam z pravku bin tree
- } else {
- return null;
- }
- }
- @Override
- public void remove() {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- };
- }
- private class PrvekBVS { //obalova trida kvuli prohazovani uzlu v binstranomu
- T data;
- public PrvekBVS(T data) {
- this.data = data;
- }
- }
- }