The following question uses Java and includes a template at the bottom. Here are the instructions:
Insert into a Binary Tree
Template:
Node ?? = ??? ; // start the
search/insert here
while ( ) {
if ( ) {
}
else {
}
}
// Java code to Insert the key into the Binary Tree
Node thecurrent = bintree.root; // initialize
thecurrent to the root of the binary tree
// loop that continues until we get a node with value -1
while(thecurrent.value != -1) {
// if value of thecurrent >= key, insert the key in
the left subtree
if(thecurrent.value >= key){
thecurrent = thecurrent.left;
}
else // value of thecurrent < key , insert key in
the right subtree
{
thecurrent =
thecurrent.right;
}
}
// set the value of thecurrent to key
thecurrent.value = key;
Get Answers For Free
Most questions answered within 1 hours.