void insertRBNode(ppRBNode root, int data) { pRBNode z = (pRBNode)malloc(sizeof(RBNode)); if(z == NULL) { perror("malloc"); exit(EXIT_FAILURE); } z->val = data; z->left = z->right = z->parent = NULL; if(*root == NULL) { z->color = 'B'; (*root) = z; } else { pRBNode y = NULL, x = (*root); while(x != NULL) { y = x; if(z->val < x->val) x = x->left; else x = x->right; } z->parent = y; if(y == NULL) (*root) = z; else if(z->val < y->val) y->left = z; else y->right = z; z->color = 'R'; insertFixup(root, z); } }
// RBTree mutators void RBTree :: RBinsert( NodePtr z ) { NodePtr y = nil ; NodePtr x = root ; while (x!=nil){ y=x; if (z -> key < x->key){ x = x->left; } else{ x = x->right; } } z -> p = y; if(y == nil){ root = z; } else if ( z -> key < y->key){ y->left = z; } else{ y->right = z; } z->left = nil; z->right = nil; z->color = 'R'; insertFixup( z ) ; }
void RBTree::insert(int key) { RBNode *z = new RBNode(key); RBNode *x = root; RBNode *y = NIL; while (x != NIL) { y = x; if (key < x->key) x = x->left; else x = x->right; } z->parent = y; if (y == NIL) root = z; else if (key < y->key) y->left = z; else y->right = z; z->left = NIL; z->right = NIL; z->color = RED; insertFixup(z); }
//红黑树插入操作 void RBTree::TreeInsert(Node& node) { Node* z = new Node(node); Node* y = NIL; Node* x = Treepoint; while (x != NIL) { y = x; if (z->getKey() < x->getKey()) x = x->getLeft(); else x = x->getRight(); } z->setParent(y); if (y == NIL) Treepoint = z; else if (z->getKey() < y->getKey()) y->setLeft(z); else y->setRight(z); z->setLeft(NIL); z->setRight(NIL); z->setColour(RED); insertFixup(z); }
SYMBOL * findInsertSymbol(char * key, int forceCreation) { SYMBOL *current, *parent, *x; int c; /* find future parent */ current = (root == NULL) ? NIL_SYM : root; parent = 0; while (current != NIL_SYM) { if( ((c = str2cmp(key, current->name)) == 0) && ((c = strcmp(key, current->name)) == 0) ) return(current); parent = current; current = (c < 0) ? current->left : current->right; } /* if forceCreation not specified just return */ if(forceCreation == LOOKUP_ONLY) return(NULL); /* allocate new symbol */ x = (SYMBOL *)callocMemory(sizeof(SYMBOL)); x->parent = parent; x->left = NIL_SYM; x->right = NIL_SYM; x->color = RED; /* insert node in tree */ if(parent) { if( (c = str2cmp(key, parent->name)) < 0) parent->left = x; else if(c > 0) parent->right = x; else if(strcmp(key, parent->name) < 0) parent->left = x; else parent->right = x; } else root =x; insertFixup(x); /* return new node */ ++symbolCount; return(x); }
void insertNode(RBTree *tree, RBData *data) { Node *current, *parent, *x; /* Find where node belongs */ current = tree->root; parent = 0; while (current != NIL) { if (compEQ(data->primary_key, current->data->primary_key)) { printf("insertNode: trying to insert but primary key is already in tree.\n"); exit(1); } parent = current; current = compLT(data->primary_key, current->data->primary_key) ? current->left : current->right; } /* setup new node */ if ((x = malloc (sizeof(*x))) == 0) { printf ("insufficient memory (insertNode)\n"); exit(1); } /* Note that the data is not copied. Just the pointer is assigned. This means that the pointer to the data should not be overwritten after calling this function. */ x->data = data; /* Copy remaining data */ x->parent = parent; x->left = NIL; x->right = NIL; x->color = RED; /* Insert node in tree */ if(parent) { if(compLT(data->primary_key, parent->data->primary_key)) parent->left = x; else parent->right = x; } else { tree->root = x; } insertFixup(tree, x); }
bool RedBlackTree<Key, Data>::insert(Key const &key, Data const & rec) { RedBlackNode<Key, Data> *current, *parent = nullNode, *x = nullNode; /* find future parent */ current = rootNode; while (valid(current)) { parent = current; int ret = Compare(key, current->id); if (ret < 0) current = current->left; else if (ret > 0) current = current->right; else return false; } /* setup new node */ if ((x = new RedBlackNode<Key, Data>()) == 0) { return false; } x->parent = parent; x->left = nullNode; x->right = nullNode; x->color = RED; x->id = Duplicate(key); x->data = rec; /* insert node in tree */ if (valid(parent)) { if (Compare(key, parent->id) <= 0) parent->left = x; else parent->right = x; } else { rootNode = x; } m_cachedSize++; insertFixup(x); return true; }
// insert new node (no duplicates allowed) RbtStatus rbtInsert(RBTreeType *tree, KeyType key, ValType val) { NodeType **root = &tree->root; NodeType *current, *parent, *x; // allocate node for data and insert in tree // find future parent current = *root; parent = 0; while (current != SENTINEL) { if (compEQ(key, current->key)) return RBT_STATUS_DUPLICATE_KEY; parent = current; current = compLT(key, current->key) ? current->left : current->right; } // setup new node if ((x = malloc (sizeof(*x))) == 0) return RBT_STATUS_MEM_EXHAUSTED; x->parent = parent; x->left = SENTINEL; x->right = SENTINEL; x->color = RED; x->key = key; x->val = val; // insert node in tree if(parent) { if(compLT(key, parent->key)) parent->left = x; else parent->right = x; } else { *root = x; } insertFixup(root, x, SENTINEL); return RBT_STATUS_OK; }
rbt_node* rbt_insert(T key, LRU_elem *associated_LRU_elem, rbt_node **root) { rbt_node *current, *parent, *x; /*********************************************** * allocate node for data and insert in tree * ***********************************************/ /* find where node belongs */ current = *root; parent = 0; while (current != NIL) { if (compEQ(key, current->key)) return (current); parent = current; current = compLT(key, current->key) ? current->left : current->right; } /* setup new node */ if ((x = malloc (sizeof(*x))) == 0) { printf ("insufficient memory (insertNode)\n"); exit(1); } x->key = key; x->parent = parent; x->left = NIL; x->right = NIL; x->color = RED; x->associated_LRU_elem = associated_LRU_elem; /* insert node in tree */ if(parent) { if(compLT(key, parent->key)) parent->left = x; else parent->right = x; } else { *root = x; } insertFixup(x, root); return(x); }
//----------------------------------------------------------------------------- /// insert new node in the tree as a child of given parent (after find was made) /// \param[in] x - new node to insert /// \param[in] parent - the parent of new node. This ptr is calculated by findNode function /// \param[in] p_root - the root of the tree //----------------------------------------------------------------------------- void insertNodeToParent(Node *x, Node*parent, Node**p_root) { x->parent = parent; x->left = NIL; x->right = NIL; x->color = RED; /* insert node in tree */ if(parent) { if(compLT(x->node_data, parent->node_data)) parent->left = x; else parent->right = x; } else { *p_root = x; } insertFixup(x, p_root); return; }
Node *insertNode(T data) { Node *current, *parent, *x; /*********************************************** * allocate node for data and insert in tree * ***********************************************/ /* find where node belongs */ current = root; parent = 0; while (current != NIL) { if (compEQ(data, current->data)) return (current); parent = current; current = compLT(data, current->data) ? current->left : current->right; } /* setup new node */ if ((x = malloc (sizeof(*x))) == 0) { printf ("insufficient memory (insertNode)\n"); exit(1); } x->data = data; x->parent = parent; x->left = NIL; x->right = NIL; x->color = RED; /* insert node in tree */ if(parent) { if(compLT(data, parent->data)) parent->left = x; else parent->right = x; } else { root = x; } insertFixup(x); return(x); }
RbtStatus rbtInsert(RbtHandle h, void *key, void *val) { NodeType *current, *parent, *x; RbtType *rbt = h; /* allocate node for data and insert in tree */ /* find future parent */ current = rbt->root; parent = 0; while (current != SENTINEL) { int rc = rbt->compare(key, current->key); if (rc == 0) return RBT_STATUS_DUPLICATE_KEY; parent = current; current = (rc < 0) ? current->left : current->right; } /* setup new node */ if ((x = malloc (sizeof(*x))) == 0) return RBT_STATUS_MEM_EXHAUSTED; x->parent = parent; x->left = SENTINEL; x->right = SENTINEL; x->color = RED; x->key = key; x->val = val; /* insert node in tree */ if(parent) { if(rbt->compare(key, parent->key) < 0) parent->left = x; else parent->right = x; } else { rbt->root = x; } insertFixup(rbt, x); return RBT_STATUS_OK; }
Node * insertNode(T data) { Node * current, * parent, * x; /*********************************************** * allocate node for data and insert in tree * ***********************************************/ /* find where node belongs */ current = root; parent = 0; while (current != NIL) { if (compEQ(data, current->data)) { ++current->count; return (current); } parent = current; current = compLT(data, current->data) ? current->left : current->right; } /* setup new node */ if ((x = malloc (sizeof(*x))) == 0) err(EMEM); x->data = data; x->parent = parent; x->left = NIL; x->right = NIL; x->color = RED; x->count = 1; /* insert node in tree */ if(parent) { if(compLT(data, parent->data)) parent->left = x; else parent->right = x; } else root = x; insertFixup(x); return(x); }
bool insert(BRTree * T, int val, int index, int k, int t) { if(T == NULL) { T = malloc(sizeof(BRTree *)); T->root = NULL; } BRNode * y = NULL; BRNode * x = T->root; while(x != NULL) { y = x; if(val <= x->value) { x = x->left; } else if (val > x->value) { x = x->right; } } x = malloc(sizeof(BRNode)); x->left = x->right = NULL; x->color = RED; x->value = val; x->index = index; x->p = y; if (y == NULL) { T->root = x; } else { if(val <= y->value) { y->left = x; } else { y->right = x; } } insertFixup(T, x); if (traverse(x, k, t)) { return true; } return false; }
int RedBlackTree::Insert(const char *szStr, int Offset) { if (szStr == NULL) { return 0 ; } unsigned int hashValue = crc32(szStr) ; TreeNode * pNode = m_pRoot ; TreeNode * tmp = NULL ; // 如果树为空的话 if (m_pRoot == m_pSentinel) { tmp = new TreeNode(hashValue) ; if (tmp == NULL) { exit(EXIT_FAILURE) ; } tmp->left = tmp->right = tmp->parent = m_pSentinel ; tmp->color = red ; tmp->pList->Push(Offset) ; m_pRoot = tmp ; return 1 ; } for (; pNode != m_pSentinel;) { if (hashValue < pNode->key && pNode->left != m_pSentinel) { pNode = pNode->left ; } else if (hashValue > pNode->key && pNode->right != m_pSentinel) { pNode = pNode->right ; } else if (hashValue == pNode->key) { pNode->pList->Push(Offset) ; return 1 ; } else { break ; } } if (pNode == m_pSentinel) { return 0; } tmp = NULL ; tmp = new TreeNode(hashValue) ; if (tmp == NULL) { exit(EXIT_FAILURE) ; } tmp->left = tmp->right = m_pSentinel ; tmp->color = red ; tmp->key = hashValue ; tmp->parent = pNode ; // 往左边插入 if (hashValue < pNode->key && pNode->left == m_pSentinel) { pNode->left = tmp ; } // 入右边插入 else if (hashValue > pNode->key && pNode->right == m_pSentinel) { pNode->right = tmp ; } tmp->pList->Push(Offset) ; insertFixup(tmp) ; return 1; }