please help me find the error in this code. whenever i try to get the successor, the program gets the status access violation error. i think it's the parameter. i try using the minimum function outside the delete and it works. but when i try it inside the delete i get the error. why?

here's the code for the successor:

node* successor(node **root){

	if((*root)->right == NULL && (*root)->left == NULL)return NULL;
	else{
		if((*root) -> right != NULL){
			printf("root %d", temp -> value);
			printf("pasok sa ryt\n");
			return minimum(&(*root) -> right);
			
		}
		else return maximum(&((*root)-> left));
	}
}

and here's the delete:

void delete(node **root, int val){
     
	node* temp;
	node* suc;
	
    printf("entered delete?");
    
    temp = search2(&(*root),val);
	printf("\n\n");
	view(temp,0);
     
	 //case 3. two children
		if(temp -> left != NULL && temp -> right != NULL){
				printf("pasok sa case 3");
				suc = successor(&temp);
				//printf("successor: %d", suc -> value);
				temp -> value = suc -> value;
				printf("which means lumabas sa successor?");
			
				if(suc -> parent-> right == suc) suc ->parent -> right = NULL;
				else suc -> parent -> left = NULL;
			free (successor);
		}//end of case 3.
		
		else{
            
		//case 1. no child
			if(temp -> left ==NULL && temp -> right == NULL){
                   printf("dito papasok dapat!\n");
			
				//if left child ung node.
				if(temp -> parent -> right == NULL){
					temp -> parent -> left = NULL;
					free (temp);
				//if right child.
				}else if(temp -> parent -> left == NULL){
					temp -> parent -> right = NULL;
					free (temp);			
				}
			}//end of case 1
			
		//case 2. one child
			else if(temp -> left != NULL || temp -> right != NULL){
			
				if(temp -> left == NULL){ //no left child, so update right child and pointers. right child ang meron siya.
					if(temp -> parent -> left == temp){//left child ung root [ ung dapat idelete]
						temp -> parent -> left = temp -> right;
						free (temp);
					}else{//right child ung root na dapat idelete;
						temp -> parent -> right = temp -> right;
						free (temp);
					}
				}else if(temp -> right == NULL){//no right child.left child ang meron siya
					if(temp -> parent -> left == temp){//left child ung root [ ung dapat idelete]
						temp -> parent -> left = temp -> left;
						free (temp);
					}else{//right child ung root na dapat idelete;
						temp -> parent -> right = temp -> left;
						free (temp);
					}
				}
			}//end of case 2.
	}//else
}

please help me find the error in this code. whenever i try to get the successor, the program gets the status access violation error. i think it's the parameter.

You think its the parameter...How are passing it to the function and what are some of the values your passing..

in the successor, I need to pass the right child of the node I need to delete.

You have so many things like below that I don't really know what your trying to accomplish here..

free (successor);//what's this do? Free the function??


temp = search2(&(*root),val);//what's this function search2?
printf("\n\n");
view(temp,0);
 
if(temp -> left != NULL && temp -> right != NULL)
{
	printf("pasok sa case 3");
	suc = successor(&temp);

Edited 6 Years Ago by gerard4143: n/a

temp = search2(&(*root),val will search for the node, and return its address so temp will point to it.

then using the successor function it will search for the successor of temp // suc = successor(&temp); the suc will contain the address of the successor of temp.

free(successor) . I just realized, that I should have used free(suc) instead of free(successor). oops.

This article has been dead for over six months. Start a new discussion instead.