bool BSTinsert(nodeTree *&root, nodeTree* node) { if (root == NULL) { root = node; return 1; } if (node->key < root->key) return BSTinsert(root->pLeft, node); if (node->key > root->key) return BSTinsert(root->pRight, node); return 0; }
void CreateBST(BTNode **bt, int key[], int n) { int i; *bt = NULL; /* Make sure that *bt is empty,or it will kill*/ for (i = 0; i < n; i++) { BSTinsert(bt,key[i]); } }
/* Inserimento versione Randomizzata Esegue inserimento standar o in radice */ bst BSTinsert( bst b, int x, int y ) { if ( b == NULL ) return NEW( x,y, NULL, NULL ); if (is_lower_than(x,y,b->x, b->y) == 0){ return b; } if (rand() < RAND_MAX/(BSTcount( b )+1)){ return BSTinsertInRoot(b,x,y); } if (is_lower_than(x,y, b->x, b->y) <0) b->sx = BSTinsert( b->sx, x,y ); if (is_lower_than(x,y, b->x, b->y) >0) b->dx = BSTinsert( b->dx, x,y ); return b; }
int BSTinsert(BTNode **bt, int key) { if (NULL == (*bt)) { (*bt) = (BTNode*)malloc(sizeof(BTNode)); (*bt)->key = key; (*bt)->lchild = NULL; (*bt)->rchild = NULL; //printf("insert %d\n", (*bt)->key); return 1; } else { if ((*bt)->key == key) return -1; else if ((*bt)->key < key) BSTinsert(&((*bt)->rchild), key); else BSTinsert(&((*bt)->lchild), key); } }
void BSTinsert(treeElem ** root, int inputList[], int begin, int end) { int mid; if (begin > end) return; mid = (begin + end)/2; *root = malloc (sizeof(treeElem)); (*root)->val = inputList[mid]; (*root)->right = 0; (*root)->left = 0; //printf("begin %d mid %d end %d\n", begin, mid, end); BSTinsert(&((*root)->left), inputList, begin, mid-1); BSTinsert(&((*root)->right), inputList, mid+1, end); }
bool addNode(nodeTree *node) { if (!isAVL) { return BSTinsert(myTree->root, node); } else { bool taller; return AVLinsert(myTree->root, node, taller); } }
rbtree* nuovo(){ rbtree* mosaico = createrbtree(); adm_positions = BSTinit(); mosaico->count = 0; mosaico->xdown=0; mosaico->yup=0; mosaico->ydown=0; mosaico->yup=0; mosaico->ordine = 1; adm_positions = BSTinsert( adm_positions, 0, 0 ); rbinsert(mosaico,0,0); mosaico->perimetro = 4; return mosaico; }
int main() { int * testlist=0; int *testlist2=0; int testlist_len=100; treeElem* root; int test, i; makelist(&testlist, testlist_len); // a list for test is built BSTinsert(&root, testlist, 0, testlist_len-1); BSTInorder(root); printf("\n"); //printf("value = %d\n",testlist[0]); test = MaxDepth(root); printf("it has %d lvls\n", test); test = isBalanced(root); printf("balance condition is %d\n", test); test = ElemCounts(root); printf("this tree contains %d of elements\n", test); tree2arr(root, &testlist2, test); for (i=0; i<100; i++) printf("%d ",testlist2[i]); printf("\n"); free(testlist); return 0; }
// inert a new key into the Red Black Tree // if key already exist no operation is done RedBlackNode* RedBlackTree::insert(int newKey){ // Step 1: // create the node; and insert the node with standard BST insert RedBlackNode * insert = BSTinsert(newKey); RedBlackNode * retVal = insert; // Node is in tree already if(insert == 0){ return 0; } // Step 2: // restore Red Black Tree property // note that the insertion could only violate the following: // of "If a node is red, then its parent is black". // All other Red Black Tree properties hold. // Move up the tree while(insert->parent->red){ // When parent is on the left of it's parent if(insert->parent == insert->parent->parent->left){ RedBlackNode * y = insert->parent->parent->right; if(y->red){ // Case 1 insert->parent->red = false; y->red = false; insert->parent->parent->red = true; insert = insert->parent->parent; } else{ // Case 2 if(insert == insert->parent->right){ insert = insert->parent; leftrotate(insert); } // Case 3 insert->parent->red = false; insert->parent->parent->red = true; rightrotate(insert->parent->parent); } } // When parent is on the right of it's parent else{ RedBlackNode * y = insert->parent->parent->left; if(y->red){ // Case 1 insert->parent->red = false; y->red = false; insert->parent->parent->red = true; insert = insert->parent->parent; } else{ // Case 2 if(insert == insert->parent->left){ insert = insert->parent; rightrotate(insert); } // Case 3 insert->parent->red = false; insert->parent->parent->red = true; leftrotate(insert->parent->parent); } } } // Root's left must be black root->left->red = false; return retVal; }
/* Inserimento del singolo nodo, con valorizzazione di tutti i campi del nodo */ rbnode *simpleinsert(rbtree *tree, int x, int y) { // controlla se la tessera nelle posizioni x, y è ammessa if (BSTsearch(adm_positions,x,y)!= 1){ return NULL; } // cancella la tessera dalle posizioni ammesse adm_positions = BSTdelete(adm_positions,x,y); rbnode *q = (rbnode*) malloc(sizeof(rbnode)); rbnode *r = tree->root; rbnode *s = tree->nil; if(!q) { fprintf(stderr,"Errore di allocazione C\n"); exit(-4); } q->x = x; q->y = y; q->left = q->right = tree->nil; q->c = red; while(r != tree->nil) { /* Controllo dove va inserito il nuovo nodo */ s = r; if (is_lower_than(x,y, r->x, r->y) < 0){ r = r->left; } else{ if (is_lower_than(x,y, r->x, r->y) == 0) return NULL; r = r->right; } } q->up = s; if(s == tree->nil) tree->root = q; else{ if(is_lower_than(x,y, s->x, s->y) < 0) s->left = q; else{ if (is_lower_than(x,y, s->x, s->y) == 0) return NULL; s->right = q; } } // aumenta il numero nodi presenti tree->count=tree->count +1; // ottiene le posizioni ammissibili del nuovo nodo int* positions = get_admitted_positions(x,y); int collisions = 0; for(size_t i = 0; i < 7; i = i+2 ) { rbnode *rnode = search(tree,positions[i],positions[i+1]); if (rnode){ collisions = collisions +1; }else{ adm_positions = BSTinsert( adm_positions, positions[i],positions[i+1] ); } } /// caricamento variabili di utilità if (x<0 && x< tree->xdown) tree->xdown = x; if (x>0 && x> tree->xup) tree->xup = x; if (y<0 && y< tree->ydown) tree->ydown = y; if (y>0 && y> tree->yup) tree->yup = y; // generazione del perimetro e dell'ordine tree->ordine = 1+ MAX(tree->xup - tree->xdown,tree->yup - tree->ydown); tree->perimetro= (tree->perimetro+4) -(collisions*2); return q; }