Guest User

Untitled

a guest
Dec 7th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 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 aiz.lb9999;
  7.  
  8. import java.util.Scanner;
  9.  
  10.  
  11. public class MyTree extends BinTree {
  12.  
  13. public MyTree(int dane) {
  14. super(dane);
  15. }
  16. public MyTree() {
  17. super(999);
  18. }
  19. @Override
  20. public void print(int poziom) {
  21. if(lewy!=null) lewy.print(poziom +1);
  22. for (int i = 0; i < poziom; i++) {
  23. System.out.print("\t");
  24. }
  25. System.out.println(dane);
  26. if(prawy!=null) prawy.print(poziom +1);
  27.  
  28. }
  29.  
  30. @Override
  31. public void readTree() {
  32. Scanner in = new Scanner(System.in);
  33. String tmp;
  34. System.out.println("Podaj wartosc do wezla");
  35. tmp=in.nextLine();
  36. dane=Integer.parseInt(tmp);
  37. System.out.println("Jestes w wiezle "+dane);
  38. System.out.println("Czy chcesz dodac lewa galaz? (t/n)");
  39. tmp=in.nextLine();
  40. if(tmp.equals("t")){
  41. lewy = new MyTree();
  42. lewy.readTree();
  43. }
  44. System.out.println("Jestes w wiezle "+dane);
  45. System.out.println("Czy chcesz dodac prawa galaz? (t/n)");
  46. tmp=in.nextLine();
  47. if(tmp.equals("t")){
  48. prawy = new MyTree();
  49. prawy.readTree();
  50. }
  51.  
  52. }
  53.  
  54. @Override
  55. public void printPre() {
  56. System.out.print(dane+" ");
  57. if(lewy!=null) lewy.printPre();
  58. if(prawy!=null) prawy.printPre();
  59. }
  60.  
  61. @Override
  62. public void printIn() {
  63. if(lewy!=null) lewy.printIn();
  64. System.out.print(dane+" ");
  65. if(prawy!=null) prawy.printIn();
  66.  
  67. }
  68.  
  69. @Override
  70. public void printPost() {
  71. if(lewy!=null) lewy.printPost();
  72. if(prawy!=null) prawy.printPost();
  73. System.out.print(dane+" ");
  74. }
  75.  
  76. @Override
  77. public boolean searchBST(int szukany) {
  78. if(dane == szukany) return true;
  79. if(szukany < dane) if(lewy!=null) return lewy.searchBST(szukany);
  80. if(szukany > dane) if(prawy!=null) return prawy.searchBST(szukany);
  81. return false;
  82. }
  83.  
  84. @Override
  85. public void addBST(int nowy) {
  86. if(searchBST(nowy)) System.out.println("Jest już w drzeewie");
  87. else
  88. if(nowy < dane){
  89. if(lewy!=null) lewy.addBST(nowy);
  90. else lewy = new MyTree(nowy);
  91. }
  92. if(nowy> dane){
  93. if(prawy!=null) prawy.addBST(nowy);
  94. else prawy = new MyTree(nowy);
  95. }
  96. }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment