struct treeNode * insertItem(struct treeNode * node, int item) { if(node==0) //insert as the root as the tree is empty { struct treeNode * newNode = makeTreeNode(item); root = newNode; return newNode; } if(node->item==item) return 0; //already an item exists, so return NULL if(item<node->item && node->left==0) //insert as the left child { struct treeNode * newNode = makeTreeNode(item); node->left = newNode; return newNode; } if(item>node->item && node->right==0) //insert as the right child { struct treeNode * newNode = makeTreeNode(item); node->right = newNode; return newNode; } if(item<node->item) return insertItem(node->left, item); //insert at left sub-tree else return insertItem(node->right, item); //insert at right sub-tree }
void biTreeInsert(BiTree *tree, char *key, int keyLength, int fileFlag){ BiTreeNode *tempTreeNode = NULL; BiTreeNode *treeNodePtr = NULL; if(NULL == tree->root){ tempTreeNode = makeTreeNode(key, keyLength); tree->root = tempTreeNode; tree->size++; return; } treeNodePtr = searchBiTree(tree->root, key); if(strcmp(key, getTreeNodeWord(treeNodePtr)) == 0){ if(fileFlag == 0) treeNodeStartWordCountPlusOne(treeNodePtr); else treeNodeEndWordCountPlusOne(treeNodePtr); }else{ tempTreeNode = makeTreeNode(key, keyLength); if(strcmp(key, getTreeNodeWord(treeNodePtr)) < 0) treeNodePtr->left = tempTreeNode; else treeNodePtr->right = tempTreeNode; tree->size++; } }
object treeInsert(Thread* t, Zone* zone, object tree, intptr_t key, object value, object sentinal, intptr_t (*compare)(Thread* t, intptr_t key, object b)) { PROTECT(t, tree); PROTECT(t, sentinal); object node = makeTreeNode(t, value, sentinal, sentinal); TreeContext c(t, zone); treeFind(t, &c, tree, key, node, sentinal, compare); expect(t, c.fresh); return treeAdd(t, &c); }