SE250:lab-6:tsen009

From Marks Wiki
Revision as of 05:20, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (11 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Lab 6 : Binary Search Trees

lol... i do not take responsibility if my code is wrong and if you copy it and get it wrong your self... :) i could use some help/suggestions here... lol..

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));
    }
	    
}


...Fin... out of time... lol...

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... im sure most of us were completely clueless... and 1 lab supervisor cannot help every student (specially when theres limited time...)...