i am making a binary search tree, but when i use some functions and return their values, i get an error when i have to print those values. i don't understand why. please help.

#include<stdio.h>
#include<stdlib.h>
#define N 10

typedef struct nodetag{
       
       int value;
       struct nodetag *left;
       struct nodetag *right;
       struct nodetag *parent;
}node;

void swap(int* a, int* b){
 
 	int temp;
 	
 	temp = *a;
 	*a = *b;
 	*b = temp;
} 

void swap2(node** a, node** b){
 
 	node *temp;
 	
 	temp = *a;
 	*a = *b;
 	*b = temp;
} 

void insert(node **root, node* temp){
	

		if(*root == NULL){
                    
                 *root = temp;
		}
		else{
             temp -> parent = *root;
             printf("parent: %d temp : %d \n", temp-> parent->value,temp -> value);
             
              if((*root) -> value > temp -> value){
             //printf("go to left\n");
                      insert((&(*root) -> left ), temp);
		
              }else if((*root) -> value < temp->value){
               //printf("go to right\n");
			         insert((&(*root) -> right), temp);
              }
              }
              
}

void insert_node(node** root, int val){
	
		node* temp;
		
		temp = (node*) malloc (sizeof(node));
		temp -> value = val;
		temp -> left = temp -> right = temp -> parent = NULL;
		
		insert(&(*root), temp);
}

void view(node *root, int tabs){

	int i;
	
	if(root != NULL){
		view(root -> right, tabs+1);
		
		for(i = 0; i < tabs; i++) printf("\t");
			printf("%2i\n", root -> value);
			view(root -> left, tabs + 1);
		}
}
int search(node *root, int val){
    
    node *temp = root;
    printf("temp -> value = %d", temp -> value);
    
    if(temp == NULL){
            printf("swak");
            return 0;
}
    if(temp != NULL){
            
            if(temp -> value == val){
            printf("found!");
                    return 1;        
            }else{
                  if(temp -> value > val){
                          search(temp -> left, val);        
                  }else{
                        search(temp->right, val);
                  }
            }
	}
            
   
}

node* search2(node **root, int val){
      
      node* temp = *root;
      if(temp -> value == val){
	        printf("found!\n");
	        printf("ang root nfaun ay nasa : %d \n", temp -> value);
	        printf("dapat tigil na.");
			return temp;
      }
	
    if(temp -> left == NULL && temp -> right == NULL){
            return NULL;        
    }else{
          if(temp -> value > val){
                  search2(&(temp -> left), val);        
          }else{
                  search2(&(temp -> right), val);
          }    
    }
}
node* minimum (node** root){
      
      
      printf("entered ryt?");
      
      if((*root) -> left != NULL){
      while((*root) ->left != NULL){
                  *root = (*root) -> left;
      }
       printf("root: %d", (*root) -> value);
      return *root;
      }else{
            return NULL;
      }
       
}

node* maximum(node** root){
      
                     printf("pasok sa max??");
      
      node* temp = (*root) -> parent;
      printf("temp = %d", temp -> value);
      printf("okay lang??");
      
      if(temp -> parent == temp -> parent->left){
              printf("aus na to. %d", temp ->parent);
              return(temp);
      }else{
            maximum(&(temp -> parent));      
      }
      
}

node* successor(node **root){

	if((*root) -> right != NULL){
               minimum(&(*root));           
    }else{
          
    }
}	


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


main(){

	node *root = NULL;
	int i, a[N];
	int val;
	int look;
	int searched = 0;
	node *temp =NULL;
	node* min =NULL;
	int delete_this;
	node* max=NULL;
	node* search = NULL;
	
	int choice = 0;
	
	while(choice!=5){
         
		printf("\nmenu.\n");
        printf("[0] implement (insert input, randomize, make the bst.)\n");
		printf("[1] view\n");
		printf("[2] search a node\n");
		printf("[3] delete one node\n");
		printf("[4] exit program\n");
        scanf("%d", &choice);


        switch(choice){
                              
            case 0:	
					
					for(i = 0; i < N; i++){
						a[i] = i + 1;
						printf("%d", a[i]);
					}
	
					printf("\n\n");
	
					//randomize! 
	
					srand(time(NULL));
					for(i = 0; i < N; i++){
						swap( &a[i], &a[rand()%N]);
						printf("%d", a[i]);
					}
	
					for(i = 0; i < N; i++){
						//insert a[i];
						val = a[i];
						insert_node(&root, val);
					}
					
					break;
					
			case 1:
			
					//view
					view(root, 0);
					break;
					
			case 2:
	
					//for searching....
					printf("enter number to search [must be between 1 - 10 ].\n");
					scanf("%d", &look);
					//searched = search(root,look);
    
					//printf("searched : %d ", searched);
					//end of searching...
					
					printf("root: %d", root -> value);
				    //temp = search2(&root,look);
				    //bakit ayaw mprint???
					//printf("temp: %d", temp -> value );
					//printf("root: %d \n", root -> value);
				    //temp = successor(&temp);
					//printf("successor: %d", temp -> value);
					
					break;
				
			case 3:
                 
                   printf("enter value to delete! : \n");
                   scanf("%d", &delete_this);
					//delete(&root, delete_this);
  
					//search2(&root, look);
					// printf("root : %d\n", root ->value);
    
					//successor(&root);
					//printf("%d", *root);
	
	/*
	for(i = 0; i < N;i++){
		//delete a[i]
		val = a[i];
		//search(&root,val);
		//view;
	}*/
					break;
	
			case 4: 
                 printf("enter number to search [must be between 1 - 10 ].\n");
			   scanf("%d", &look);
                 search = search2(&root, look);
                 //root = root -> right;
                 
              //   min = minimum(&root);
                // printf("get max.\n");
                printf("roooot: %d", root -> value);
                printf("root parent: %d", root -> parent -> value);
                 //max = maximum(&root);
                 
                 printf("lumabas pa ba dito?");
                 
                // printf("minimum: %d", min -> value);
			break;

          case 5:return;
		
			}
	} 
}

Recommended Answers

All 2 Replies

You can't just simply swap nodes (see lines 24-31 of the code you posted) of a linked list because all the pointers will be destroyed (invalidated). A better way to swap nodes is to swap the data, leaving all the pointers in the node structure unchanged.

thanks for that, I alaready changed it to int, but still the search, search2, minimum, and maximum function won't work. I think there's something wrong with the node that I return. but I can't find it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.