Access Millions of academic & study documents

ADT Table implemented as Binary Search Tree in JAVA Need help with

Content type
User Generated
Showing Page:
1/15
ADT Table implemented as Binary Search Tree in JAVA.
Need help with the following methods:
Search -- Given a key, return that key\'s data item
getSize -- Return # of nodes
getHeight -- Return the tree\'s height
getAvgLevel -- Return the avg level of the nodes in the
tree
showTree -- Display the elements of the tree (with the
appearance of a \'tree\')
toString -- Display all the elements in the the table in
accordance to key\'s order (ascending order)
Define a Table class for the whole tree and a Node class
for each node in the tree. Must use recursion for
operations. Use an Comparable interface. Here is my
interface and node class:
interface Comparable
{
public int compareTo ( Comparable other );
// return -1 if les than other, 0 if equal, and +1 if greater
than
public String toStringKey();
//return a short string to summarize the items, use for
showTree
}
class Node
{
public Comparable data;
public Node left;
public Node right;
}

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/15
Solution
public class *ST<Key extends Comparable<Key>, Value>
*ST() // create a symbol table
void put(Key key, Value v) // put key-value pair into the
table
Value get(Key key) // return value paired with key
// or null if no such value
boolean contains(Key key)
public boolean contains(Key key) {
return get(key) != null;
}
private class Node {
Key key;
Value val;
Node left, right;
Node(Key key, Value val) {
this.key = key;
this.val = val;
}
}

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/15

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 15 pages?
Access Now
Unformatted Attachment Preview
ADT Table implemented as Binary Search Tree in JAVA. Need help with the following methods: Search -- Given a key, return that key\'s data item getSize -- Return # of nodes getHeight -- Return the tree\'s height getAvgLevel -- Return the avg level of the nodes in the tree showTree -- Display the elements of the tree (with the appearance of a \'tree\') toString -- Display all the elements in the the table in accordance to key\'s order (ascending order) Define a Table class for the whole tree and a Node class for each node in the tree. Must use recursion for operations. Use an Comparable interface. Here is my interface and node class: interface Comparable { public int compareTo ( Comparable other ); // return -1 if les than other, 0 if equal, and +1 if greater than public String toStringKey(); //return a short string to summarize the items, use for showTree } class Node { public Comparable data; public Node left; public Node right; } Solution public class *ST *ST() // create a symbol table void put(Key key, Value v) // put key -value pair into the table Value get(Key key) // return value paired with key // or null if no such value boolean contains(Key key) public boolean contains(Key key) { return get(key) != null; } private class Node { Key key; Value val; Node left, right; Node(Key key, Value val) { this.key = key; this.val = val; } } private static void traverse(Node x) { if (x == null) return; traverse(x.left); StdOut.println(x.key + ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4