static int tldlist_insert(char *tldStr, TLDNode *node, TLDList *tld) { int comparison = strcmp(tldStr, node->tld); if (comparison == 0) { node->count++; free(tldStr); return 2; //tld already is in the tree } else if (comparison < 0 && node->left == NULL) { node->left = tldnode_create(node, tldStr, 1); if (node->left == NULL) return 0; updateHeight(node->left); restoreBalance(node->left, tld); return 1;//TODO height } else if (comparison < 0 && node->left != NULL) return tldlist_insert(tldStr, node->left, tld); else if (comparison > 0 && node->right == NULL) { node->right = tldnode_create(node, tldStr, 1); if (node->right == NULL) return 0; updateHeight(node->right); restoreBalance(node->right, tld); return 1; } else if (comparison > 0 && node->right != NULL) return tldlist_insert(tldStr, node->right, tld); return 0; }
extern void LSQ_DeleteElement(LSQ_HandleT handle, LSQ_IntegerIndexT key) { AVLTreeT *tree = (AVLTreeT *)handle; TreeNodeT *node = NULL, *parent = NULL; IteratorT * iter = (IteratorT *)LSQ_GetElementByIndex(handle, key); int new_key; if (!LSQ_IsIteratorDereferencable(iter)) return; parent = iter->node->parent; if (iter->node->l_child == NULL && iter->node->r_child == NULL) replaceNode(tree, iter->node, NULL); else if (iter->node->l_child != NULL && iter->node->r_child != NULL) { node = successor(iter->node); new_key = node->key; iter->node->value = node->value; LSQ_DeleteElement(handle, node->key); iter->node->key = new_key; return; } else if (iter->node->l_child != NULL) replaceNode(tree, iter->node, iter->node->l_child); else if (iter->node->r_child != NULL) replaceNode(tree, iter->node, iter->node->r_child); free(iter->node); tree->size--; restoreBalance(tree, parent, BT_AFTER_DELETE); }
// insert new node into tree void AVLTreeIndex::insert(AVLTreeNode* newNode) { AVLTreeNode *temp, *prev, *parent; temp = root; // current node prev = NULL; // previous node traversed parent = NULL; // parent node to current node // case: tree is empty if (root == NULL) { root = newNode; return; } // case: tree is not empty, traverse for location to insert till temp reaches end while (temp != NULL) { prev = temp; if (temp->balance != balanced) parent = temp; if (temp->key.compare(newNode->key) < 0) temp = temp->right; else if (temp->key.compare(newNode->key) > 0) temp = temp->left; else if (temp->key.compare(newNode->key) == 0) { //std::cout << "node already exists" << std::endl; break; } } ; newNode->parent = prev; // set parent if (newNode->key.compare(prev->key) < 0) prev->left = newNode; // insert left else prev->right = newNode; // insert right treeSize++; restoreBalance(prev, newNode); // restores balance of AVL tree }
void AvlTree<T, M>::rebalanceAfterSpliceOut(Node* k, int delta) { while (true) { k->balance -= delta; if ((k->balance == 2) || (k->balance == -2)) k = restoreBalance(k); if ((k->balance != 0) || (!k->parent)) break; delta = 2 * (k->parent->left == k) - 1; k = k->parent; } }
extern void LSQ_InsertElement(LSQ_HandleT handle, LSQ_IntegerIndexT key, LSQ_BaseTypeT value) { AVLTreeT *tree = (AVLTreeT *)handle; TreeNodeT *insert_node = NULL, *node = NULL, *parent = NULL; if (IS_HANDLE_INVALID(handle)) return; node = tree->root; while (node != NULL) { parent = node; if (key > node->key) node = node->r_child; else if (key < node->key) node = node->l_child; else { node->value = value; return; } } insert_node = (TreeNodeT *)malloc(sizeof(TreeNodeT)); if (insert_node == NULL) return; insert_node->key = key; insert_node->value = value; insert_node->r_child = NULL; insert_node->l_child = NULL; tree->size++; insert_node->parent = parent; if (parent == NULL) { tree->root = insert_node; return; } if (key > parent->key) parent->r_child = insert_node; else parent->l_child = insert_node; restoreBalance(tree, parent, BT_AFTER_INSERT); }