java - toString() method and recursion -
i creating class implements generic set
using binary search tree. implementing tostring()
method displays parenthetic expression, having trouble getting work. example, here basic binary tree:
i want tostring method output binary tree such:
(10 (5 (4 () ()) (6 () ())) (20 (15 () ()) (25 () ())))
where ()
signifies empty tree.
here class tostring method. appreciated!
// allow short name access following classes import csc143.data_structures.*; public class myset<e> implements simpleset<e> { // root of "tree" structures set private btnode root; // current number of elements in set private int numelems; public myset() { root = null; numelems = 0; } /** * add element set. * * @param e element added set. * @return <tt>true</tt> if operation updated contents of set. */ public boolean add(e e) { try { root = addtosubtree(root, (comparable) e); return true; } catch(duplicateadded exc) { // duplicate trying added return false; } } // helper method adds element "e" tree rooted @ r. returns // (possibly new) tree containing "e", or throws duplicateadded exception // if "e" exists in tree. private btnode addtosubtree(btnode r, comparable elem) throws duplicateadded { if(r == null) { numelems++; return new btnode(elem); } int compare = elem.compareto(r.item); // element in tree if(compare == 0) { throw new duplicateadded(); } else if(compare < 0) { r.left = addtosubtree(r.left, elem); } else { // compare > 0 r.right = addtosubtree(r.right, elem); } // element has been added return r; } /** * remove elements set. */ public void clear() { root = null; numelems = 0; } /** * checks existance of specified value within set. * * @param e value sought. * @return <tt>true</tt> if value exists in set. */ public boolean contains(e e) { return subtreecontains(root, (comparable) e); } // helper method returns whether element "elem" in // (sub-)tree root "r". private boolean subtreecontains(btnode r, comparable elem) { if(r == null) { return false; } else { int compare = elem.compareto(r.item); // found element if(compare == 0){ return true; } else if(compare < 0) { return subtreecontains(r.left, elem); } else { // compare > 0 return subtreecontains(r.right, elem); } } } /** * check existance of elements in set. * * @return <tt>true</tt> if there no elements in set. */ public boolean isempty() { return root == null; } /** * return number of elements in set. * * @return number of elements in set. */ public int size() { return numelems; } /** * returns string representation of contents of set. * * @return string representation of set. */ public string tostring() { return subtreetostring(root); } private string subtreetostring(btnode r) { string str = ""; if(r == null) { str += "() "; return str; } str += "(" + r.item; str += subtreetostring(r.left) + subtreetostring(r.right) + ")"; return str; } // inner class creates node compose binary tree structure class btnode<e> { /** * item stored in node. */ public e item; /** * node left of "this" node. */ public btnode left; /** * node right of "this" node. */ public btnode right; /** * constructs btnode object (three parameters). * * @param item item stored in node. * @param left node left of "this" node. * @param right node right of "this" node. */ @suppresswarnings("unchecked") public btnode(object item, btnode left, btnode right) { // bind references this.item = (e) item; this.left = left; this.right = right; } /** * constructs btnode (one parameter). * * @param item stored in node. */ public btnode(object item) { // call 3 parameter constructor this(item, null, null); } } }
i ran few issues when tried running code, assuming issues resolved - following worked me:
private string subtreetostring(btnode r) { string str = ""; if(r == null) { return str; } str += r.item; str += " (" + subtreetostring(r.left) + ") (" + subtreetostring(r.right) + ")"; return str; }
output:
10 (5 (4 () ()) (6 () ())) (20 () (25 () ()))
(there's mistake in example output provided above - closing bracket should added after children of 6
)
Comments
Post a Comment