Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package aiz.lb9999;
- import java.util.Scanner;
- public class MyTree extends BinTree {
- public MyTree(int dane) {
- super(dane);
- }
- public MyTree() {
- super(999);
- }
- @Override
- public void print(int poziom) {
- if(lewy!=null) lewy.print(poziom +1);
- for (int i = 0; i < poziom; i++) {
- System.out.print("\t");
- }
- System.out.println(dane);
- if(prawy!=null) prawy.print(poziom +1);
- }
- @Override
- public void readTree() {
- Scanner in = new Scanner(System.in);
- String tmp;
- System.out.println("Podaj wartosc do wezla");
- tmp=in.nextLine();
- dane=Integer.parseInt(tmp);
- System.out.println("Jestes w wiezle "+dane);
- System.out.println("Czy chcesz dodac lewa galaz? (t/n)");
- tmp=in.nextLine();
- if(tmp.equals("t")){
- lewy = new MyTree();
- lewy.readTree();
- }
- System.out.println("Jestes w wiezle "+dane);
- System.out.println("Czy chcesz dodac prawa galaz? (t/n)");
- tmp=in.nextLine();
- if(tmp.equals("t")){
- prawy = new MyTree();
- prawy.readTree();
- }
- }
- @Override
- public void printPre() {
- System.out.print(dane+" ");
- if(lewy!=null) lewy.printPre();
- if(prawy!=null) prawy.printPre();
- }
- @Override
- public void printIn() {
- if(lewy!=null) lewy.printIn();
- System.out.print(dane+" ");
- if(prawy!=null) prawy.printIn();
- }
- @Override
- public void printPost() {
- if(lewy!=null) lewy.printPost();
- if(prawy!=null) prawy.printPost();
- System.out.print(dane+" ");
- }
- @Override
- public boolean searchBST(int szukany) {
- if(dane == szukany) return true;
- if(szukany < dane) if(lewy!=null) return lewy.searchBST(szukany);
- if(szukany > dane) if(prawy!=null) return prawy.searchBST(szukany);
- return false;
- }
- @Override
- public void addBST(int nowy) {
- if(searchBST(nowy)) System.out.println("Jest już w drzeewie");
- else
- if(nowy < dane){
- if(lewy!=null) lewy.addBST(nowy);
- else lewy = new MyTree(nowy);
- }
- if(nowy> dane){
- if(prawy!=null) prawy.addBST(nowy);
- else prawy = new MyTree(nowy);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment