Advertisement
kamasazi99

aiz 9 lab

Dec 8th, 2019
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 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 javaapplication114;
  7.  
  8. import java.util.*;
  9. public class drzewo extends BinaryTreeNode {
  10.    
  11.     public drzewo (int dane){
  12.         super(dane);
  13.        
  14.     }
  15.  
  16.     @Override
  17.     public void print(int poziom) {
  18.         if(lewy!=null){
  19.             lewy.print(poziom+1);
  20.         }
  21.         for (int i = 0; i < poziom; i++) {
  22.             System.out.print("    ");
  23.         }
  24.         System.out.println(dane);
  25.         if(prawy!=null){
  26.             prawy.print(poziom+1);
  27.         }
  28.     }
  29.  
  30.     @Override
  31.     public boolean searchBSTRec(int szukany) {
  32.     if(dane==szukany){
  33.             return true;
  34.         }
  35.         if(szukany<=dane && lewy!=null){
  36.             return lewy.searchBSTRec(szukany);
  37.         }
  38.         if(szukany>=dane && prawy!=null){
  39.             return prawy.searchBSTRec(szukany);
  40.         }
  41.         else
  42.             return false;
  43.     }
  44.  
  45.     @Override
  46.     public void addBSTRec(int nowy) {
  47.          if (!searchBSTRec(nowy)) {
  48.         if(nowy<dane){
  49.             if(lewy==null){
  50.                 lewy=new drzewo(nowy);
  51.             }
  52.             else {
  53.                 lewy.addBSTRec(nowy);
  54.             }
  55.         }
  56.         if(nowy>dane){
  57.             if(prawy==null){
  58.                 prawy=new drzewo(nowy);
  59.             }
  60.             else {
  61.                 prawy.addBSTRec(nowy);
  62.             }
  63.         }
  64.     }
  65.     }
  66.  
  67.     @Override
  68.     public Pair<BinaryTreeNode, BinaryTreeNode> searchBST(int szukany) {
  69.        Pair<BinaryTreeNode, BinaryTreeNode> p = new Pair<>(null, null);
  70.         p.first=this;
  71.         while (szukany != p.first.dane) {
  72.             if (szukany < p.first.dane) {
  73.                 p.second = p.first;
  74.                 p.first = p.first.lewy;
  75.             } else if (szukany > p.first.dane) {
  76.                 p.second = p.first;
  77.                 p.first = p.first.prawy;
  78.             }
  79.             if (p.first == null) {
  80.                 break;
  81.             }
  82.         }
  83.        
  84.         System.out.println("second: " + p.second);
  85.         System.out.println("first: " + p.first);
  86.         return p;
  87.     }
  88.    
  89.    
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96. /*
  97.  * To change this license header, choose License Headers in Project Properties.
  98.  * To change this template file, choose Tools | Templates
  99.  * and open the template in the editor.
  100.  */
  101. package javaapplication114;
  102.  
  103. /**
  104.  *
  105.  * @author User
  106.  */
  107. public class Lab9 {
  108.  
  109.     /** arguments
  110.      */
  111.     public static void main(String[] args) {
  112.      drzewo x=new drzewo(8);
  113.      
  114.      x.addBSTRec(3);
  115.        x.addBSTRec(10);
  116.      
  117.        x.addBSTRec(1);
  118.        x.addBSTRec(6);
  119.        x.addBSTRec(14);
  120.        x.addBSTRec(7);
  121.        x.addBSTRec(4);
  122.        x.addBSTRec(13);
  123.                    System.out.println(x.searchBSTRec(3));
  124.                    System.out.println(x.searchBSTRec(10));
  125.                  
  126.                    System.out.println("----------------");
  127.                     x.print(0);
  128.                     System.out.println("---------------");
  129.                  x.searchBST(7);
  130.                    
  131.     }
  132.    
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement