SE250:lab-6:apra102

From Marks Wiki
Jump to navigation Jump to search

Lab-6 On Binary Search Trees:

Task 1

To find the height of the tree i got the code as follows:

int main(void){
int height_Of_Tree;

 	Node* node = makeRandomTree(7);
	height_Of_Tree = height(node);
	printf("height: %d", height_Of_Tree);

I compiled the same code with different samples of tree. And noted the values of height for different samples.

Task 2&3

As we know the minimum value of the tree would be the extreme left value of the tree and the maximum is the extreme right value of the tree. I wrote the following code to get the minimum and maximum values. First I checked whether the node pointing is empty or not if not continue to go and when it reaches the empty node then return the node which is before the empty node.


Node* minimum( Node* node ) {
	while(node->left != empty){
		node = node->left;
	}
	return node;
}
Node* maximum( Node* node ) {
	while(node->right != empty){
		node = node->right;
	}
	return node;
}

Task 3

This is to write a look up function. I tried in this this way, the code to find the key is:


while ((node != empty)||(node == empty)) {
	if (key == node->key){
		return node;}
	else if (key > node->key){
		node = node->right;}
	else{
		node = node->left;
	}
}

I came up to here in this time. But I find this lab quit interesting. I m going to colmplete this lab as this is a good exercise on BST and on pointers.