Advertisement
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 javaapplication114;
- import java.util.*;
- public class drzewo extends BinaryTreeNode {
- public drzewo (int dane){
- super(dane);
- }
- @Override
- public void print(int poziom) {
- if(lewy!=null){
- lewy.print(poziom+1);
- }
- for (int i = 0; i < poziom; i++) {
- System.out.print(" ");
- }
- System.out.println(dane);
- if(prawy!=null){
- prawy.print(poziom+1);
- }
- }
- @Override
- public boolean searchBSTRec(int szukany) {
- if(dane==szukany){
- return true;
- }
- if(szukany<=dane && lewy!=null){
- return lewy.searchBSTRec(szukany);
- }
- if(szukany>=dane && prawy!=null){
- return prawy.searchBSTRec(szukany);
- }
- else
- return false;
- }
- @Override
- public void addBSTRec(int nowy) {
- if (!searchBSTRec(nowy)) {
- if(nowy<dane){
- if(lewy==null){
- lewy=new drzewo(nowy);
- }
- else {
- lewy.addBSTRec(nowy);
- }
- }
- if(nowy>dane){
- if(prawy==null){
- prawy=new drzewo(nowy);
- }
- else {
- prawy.addBSTRec(nowy);
- }
- }
- }
- }
- @Override
- public Pair<BinaryTreeNode, BinaryTreeNode> searchBST(int szukany) {
- Pair<BinaryTreeNode, BinaryTreeNode> p = new Pair<>(null, null);
- p.first=this;
- while (szukany != p.first.dane) {
- if (szukany < p.first.dane) {
- p.second = p.first;
- p.first = p.first.lewy;
- } else if (szukany > p.first.dane) {
- p.second = p.first;
- p.first = p.first.prawy;
- }
- if (p.first == null) {
- break;
- }
- }
- System.out.println("second: " + p.second);
- System.out.println("first: " + p.first);
- return p;
- }
- }
- /*
- * 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 javaapplication114;
- /**
- *
- * @author User
- */
- public class Lab9 {
- /** arguments
- */
- public static void main(String[] args) {
- drzewo x=new drzewo(8);
- x.addBSTRec(3);
- x.addBSTRec(10);
- x.addBSTRec(1);
- x.addBSTRec(6);
- x.addBSTRec(14);
- x.addBSTRec(7);
- x.addBSTRec(4);
- x.addBSTRec(13);
- System.out.println(x.searchBSTRec(3));
- System.out.println(x.searchBSTRec(10));
- System.out.println("----------------");
- x.print(0);
- System.out.println("---------------");
- x.searchBST(7);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement