diff --git a/src/Tree.java b/src/Tree.java index 458a7c7..7fbf37e 100644 --- a/src/Tree.java +++ b/src/Tree.java @@ -1,2 +1,65 @@ -public class Tree { +import java.sql.Array; +import java.util.*; + + +public class Tree>{ + private T root; + private ArrayList> subtrees; + + + public Tree(T root, ArrayList> subtrees){ + if (root == null){ + this.subtrees = new ArrayList<>(); + } else { + this.root = root; + this.subtrees = subtrees; + } + } + + + public int count(T item){ + if (this.root == null){ + return 0; + } else { + int counter = 0; + if (this.root.equals(item)){ + counter++; + } + for (Tree subtree : subtrees){ + counter += subtree.count(item); + } + return counter; + } + } + + + public String __str__(){ + return this.str_indented(); + } + + + private String _str_indented(int depth){ + if (this.is_empty()) { + return ""; + } else { + StringBuilder s = new StringBuilder(' ' * depth + (String) this.root + "\n"); + for (Tree subtree : this.subtrees){ + s.append(subtree._str_indented(depth + 1)); + } + return s.toString(); + } + } + private String _str_indented(){ + if (this.is_empty()){ + return ""; + } else { + StringBuilder s = new StringBuilder((String) this.root + "\n"); + for (Tree subtree : this.subtrees){ + s.append(subtree._str_indented(1)); + } + return s.toString(); + } + } + + }