From ae6a6b9af841c306debe406a9d8003ab7709687c Mon Sep 17 00:00:00 2001 From: cho4 Date: Mon, 18 Sep 2023 14:58:40 -0400 Subject: [PATCH 1/2] Implemented count(), toString() --- src/Tree.java | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/Tree.java b/src/Tree.java index 458a7c7..46710a1 100644 --- a/src/Tree.java +++ b/src/Tree.java @@ -1,2 +1,45 @@ public class Tree { + + public int count(Object item) { + + if (this.is_empty()) { + return 0; + } else { + + int num = 0; + + if (this.root == item) { + num += 1; + } + + for (Tree subtree : this.subtrees) { + num += subtree.count(item); + } + + return num; + } + } + + public String toString() { + return this.str_indented(); + } + + private String str_indented(int depth) { + + if (this.is_empty()) { + return ""; + } else { + String s = " ".repeat(depth) + toString(this.root) + "\n"; + for (Tree subtree : this.subtrees) { + s += subtree.str_indented(depth + 1); + } + + return s; + + } + } + + private String str_indented() { + return this.str_indented(0); + } } From d13fa90db09c30190e0256998c0034bd87936452 Mon Sep 17 00:00:00 2001 From: Varun Sahni Date: Mon, 18 Sep 2023 15:45:30 -0400 Subject: [PATCH 2/2] change first few funcs Tree class --- src/Tree.java | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/src/Tree.java b/src/Tree.java index 458a7c7..fcec4d2 100644 --- a/src/Tree.java +++ b/src/Tree.java @@ -1,2 +1,70 @@ +import java.util.ArrayList; +import java.util.Optional; + public class Tree { -} + + /** + * The person's name (family name last). + */ + private Optional root; + /** + * The person's UTORid + */ + ArrayList subtrees; + + /** + * Initialize this Person named name with UTORid utorid. + * + * @param root the person's name (family name last) + * @param subtrees the person's UTORid + */ + Tree (Optional root, ArrayList subtrees) { + /** Initialize a new Tree with the given root value and subtrees. + + If is None, the tree is empty. + Precondition: if is None, then is empty. + */ + + this.root = root; + if (subtrees == null) { + this.subtrees = new ArrayList(); + + } else { + this.subtrees = new ArrayList<>(this.subtrees); + + } + + + } + + + Tree () { + /** Initialize a new Tree with the given root value and subtrees. + + If is None, the tree is empty. + Precondition: if is None, then is empty. + */ + + this.root = root; + this.subtrees = new ArrayList(); + + } + + boolean isEmpty() { + return this.root == null; + } + + int size() { + if (this.isEmpty()) { + return 0; + } else { + int size = 1; + for (Tree subtree : this.subtrees) { + size += subtree.size(); + } + return size; + } + + + } +} \ No newline at end of file