Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```public class MyTree{
- private MyTree left, right;
- private char value = 0;
- private int counter = 0;
- public MyTree(){}
- public MyTree(char c){
- add(c);
- }
- private MyTree(char c,int counter){
- add(c);
- this.counter = counter++;
- }
- public MyTree add(char c){
- if (value == 0){
- this.value = c;
- }
- else if(c > value){
- if(right == null){
- right = new MyTree(c);
- }else right.add(c);
- }
- else if(c < value){
- if(left == null){
- left = new MyTree(c);
- }else left.add(c);
- counter++;
- }
- return this;
- }
- public MyTree add(char[] chars){
- for (int i = 0; i < chars.length; i++) {
- add(chars[i]);
- }
- return this;
- }
- public void count(char c){
- }
- @Override
- public String toString() {
- String tmp = "[";
- if(left != null){
- tmp += left.toString() + ", ";
- }
- if(value != 0){
- tmp += value + ", ";
- }
- if(right != null){
- tmp += right.toString() + ", ";
- }
- tmp += "]";
- return tmp;
- }
- public int getCount(){
- return counter;
- }
- public MyTree printTree(){
- System.out.println(toString());
- return this;
- }
- public MyTree printCount(){
- System.out.println(getCount());
- return this;
- }
- public static void main(String[] args) {
- MyTree myTreeElement = new MyTree();
- char[] lettersToPrint = new char[]{
- 'a','a','b','c','d', 'e','f','g','h','i','j'
- };
- myTreeElement.add(lettersToPrint)
- .printTree()
- .printCount();
- }
- }```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement