Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion src/Tree.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,65 @@
public class Tree {
import java.sql.Array;
import java.util.*;


public class Tree<T extends Comparable<T>>{
private T root;
private ArrayList<Tree<T>> subtrees;


public Tree(T root, ArrayList<Tree<T>> 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<T> 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<T> 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<T> subtree : this.subtrees){
s.append(subtree._str_indented(1));
}
return s.toString();
}
}


}