I'm trying to run this code in cygwin 1.7 but i get segmentation fault when I choose to search. I tried this in cygwin 1.5 before but it works. does anybody know the reason why it won't work in cygwin 1.7?

here's the code

/****************
Rachel Perono
running BST :)
****************/


#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 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)
		return;
	else{
		view(root -> right, tabs+1);
		
		for(i = 0; i < tabs; i++) printf("\t");
			printf("%2i\n", root -> value);
			view(root -> left, tabs + 1);
	}
		
		
}

node* search2(node **root, int val){
      
	if((*root) -> value == val){
            printf("%d", (*root) -> value);
			printf("found!");
			return *root;
 	}else{
		if((*root)-> left != NULL && (*root) -> value > val){
			search2(&(*root) -> left, val);
		}else if((*root)-> right != NULL &&(*root)->value < val){
			search2(&(*root) -> right, val);
		}else{//pasok dito pag wala sa list.
			printf("not found!\n");
			return NULL;
		}
		
	}
		
	

}

main(){

	node *root = NULL;
	node *temp = NULL;
	int i, a[N];
	int val;
	int look;
	int searched = 0;
	int delete_this;
	int number;
	
	int choice = 0;
	
	while(choice!=7){
         
		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] 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);
					}
					
					//printf("root sa umpisa ay nasa : %d\n", root -> value);
					
					break;
					
			case 1:
			
					//view
					view(root, 0);
					break;
					
			case 2:
	
					printf("enter number to search [must be between 1 - 10 ].\n");
					scanf("%d", &look);
	                root = search2(&root,look);
					view(root, 0);
					
				    printf("root searched: %d\n", root -> value );
					
					break;
				
			case 3:
                  return;
		
			}
	} 
}

Not every program runs great on emulators. Try running it in a unix system.

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.