Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. ```public class MyTree{
  2.     private MyTree left, right;
  3.     private char value = 0;
  4.     private int counter = 0;
  5.     public MyTree(){}
  6.    
  7.     public MyTree(char c){
  8.         add(c);
  9.     }
  10.     private MyTree(char c,int counter){
  11.         add(c);
  12.         this.counter = counter++;
  13.     }
  14.     public MyTree add(char c){
  15.         if (value == 0){
  16.             this.value = c;
  17.         }
  18.         else if(c > value){
  19.             if(right == null){
  20.                 right = new MyTree(c);
  21.             }else right.add(c);
  22.         }
  23.         else if(c < value){
  24.             if(left == null){
  25.                 left = new MyTree(c);
  26.             }else left.add(c);
  27.             counter++;
  28.         }
  29.         return this;
  30.     }
  31.     public MyTree add(char[] chars){
  32.         for (int i = 0; i < chars.length; i++) {
  33.             add(chars[i]);
  34.         }
  35.         return this;
  36.     }
  37.     public void count(char c){
  38.     }
  39.     @Override
  40.     public String toString() {
  41.         String tmp = "[";
  42.         if(left != null){
  43.             tmp += left.toString() + ", ";
  44.         }
  45.         if(value != 0){
  46.             tmp += value + ", ";
  47.         }
  48.         if(right != null){
  49.             tmp += right.toString() + ", ";
  50.         }
  51.         tmp += "]";
  52.         return tmp;
  53.     }
  54.     public int getCount(){
  55.         return counter;
  56.     }
  57.     public MyTree printTree(){
  58.         System.out.println(toString());
  59.         return this;
  60.     }
  61.     public MyTree printCount(){
  62.         System.out.println(getCount());
  63.         return this;
  64.     }
  65.     public static void main(String[] args) {
  66.         MyTree myTreeElement = new MyTree();
  67.         char[] lettersToPrint = new char[]{
  68.             'a','a','b','c','d', 'e','f','g','h','i','j'
  69.         };
  70.         myTreeElement.add(lettersToPrint)
  71.         .printTree()
  72.         .printCount();
  73.     }
  74. }```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement