void insert_bst(binaryTreeNode ** tree,binaryTreeElementT value) { if(!(*tree)) { (*tree) = (binaryTreeNode*)malloc(sizeof(binaryTreeNode)); (*tree)->left = NULL; (*tree)->right = NULL; (*tree)->value = value; }else{ if(value<=(*tree)->value) { insert_bst(&(*tree)->left, value); }else{ insert_bst(&(*tree)->right, value); } } }
/**************************************************************************************************************************************** * FUNCTION NAME : main.c * * DESCRIPTION : main function for implementing bst * * RETURN VALUE : SUCCESS * **************************************************************************************************************************************/ int main(int argc, char *argv[]) { if(NULL == argv[1]) { printf("please enter input file name\n"); exit(FAILURE); } if(NULL == argv[2]) { printf("please enter output file name\n"); exit(FAILURE); } FILE *fp1; //file pointer for input file FILE *fp2; //file pointer for output file file_open(&fp1, argv[1], "r");//opening input file in read mode file_open(&fp2, argv[2], "w");//opening output file in write mode char input[MAX]; //for storing input file names node *root = NULL; //root node of tree int count = 0; //name count int duplicates = 0; while(1) { memset(input, 0, MAX * sizeof(char));//initializing input /*reading from the file*/ fgets(input, (MAX-1)*sizeof(char), fp1); if(feof(fp1)) { break; } insert_bst(&root, input, &duplicates); count++; } printf("\n\n**********File contents in level order***********\n\n"); fprintf(fp2, "Total names present in the input file is %d\n", count); fprintf(fp2, "Total number of duplicate names present in the input file is %d\n\n", duplicates); fprintf(fp2, "\n\n**********File contents in level order***********\n\n"); insert_file(&root, &fp2); file_close(&fp1); //closing input file file_close(&fp2); //closing output file free_bst(&root); return SUCCESS; }
/* Recursively insets an integer into correct place * in binary search tree * O(h) best case O(log n) worst case O(n) for n nodes * Returns a pointer to the inserted node */ struct b_node* insert_bst(struct b_node* node,struct b_node* parent, int data) { //If both node and parent are null then this is a brand new tree if(node == NULL) { struct b_node* new_node = init_node(data); if (new_node == NULL) { return new_node; } new_node->parent = parent; return new_node; } if(node->value > data) { //This leess than current node value, so it must be a left child/descendant return insert_bst(node->left, node, data); } if(node->value < data){ //This is greater than value of current node - must be right child/descendant return insert_bst(node->right, node, data); } //Clearly, data must now be equal to node->value so this depends on how //you define insertions of duplicates, I'm going to just make it return //the existing pointer return node; }
int main(int argc, char **argv) { Node *root = NULL; Node *dl, *tl; int v; for(int i = 0; i < 10; i++) { v = rand() % 100; printf("%d ", v); insert_bst(&root, v); } printf("\n"); print_bst(root); printf("\n"); bst2dll(root, &dl, &tl); printf("%p %p\n", dl, tl); print_list(dl); return 0; };
int main(void) { char buf[BUFSIZE]; int operator, operand; bool terminal_flag = false; node_pointer_t root; while(!terminal_flag) { operator = -1; operand = -1; fgets(buf, BUFSIZE-2, stdin); sscanf(buf, "%d %d", &operator, &operand); switch(operator) { case 0: if(!insert_bst(&root, operand)) { fprintf(stderr, " [!] insertion failed, end program\n"); exit(EXIT_FAILURE); } break; case 1: if(!delete_bst(root, operand)) { fprintf(stderr, " [!] deletion failed, end program\n"); exit(EXIT_FAILURE); } break; case 2: print_preorder_bst(root); printf("\n"); print_inorder_bst(root); printf("\n"); print_postorder_bst(root); printf("\n"); break; case 3: terminal_flag = true; continue; default: fprintf(stderr, " ERROR: wrong operator\n"); continue; } } exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int ch=0,num=0; char choice; struct node* head; head=NULL; //Initialization of the head pointer //struct snode* stop; //stop=NULL; do { printf("\nPlease Enter you choice:"); printf("\n1:Insert"); printf("\n2:Recursive Display"); printf("\n3:Non Recursive Display"); scanf("%d",&ch); printf("\nYour choice is %d",ch); switch(ch) { case 1: printf("\nEnter the number to insert:"); scanf("%d",&num); insert_bst(&head,num); break; case 2: display_bst(head); break; case 3: nr_display_bst(head);//printf("\nEnter the number to delete:"); break; } getchar(); //Extra getchar() to bypass newline character in input buffer printf("\nDo you want to continue (y/n)"); scanf("%c",&choice); } while(choice=='y'||choice=='Y'); }
int main() { int i, key; link root = NULL; srand(time(NULL)); for (i = 0; i < N; i++) root = insert_bst(root, rand() % RANGE); printf("\t\\tree"); print_bst(root); printf("\n\n"); while (root) { key = rand() % RANGE; if (search_bst(root, key)) { printf("delete %d in tree\n", key); root = delete_bst(root, key); printf("\t\\tree"); print_bst(root); printf("\n\n"); } } return 0; }
struct b_node* insert(struct b_node* root, int data) { if (root == NULL) { return insert_bst(root, NULL, data); } return insert_bst(root, root->parent, data); }