Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added count method #53

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Binary file added out/production/multiset-adt/Tree.class
Binary file not shown.
108 changes: 107 additions & 1 deletion src/Tree.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,108 @@
public class Tree {
import java.util.ArrayList;
import java.util.List;

public class Tree<T> {
// Private Attributes
// The item stored at this tree's root, or null if the tree is empty.
private T root;

}
private int count(int item) {
if (this.isEmpty()) {
// The list of all subtrees of this tree.
private List<Tree<T>> subtrees;

public Tree(T root) {
this.root = root;
this.subtrees = new ArrayList<>();
}

public Tree(T root, List<Tree<T>> subtrees) {
this.root = root;
this.subtrees = new ArrayList<>(subtrees);
}

public T getRoot() {
return root;
}

public List<Tree<T>> getSubtrees() {
return subtrees;
}

public void setRoot(T root) {
this.root = root;
}

public void setSubtrees(List<Tree<T>> subtrees) {
this.subtrees = subtrees;
}

public boolean is_empty() {
return (this.root == null);
}

public int count(int item) {
if (this.is_empty()) {
return 0;
} else {
int num = 0;
if (this.root.equals(item)) {
num += 1;
}
for (Tree<T> x : this.getSubtrees()) {
num += x.count(item);
}
return num;
}

public double average(){
if(this.is_empty()){
return 0.0;
}
count = this.Count();
size = this.length();
return count / size;

}
private int average_helper(){
total = 0;
if(this.root){
total += 1;
}
for(Tree i: this.subtrees){
total += this.average_helper();
}
return total;
}
@Override
public boolean equals(Object o) {
if (this.is_empty() & o.is_empty()) {
return true;
} else if (this.is_empty() || other.is_empty()) {
return false;
} else {
if (this.root != other._root) {
return false;
}
if (this.subtrees.length() != other._subtrees.length()) {
return false;
}
return self._subtrees == other._subtrees;
}
}
}
}

public int length() {
if (this.is_empty()) {
return 0;
} else {
int size = 1;
for (Tree<T> x : this.getSubtrees()) {
size += x.length();
}
return size;
}
}
}
4 changes: 4 additions & 0 deletions test/TreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ public void dummyTest() {
assertTrue(true);
}

public void TestAverage(){
Tree t = new Tree();
assertTrue(t.average() == 0.0);
}
}