// inserts a new node in the binary search tree at the correct position void bstInsert(BinaryTreeNode* root, double value) { if(root->key < value) { if(root->rightChild == NULL) { BinaryTreeNode* newNode = (BinaryTreeNode*) malloc(sizeof(BinaryTreeNode)); newNode->key = value; newNode->leftChild = NULL; newNode->rightChild = NULL; root->rightChild = newNode; } else bstInsert(root->rightChild, value); } else { if(root->leftChild == NULL) { BinaryTreeNode* newNode = (BinaryTreeNode*) malloc(sizeof(BinaryTreeNode)); newNode->key = value; newNode->leftChild = NULL; newNode->rightChild = NULL; root->leftChild = newNode; } else bstInsert(root->leftChild, value); } }
/* This function takes data to form a new node, adds the node to the BST, and returns a pointer to the new BST. * Before and after you execute the function, BST should always satisfy: node->left->data <= node->data < node->right->data * Parameters: * root - the pointer to the original root node of a bst * data - the integer value used to construct the to-be-inserted node * Returns: * the pointer to the root node of the new bst */ Node* bstInsert(Node* root, int data) { if(root == NULL) { return createNode(data); } else { if(data < root->data) { if(root->left == NULL) { root->left = createNode(data); } else { bstInsert(root->left,data); } } else if(data >= root->data) { if(root->right == NULL) { root->right = createNode(data); } else { bstInsert(root->right,data); } } return root; } }
int bstInsert(int v, bst *t) { //Check to see that v has not already been inserted if(t->value == v) return 0; //Assume root node is VALUEDEF if empty if(t->value == VALUEDEF) { t->value = v; return 0; } //v less than value, insert in left subtree if(v < t->value) { //If left subtree doesn't exist, create it if(t->left == NULL) t->left = createNode(); bstInsert(v, t->left); } //v is greater than value, insert in right else { //If right subtree doesn't exist, create it if(t->right == NULL) t->right = createNode(); bstInsert(v, t->right); } return 0; }
bool stAddSymbol(symtable *st, symbol *sym) { block *b; if (listSize(st->blockList) > 1) { b = (block *) listGetFront(st->blockList); if (bstContains(b->symbols, sym)) return false; } b = (block *) listGetBack(st->blockList); //Fail if already in block if (bstContains(b->symbols, sym)) return false; sym_type type = symbolGetType(sym); switch (type) { case symt_var: if (symVarGetType(sym) != pj_text) //Don't allocate space for files symVarSetLocation(sym, b->nextLoc++); break; case symt_array: symArraySetLocation(sym, b->nextLoc); b->nextLoc += symArrayGetRange(sym); break; default: break; } bstInsert(b->symbols, sym); return true; }
/* * Attempts to add the element to the set. If the element is already present in the set, then it is * not added. If the element was added, then the size of the set will be incremented by 1. * * Arguments: * set -- The set to add the element to * element -- The element to add to the set */ void setAdd( Set *set, void *element ) { if( element ) { if( ! bstFind(set->elements, element) ) { bstInsert( set->elements, element ); set->size += 1; } } }
int main() { // Simple example of usage bsTree bst = bstCreate(); bstSetCompare(bst, compare); int key_data[12] = {38, 13, 51, 10, 12, 40, 84, 25, 89, 37, 66, 95}; int *keys[12]; char *data[12]; char data_count = 'a'; for (int i = 0; i < 12; i++) { keys[i] = malloc(sizeof(**keys)); data[i] = malloc(sizeof(**data)); assert(keys[i] != NULL); assert(data[i] != NULL); *(keys[i]) = key_data[i]; *(data[i]) = data_count++; printf("key = %d data = %c\n", *keys[i], *data[i]); bstInsert(bst, keys[i], data[i]); } printf("\ntree: "); bstPrintKeys(bst); printf("\n"); printf("Checking tree data:\n"); for (int i = 0; i < 12; i++) { assert(bstSearch(bst, keys[i]) == data[i]); } printf("Success\n\n"); // remove key of root from tree // comparison to find key in tree is done using compare // function defined above so no need to use original key // pointer int root = 38; printf("Deleting root (key = %d):\n", root); bstDelete(bst, &root); printf("tree: "); bstPrintKeys(bst); assert(bstSearch(bst, &root) == NULL); printf("Success\n\n"); // key of what should be the new root // it is at index 5 of key_data so should have data at // index 5 of data array int new_root = 40; printf("Checking new root (key = %d):\n", new_root); assert(bstSearch(bst, &new_root) == data[5]); printf("Success\n"); bstSetFreeData(bst, free); bstSetFreeKey(bst, free); bstDestroy(bst); return 0; }
/******************************************************************************* * Attempts to insert a Website into a hash table and BST. * * Pre: pList points to a list with a hash table and BST. * pWebsite is initialized. * * Post: The Website has been inserted or destroyed. * * Return: true iff the Website was successfully inserted. ******************************************************************************/ bool listInsert(ListHead *pList, Website *pWebsite) { if(hashInsert(pList, pWebsite)) { bstInsert(pList, pWebsite); return true; } else { websiteFree(pWebsite); return false; } }
void main() { BinaryTreeNode* node; BinaryTreeNode* root = (BinaryTreeNode*) malloc(sizeof(BinaryTreeNode)); root->key = 5.5; root->leftChild = NULL; root->rightChild = NULL; bstInsert(root, 7.7); bstInsert(root, 3.2); bstInsert(root, 4.0); node = bstFind(root, 3.2); if(node != NULL) printf("The value %.2f was found.\n", node->key); else printf("Value 3.2 was not found.\n"); /*node = bstMin(root); if(node != NULL) printf("The minimum value is %f.\n", node->key); node = bstMax(root); if(node != NULL) printf("The maximum value is %f.\n", node->key); root = bstDelete(root, 3.2); node = bstFind(root, 3.2); if(node != NULL) printf("The value %f was found.\n", node->key); else printf("Value 3.2 was not found.\n"; // delete the root root = bstDelete(root, 7.7); printf("Height of binary search tree is %d.\n", bstHeight(root)); bstClear(root); */ bstPrint(root); printf("Height of binary tree is %d.\n", bstHeight(root)); }
void main() { short done=0; int newnum; char ch; char buffer[256]; ITEM* newitem; BST bst; bstNode* node; ITEM tempitem; bstInit(&bst); bstSetCompareFunc(&bst,compare); while(!done) { fprintf(stderr,"(1) Add Item\n(2) Lookup\n(3) Remove Item\n"); ch=getchar(); fflush(stdin); switch(ch) { case '1': newitem=(ITEM*)malloc(sizeof(ITEM)); printf("\nEnter a string\n"); scanf("%s",buffer); newitem->data=strdup(buffer); printf("Enter key\n"); scanf("%d",&newnum); newitem->key=newnum; printf("inserting at %d\n",newnum); bstInsert(newitem,&bst); break; case '2': printf("Enter key\n"); scanf("%d",&newnum); printf("Looking for %d\n",newnum); tempitem.key=newnum; node=bstFind(&tempitem,&bst); if(node != NULL) { newitem=(ITEM*)bstGetInfo(node); printf("found %s\n",newitem->data); } else { printf("Item not found\n"); } break; case '3': printf("Enter key\n"); scanf("%d",&newnum); tempitem.key=newnum; node=bstFind(&tempitem,&bst); if(node != NULL) { newitem=(ITEM*)bstGetInfo(node); printf("removing %s\n",newitem->data); bstDelete(node,&bst); } else { printf("Item not found\n"); } break; default:done=1; } fflush(stdin); } }
int main(int argc, char **argv) { FILE *in = NULL; int num_read, array_size = 0; if(argc != 2){ printf("hw4 <input-file>\n"); return 1; } in = fopen(argv[1], "r"); if(in == NULL){ printf("File can not be opened.\n"); return 2; } // declare the array int array[MAX_SIZE]; // read from the second line to get each element of the array while(!feof(in)){ fscanf(in, "%d", &num_read); if(isValidID(array, array_size, num_read) == 0) { array[array_size] = num_read; array_size++; printf("%d added to array\n", num_read); } else if(isValidID(array, array_size, num_read) == 1) { printf("ERROR - %d bad ID. Not in range [0, 99].\n", num_read); } else if(isValidID(array, array_size, num_read) == 2) { printf("ERROR - %d bad ID. Duplicate.\n", num_read); } } fclose(in); Node *root1 = NULL, *root2 = NULL, *root3 = NULL; int i; // task1: construct a bst from the unsorted array printf("=== task1: construct a bst from the unsorted array ===\n"); for (i = 0; i < array_size; i++) { root1 = bstInsert(root1, array[i]); } displayTree(root1, 0); printf("Height of bst1 is %d\n", getBstHeight(root1)); // task2: sort the array and use the sorted array to construct a bst, each time taking an element in order printf("=== task2: sort the array and use the sorted array to construct a bst ===\n"); bsort1(array, array_size); for (i = 0; i < array_size; i++) { root2 = bstInsert(root2, array[i]); } displayTree(root2, 0); printf("Height of bst2 is %d\n", getBstHeight(root2)); // task3: use the sorted array to construct a balanced bst printf("=== task3: use the sorted array to construct a balanced bst ===\n"); root3 = sortedArrayToBalancedBst(array, 0, array_size-1); displayTree(root3, 0); printf("Height of bst3 is %d\n", getBstHeight(root3)); return 0; }