insertion_status insertNodeRec(int data, BSTHead root) { if (root == NULL) { return NOT_INSERTED; } if (root->data == data) { //printf("This node is already inserted\n"); return NOT_INSERTED; } if (root->data > data) { // insert on the left if (root->left == NULL) { BSTNode newNode = createBSTNode(data); root->left = newNode; return INSERTED; } else { insertNodeRec(data, root->left); } } else { // insert on the right if (root->right == NULL) { BSTNode newNode = createBSTNode(data); root->right = newNode; return INSERTED; } else { insertNodeRec(data, root->right); } } }
int insertInBST(BST *tree, void *data){ Node *node,*parentNode; if(NULL == tree->root){ tree->root = createBSTNode(data); return 1; } parentNode = traverse(tree->root,data,tree->compare); if(parentNode){ if(tree->compare(data,parentNode->data) > 0) parentNode->right = createBSTNode(data); else parentNode->left = createBSTNode(data); } else return 0; return 1; }
insertion_status insertNode(int data, BSTHead root) { /* if root is null, create the new root */ if (root == NULL) { root = createBSTNode(data); } else { BSTNode bstCursor = root; BSTNode newNode = createBSTNode(data); do { if (bstCursor->data > data) { /* add data node to the right */ bstCursor = bstCursor->right; return INSERTED; } else if ( bstCursor->data < data) { /* add data node to the left */ bstCursor = bstCursor->left; return INSERTED; } } while (bstCursor->left != NULL || bstCursor->right != NULL); if (bstCursor->data > data) { /* add to the left of bstCursor */ bstCursor->left = newNode; return INSERTED; } else if (bstCursor->data < data) { /* add to the right of bstCursor */ bstCursor->right = newNode; return INSERTED; } else { //printf("This node is already inserted\n"); return NOT_INSERTED; } } }
void menu() { int option; BSTAddr bstAddr = 0; BSTNodeAddr bstNodeAddr; bstAddr = (BSTAddr) malloc(sizeof(BSTAddr)); (*bstAddr) = 0; do { printf("\n**************************************" "\n* BST *" "\n* 1. Insert Fraction *" "\n* 2. Display *" "\n* 3. Search for Fraction *" "\n* 4. Remove a Node *" "\n* 5. Get and Display Smallest Node *" "\n* 6. Quit *" "\n**************************************"); printf("\nSelect the option (1 throuh 6): "); scanf("%d", &option); switch(option) { case 1: bstNodeAddr = createBSTNode(); insertFractionBSTYourName(bstAddr, bstNodeAddr); break; case 2: if (*bstAddr == 0) { printf("\nThere are no fractions to display\n"); } else { displaymenu(*bstAddr); } break; case 3: if (*bstAddr == 0) { printf("\nThere are no fractions to search for\n"); } else { searchFractionBSTYourName(*bstAddr); } break; case 4: if (*bstAddr == 0) { printf("\nThere are no fractions to remove\n"); } else { removeFractionBSTYourname(bstAddr); } break; case 5: if (*bstAddr == 0) { printf("\nThere are no fractions to get\n"); } else { getSmallestFractionBSTYourName(*bstAddr); } break; case 6: printf("\n Have Fun!\n\n"); free(bstAddr); break; default: printf("\n You should not be in this class!\n\n"); } } while (option != 6); return; }