SE250:lab-6:hbar055

From Marks Wiki
Jump to navigation Jump to search

Tasks

minimum

the following code will "hopefully" find the minimum value of the binary search tree... unsure if its correcte (untested), but its the basic idea...

Node* minimum( Node* node ) {
   if (node == empty){

return empty;

   }
   while ( (*node)->left != empty){

node = &(*node)->left;

   }	
   return node;
}

maximum

the following code will "hopefully" find the maximum value of the binary search tree... unsure if its correcte (untested), but its the basic idea...

Node* maximum( Node* node ) {
   if (node == empty){

return empty;

   }
   while ( (*node)->right != empty){

node = &(*node)->right;

   }	
   return node;
}


lookup

the following code will "hopefully" find the node of the given key from the binary search tree... unsure if its correct (untested), but its the basic idea...

Node* lookup( int key, Node* node ) {
   if (node == empty){

return empty;

   }
   if (key < (*node)->key){

return (lookup(key, (*node)->left));

   }
   else if (key > (*node)->key){

return (lookup(key, (*node)->right));

   }
    else if (key == (*node)->key){

return node;

   }
//now this leaves onething out... what to do if the key isnt in the tree.. :S (would wnd up with                                  
an endless loop...
}

next_prev

Im assuming this means the next value/node on the binary tree (from, the given one)... code is basically as follows, but not tested, not fully functioning... hehe... question wasent really clear.


Node* next_pre( Node* node ) {
   if (node == empty){

return empty;

   }
   if ( (*node)->right == empty){

return node;

   }
   else{

node = (*node)->right; return (minimum(node));

   }
}

prev_pre

Im assuming this means the previous value/node on the binary tree (from, the given one)... code is basically as follows, but not tested, not fully functioning... hehe... question wasent really clear...

Node* prev_pre( Node* node ) {
   if (node == empty){

return empty;

   }
   if ( (*node)->left == empty){

return node;

   }
   else{

node = (*node)->left; return (maximum(node));

   }
}


Conclusion

Would be nice if we knew what we were doing. we knew the functionality of the binary trees, but we were never thought HOW to actually make it function. would be nice if we had a lecture on it before the lab next time.