I REALLY NEED HELP ON THIS ASSIGNMENT, CAN SOMEONE HELP ME. THANK YOU. FOR MY CL
ID: 3768457 • Letter: I
Question
I REALLY NEED HELP ON THIS ASSIGNMENT, CAN SOMEONE HELP ME. THANK YOU.
FOR MY CLASS Machine Architecture and Organization. I AM gradful.
Implement a sorted binary tree class in Jack.
Specifically, a class named SortedTreeNode (in a file named
SortedTreeNode.jack) that represents one node in a sorted binary
tree.
The basic sorting rule is that, for any SortedTreeNode, the value in
the left node and every sub-node of the left node must be less than
the value in the current node, and the value in the right node and
every sub-node of the right node must be greater than the value in the
current node, and this rule must apply to the left node, and to every
node under it, and to the right node, and to every node under it.
Each node must have the following fields:
Name Type
---- ----
value int
left SortedTreeNode
right SortedTreeNode
Each node must have the following constructors / functions / metods:
Constructor new, with arguments
Type Descr
---- -----
int Value to store in new node
SortedTreeNode Left sub-node, or null
SortedTreeNode Right sub-node, or null
Method add, with arguments
Type Descr
---- -----
int Value to add to tree
The add method must add a new node to the tree rooted at the node
whose add method was called, which contains the specified value.
After the addition is complete, the basic sorting rule must be true.
Method dispose, with no arguments
When called on a particular node, dispose() must free the memory used
by that node and all sub-nodes. (Memory is freed by calling the
Memory.deAlloc() and passing the object.)
method orderedDump, with no arguments.
When called on a particular node, orderedDump() must print out the
value in that node and the values in all sub-nodes of that node in
order. A recursive implentation is the easiest way to do this.
I have provided the file SortedTreeNode.jack. It contains the
declarations of all of these fields and methods, but not the body of
the code. Your job is to fill in the code to actually implement the
methods.
I have provided an additional method, debugDump(), in the sample
file. It prints out the sub-tree starting at the node it's called on,
with information on where in memory each object is stored. I found
this useful in debugging my implementation, maybe you will too.
I have also provided a Main.jack file, which creates a tree by calling
the constructor and the add function, and prints out the result using
the orderedDump() function.
class Main {
function void main() {
var SortedTreeNode t;
let t = SortedTreeNode.new(1000,null,null);
do t.add(3721);
do t.add(4821);
do t.add(5921);
do t.add(7021);
do t.add(2100);
// do Output.printString("added 2100"); do Output.println();
// do t.orderedDump();
do t.add(1300);
do t.add(871);
do t.add(1);
do t.add(2);
do t.add(3);
do t.add(5000);
do t.add(6000);
do t.add(7000);
do t.orderedDump();
return;
}
}
// Rule is that all keys in the left branch are less than the key of
// the current node, and all nodes in the right branch are greater.
// It's an error to add a key for the second time.
class SortedTreeNode {
field int value;
field SortedTreeNode left, right;
// lin and rin may be null.
constructor SortedTreeNode new(int val, SortedTreeNode lin, SortedTreeNode rin) {
// Put constructor code here
return this;
}
// Add new node for given value in the sub-tree rooted at the current node.
method void add(int val) {
// Put add code here
}
// Dispose the subtree under the current node.
method void dispose () {
// Put dispose code here
}
// Print all the values in the tree *in order*, and then give the node,
// left child address (or "empty"), and right child address (or "empty").
method void orderedDump () {
// put orderedDump code here
}
// Dump the tree from here down.
// Mostly for debugging.
method void debugDump () {
var Array p;
let p = this;
do Output.printString("Node ");
do Output.printInt(p);
do Output.printString(" val ");
do Output.printInt(value);
do Output.printString(" left ");
if (left = null) {
do Output.printString("empty");
} else {
do Output.printInt(p[1]);
}
do Output.printString(" right ");
if (right = null) {
do Output.printString("empty");
} else {
do Output.printInt(p[2]);
}
do Output.println();
if (~ (left = null)) {
do left.orderedDump();
}
if (~( right = null)) {
do right.orderedDump();
}
return;
}
}
Explanation / Answer
program :
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.