struct node* insert(struct node* root, int value) { if(root == NULL){ return newnode(value); } if(value < root->data){ root->left = insert(root->left, value); } else { root->right = insert(root->right, value); } root->height = compare(heightof(root->left), heightof(root->right)) + 1; b = balance(root); if(b > 1){ if(value < root->left->data){ //left left return rightrotate(root); } else { //left right root->left = leftrotate(root->left); return rightrotate(root); } } if(b < -1){ if(value > root->right->data){ //right right return leftrotate(root); } else { //right left root->right = rightrotate(root->right); return leftrotate(root); } } return root; }
void splay(int x, int goal) { int y, z; for(;;) { if((y = pre[x]) == goal) break; if((z = pre[y]) == goal) right[y] == x ? leftrotate(y) : rightrotate(y); else { if(right[z] == y) { if(right[y] == x) leftrotate(z), leftrotate(y); else rightrotate(y), leftrotate(z); } else { if(left[y] == x) rightrotate(z), rightrotate(y); else leftrotate(y), rightrotate(z); } } } update(x); }
AVL_node* insert_AVL(AVL_node *node,int key) { if(node == NULL) return newNode(key); if(node->key > key) node->left = insert_AVL(node->left, key); else node->right = insert_AVL(node->right, key); node->height = max(height(node->left), height(node->right)) +1; int balance = height(node->left) - height(node->right); if(balance > 1 && node->left->key > key) return rightrotate(node); if(balance > 1 && node->left->key < key) { node->left = leftrotate(node->left); return rightrotate(node); } if(balance < -1 && node->right->key < key) return leftrotate(node); if(balance < -1 && node->right->key > key) { node->right = rightrotate(node->right); return leftrotate(node); } return node; }
AVL_node* delete_AVL(int key, AVL_node* root) { if(root==NULL) return NULL; if(root->key > key) root->left = delete_AVL(key,root->left); else if(root->key < key) root->right = delete_AVL(key,root->right); else { if(root->left == NULL) { AVL_node* temp = root; root = root->right; free(temp); return(root); } else if(root->right == NULL) { AVL_node* temp = root; root = root->left; free(temp); return(root); } AVL_node* temp = minValueNode(root->right); root->key = temp->key; root->right = delete_AVL(temp->key, root->right); return root; } if (root == NULL) return root; root->height = max(height(root->left), height(root->right)) + 1; int balance = height(root->left) - height(root->right); if (balance > 1 && height(root->left->left) - height(root->left->right) >= 0) return rightrotate(root); if (balance > 1 && height(root->left->left) - height(root->left->right) < 0) { root->left = leftrotate(root->left); return rightrotate(root); } if (balance < -1 && height(root->right->left) - height(root->right->right) <= 0) return leftrotate(root); if (balance < -1 && height(root->right->left) - height(root->right->right) > 0) { root->right = rightrotate(root->right); return leftrotate(root); } return root; }
void md5_transform(struct md_context *context) { uint32_t a, b, c, d, f, g, j, x, temp; uint32_t buf; a = context->h[0]; b = context->h[1]; c = context->h[2]; d = context->h[3]; for(j=0;j<16;j++) { f = ((c ^ d) & b) ^ d; g = j; temp = d; d = c; c = b; memcpy(&buf, &context->buffer[g*4], sizeof(uint32_t)); x = (a + f + k[j] + buf);//(*(uint32_t *)(&context->buffer[g * 4]))); b = b + leftrotate(x, r[j]); a = temp; } for(j=16;j<32;j++) { f = ((b ^ c) & d) ^ c; g = (5*j + 1) % 16; temp = d; d = c; c = b; memcpy(&buf, &context->buffer[g*4], sizeof(uint32_t)); x = (a + f + k[j] + buf);//(*(uint32_t *)(&context->buffer[g * 4]))); b = b + leftrotate(x, r[j]); a = temp; } for(j=32;j<48;j++) { f = b ^ c ^ d; g = (3 * j + 5) % 16; temp = d; d = c; c = b; memcpy(&buf, &context->buffer[g*4], sizeof(uint32_t)); x = (a + f + k[j] + buf);//(*(uint32_t *)(&context->buffer[g * 4]))); b = b + leftrotate(x, r[j]); a = temp; } for(j=48;j<64;j++) { f = c ^ (b | (~d)); g = (7*j) % 16; temp = d; d = c; c = b; memcpy(&buf, &context->buffer[g*4], sizeof(uint32_t)); x = (a + f + k[j] + buf);//(*(uint32_t *)(&context->buffer[g * 4]))); b = b + leftrotate(x, r[j]); a = temp; } context->h[0] += a; context->h[1] += b; context->h[2] += c; context->h[3] += d; }
/* Controllo se le proprietà dopo la cancellazione vanno bene */ void fixup(rbtree *tree, rbnode *x) { while(x != tree->root && x->c == black) { /* Finchè x non è la radice e il padre è black */ if(x == x->up->left) { rbnode *w = x->up->right; if(w->c == red) { w->c = black; x->up->c = red; leftrotate(tree, x->up); w = x->up->right; } if(w->left->c == black && w->right->c == black) { w->c = red; x = x->up; } else { if(w->right->c == black) { w->left->c = black; w->c = red; rightrotate(tree, w); w = x->up->right; } w->c = x->up->c; x->up->c = black; w->right->c = black; leftrotate(tree, x->up); x = tree->root; } } else { rbnode *w = x->up->left; if(w->c == red) { w->c = black; x->up->c = red; rightrotate(tree, x->up); w = x->up->left; } if(w->right->c == black && w->left->c == black) { w->c = red; x = x->up; } else { if(w->left->c == black) { w->right->c = black; w->c = red; leftrotate(tree, w); w = x->up->left; } w->c = x->up->c; x->up->c = black; w->left->c = black; rightrotate(tree, x->up); x = tree->root; } } } x->c = black; }
void rotate(int a[],int d,int n) { int i; for(i=0;i<d;i++) { leftrotate(a,n); } }
static void rb_insert(rbnode_t *tree, rbnode_t node) { rbnode_t root, uncle, parent; root = *tree; node->color = _RED_; while (node != root && node->parent->color == _RED_) { parent = node->parent ; if (parent == parent->parent->left) { uncle = parent->parent->right; if (uncle!=nil && uncle->color == _RED_) { parent->color = _BLACK_; uncle->color = _BLACK_; parent->parent->color = _RED_; node = parent->parent; } else { if (node == parent->right) { node = parent; leftrotate(tree, node); parent = node->parent; } parent->color = _BLACK_; parent->parent->color = _RED_; rightrotate(tree,parent->parent); } } else { uncle = parent->parent->left; if (uncle != nil && uncle->color == _RED_) { parent->color = _BLACK_; uncle->color = _BLACK_; parent->parent->color = _RED_; node = parent->parent; } else { if (node == parent->left) { node = parent; rightrotate(tree, node); parent = node->parent; } parent->color = _BLACK_; parent->parent->color = _RED_; leftrotate(tree,parent->parent); } } } (*tree)->color = _BLACK_; }
void maintain(node_ptr &R) { if (R->lchild != nilptr) { if (R->lchild->lchild->h == R->rchild->h + 1) { rightrotate(R); } else if (R->lchild->rchild->h == R->rchild->h + 1) { leftrotate(R->lchild); rightrotate(R); } } if (R->rchild != nilptr) { if (R->rchild->rchild->h == R->lchild->h + 1) { leftrotate(R); } else if (R->rchild->lchild->h == R->lchild->h + 1) { rightrotate(R->rchild); leftrotate(R); } } }
/* Inserimento di un nuovo elemento nell'albero */ int rbinsert(rbtree *tree, int xa, int ya) { rbnode *x = simpleinsert(tree, xa, ya); if (x == NULL) return -1; while(x != tree->root && x->up->c == red) { /* Finchè x non è la radice e il padre è red */ if(x->up == x->up->up->left) { /* Se il padre di x è uguale al figlio sinistro di suo nonno, cioè il padre di x */ rbnode *y = x->up->up->right; /* y è uguale allo zio di x */ if(y->c == red) { /* se il colore di y è red */ x->up->c = black; /* il colore del padre di x è black */ y->c = black; /* il colore di y è balck */ x->up->up->c = red; /* il colore del nonno di x è red */ x = x->up->up; /* x è uguale a suo nonno */ } else { if(x == x->up->right){ /* se x è uguale a suo fratello */ x = x->up; leftrotate(tree,x); /* leftrotate con x */ } x->up->c = black; /* il colore del padre di x è black */ x->up->up->c = red; /* il colore del nonno di x è red */ rightrotate(tree, x->up->up); /* rightrotate con il nonno di x */ } } else { rbnode *y = x->up->up->left; /* y è uguale al figlio sinistro del nonno di x */ if(y->c == red) { /* se il colore di y è red */ x->up->c = black; /* il colore del padre di x è black */ y->c = black; /* il colore di y è black */ x->up->up->c = red; /* il colore del nonno di x è red */ x = x->up->up; /* x è uguale a suo nonno */ } else { if(x == x->up->left){ /* se x è uguale a suo fratello */ x = x->up; /* x è uguale a suo padre */ rightrotate(tree,x); /* rightrotate con x */ } x->up->c = black; /* il colore del padre di x è black */ x->up->up->c = red; /* il colore del nonno di x è red */ leftrotate(tree, x->up->up); /* leftrotate con il nonno di x */ } } } tree->root->c = black; /* il colore della root è black */ }
void Sha1::processChunk(const char* data) { /* Extend the data to 80 bytes, make it big endian */ unsigned int extended[80]; for(int i = 0; i != 16; ++i) extended[i] = Endianness::bigEndian<unsigned int>(*reinterpret_cast<const unsigned int*>(data+i*4)); for(int i = 16; i != 80; ++i) extended[i] = leftrotate((extended[i-3] ^ extended[i-8] ^ extended[i-14] ^ extended[i-16]), 1); /* Initialize value for this chunk */ unsigned int d[5]; unsigned int f, constant, temp; std::copy(_digest, _digest+5, d); /* Main loop */ for(int i = 0; i != 80; ++i) { if(i < 20) { f = d[3] ^ (d[1] & (d[2] ^ d[3])); constant = constants[0]; } else if(i < 40) { f = d[1] ^ d[2] ^ d[3]; constant = constants[1]; } else if(i < 60) { f = (d[1] & d[2]) | (d[3] & (d[1] | d[2])); constant = constants[2]; } else { f = d[1] ^ d[2] ^ d[3]; constant = constants[3]; } temp = leftrotate(d[0], 5) + f + d[4] + constant + extended[i]; d[4] = d[3]; d[3] = d[2]; d[2] = leftrotate(d[1], 30); d[1] = d[0]; d[0] = temp; } /* Add the values to digest */ for(int i = 0; i != 5; ++i) _digest[i] += d[i]; }
/* insert word into a redblack tree */ int insert(char *word) { TREEREC *curr = ans->root, *par, *gpar, *prev = NULL, *wcreate(); int val; if( ans->root == NULL ) { ans->ans = ans->root = wcreate(word, NULL); return 1; } while( curr != NULL && (val = scmp(word, curr->word)) != 0 ) { prev = curr; if( val > 0 ) curr = curr->right; else curr = curr->left; } ans->ans = curr; if( curr == NULL ) /* insert a new node, rotate up if necessary */ { if( val > 0 ) curr = prev->right = wcreate(word, prev); else curr = prev->left = wcreate(word, prev); curr->colour = RED; while( (par = curr->par) != NULL && ( gpar = par->par ) != NULL && curr->par->colour == RED ) { if( par == gpar->left ) { if( gpar->right!=NULL && gpar->right->colour == RED ) { par->colour = BLACK; gpar->right->colour = BLACK; gpar->colour = RED; curr = gpar; } else { if( curr == par->right ) { curr = par; leftrotate(ans, curr); par = curr->par; } par->colour = BLACK; if( ( gpar=par->par ) != NULL ) { gpar->colour = RED; rightrotate(ans, gpar); } } } else { if( gpar->left!=NULL && gpar->left->colour == RED ) { par->colour = BLACK; gpar->left->colour = BLACK; gpar->colour = RED; curr = gpar; } else { if( curr == par->left ) { curr = par; rightrotate(ans, curr); par = curr->par; } par->colour = BLACK; if( ( gpar=par->par ) != NULL ) { gpar->colour = RED; leftrotate(ans, gpar); } } } } if( curr->par == NULL ) ans->root = curr; ans->root->colour = BLACK; return 1; } return 0; }
unsigned rightrotate(unsigned x, int i) { return leftrotate(x, 32 - i); }
static void rb_delete_fixup(struct rbtree_elem **rootp, struct rbtree_elem *node) { struct rbtree_elem *root, *parent, *sibling ; root = *rootp; parent = node->parent; while (node != root && node->color == _BLACK_) { if (node == parent->left) { sibling = parent->right; if (sibling->color == _RED_) { sibling->color = _BLACK_; parent->color = _RED_; leftrotate(rootp, parent); parent = node->parent; sibling = parent->right; } if ( sibling->left->color == _BLACK_ && sibling->right->color == _BLACK_) { sibling->color = _RED_; node = parent; parent = node->parent; } else { if (sibling->right->color == _BLACK_) { sibling->left->color = _BLACK_; sibling->color = _RED_; rightrotate(rootp, sibling); parent = node->parent; sibling = parent->right; } sibling->color = parent->color; parent->color = _BLACK_; sibling->right->color = _BLACK_; leftrotate(rootp, parent); node = root; } } else { //node != parent->left sibling = parent->left ; if (sibling->color == _RED_) { sibling->color = _BLACK_; parent->color = _RED_; rightrotate(rootp, parent); parent = node->parent; sibling = parent->left; } if ( sibling->left->color == _BLACK_ && sibling->right->color==_BLACK_) { sibling->color = _RED_; node = parent; parent = node->parent; } else { if (sibling->left->color == _BLACK_) { sibling->right->color = _BLACK_; sibling->color = _RED_; leftrotate(rootp, sibling); parent = node->parent; sibling = parent->left; } sibling->color = parent->color ; parent->color = _BLACK_ ; sibling->left->color = _BLACK_ ; rightrotate(rootp, parent); node = root ; } } } node->color = _BLACK_ ; }
// 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; }
char* md5calc(unsigned char *inbuf, size_t bufsize) { const unsigned int r[64] = { 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 }; const unsigned int k[64] = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 }; /* Initialize */ unsigned int h[8] = {0x67452301,0, 0xefcdab89,0, 0x98badcfe,0, 0x10325476,0}; /* Padding */ unsigned char *workbuf = (unsigned char *)malloc(bufsize + 128); size_t work_size = padding(workbuf, inbuf, bufsize, false); unsigned int wh[8] = {0,0,0,0,0,0,0,0}; int pos, i; unsigned int ChunkDat[16]; unsigned int f, g, t; /* Processing */ for (pos = 0; (size_t)pos < work_size; pos += 64) { /* Break chunk into 16x little-endian int32 */ for (i = 0; i < 16; i++) ChunkDat[i] = (workbuf[pos + i*4]) | (workbuf[pos + i*4 +1] << 8) | (workbuf[pos + i*4 +2] << 16) | (workbuf[pos + i*4 +3] << 24); /* Initialize Hash */ for (i = 0; i < 8; i+=2) wh[i] = h[i]; for (i = 1; i < 8; i+=2) wh[i] = 0; /* Overflown bytes */ /* Main loop */ for (i = 0; i < 64; i++) { if ((i >= 0)&&(i <= 15)) { f = (wh[2] & wh[4]) | ((~wh[2]) & wh[6]); g = i; } else if ((i >= 16)&&(i <= 31)) { f = (wh[6] & wh[2]) | ((~wh[6]) & wh[4]); g = (5 * i + 1) % 16; } else if ((i >= 32)&&(i <= 47)) { f = wh[2] ^ wh[4] ^ wh[6]; g = (3 * i + 5) % 16; } else { f = wh[4] ^ (wh[2] | (~wh[6])); g = (7 * i) % 16; } t = wh[6]; wh[6] = wh[4]; wh[4] = wh[2]; wh[2] += leftrotate(wh[0] + f + k[i] + ChunkDat[g], r[i]); wh[0] = t; } /* Add hash */ for (i = 0; i < 8; i+=2) h[i] += wh[i]; for (i = 1; i < 8; i+=2) h[i] = 0; /* Overflown bytes */ } /* answer */ sprintf_s(hashbuf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", h[0]&0xff, (h[0]>>8)&0xff, (h[0]>>16)&0xff, (h[0]>>24)&0xff, h[2]&0xff, (h[2]>>8)&0xff, (h[2]>>16)&0xff, (h[2]>>24)&0xff, h[4]&0xff, (h[4]>>8)&0xff, (h[4]>>16)&0xff, (h[4]>>24)&0xff, h[6]&0xff, (h[6]>>8)&0xff, (h[6]>>16)&0xff, (h[6]>>24)&0xff); return hashbuf; }
void insert(int val) { nd *ptr=root, *parent, *temp, *imbal=NULL; int b; while(ptr) { parent=ptr; if(val<ptr->data) ptr=ptr->left; else ptr=ptr->right; } temp=createnode(val); if(val<parent->data) parent->left=temp; else parent->right=temp; printf("\nNode Created. Proceeding to balance."); correctheight(temp->data); printf("\nHeight Corrected."); ptr=root; while(ptr) { b=bal(ptr); if(b<-1||b>1) imbal=ptr; if(temp->data<ptr->data) ptr=ptr->left; else ptr=ptr->right; } if(imbal) { printf("\nImbalance Detected. Proceeding to balance."); b=bal(imbal); if(b>0) { if(bal(imbal->left)<0) { leftrotate(imbal->left); rightrotate(imbal); } else rightrotate(imbal); } if(b<0) { if(bal(imbal->right)>0) { rightrotate(imbal->right); leftrotate(imbal); } else leftrotate(imbal); } } printf("\nTree balanced."); }
unsigned L2(unsigned x) { return x ^ leftrotate(x, 13) ^ leftrotate(x, 23); }
unsigned L(unsigned x) { return x ^ leftrotate(x, 2) ^ leftrotate(x, 10) ^ leftrotate(x, 18) ^ leftrotate(x, 24); }
void del(nd* p) { nd *ptr, *temp=root, *imbal=NULL; int b; if(p!=root) { while(temp) { if(temp->left==p||temp->right==p) break; if(p->data<temp->data) temp=temp->left; else temp=temp->right; } } if(p->left==NULL&&p->right==NULL) { if(p==root) root=NULL; else if(temp->left==p) temp->left=NULL; else temp->right=NULL; free(p); } else { ptr=p; ptr=ptr->right; if(ptr) { while(ptr->left) ptr=ptr->left; p->data=ptr->data; del(ptr); } else { if(p==root) root=root->left; else if(temp->right==p) temp->right=p->left; else temp->left=p->left; free(p); } } correctheight(temp->data); printf("\nDeleting...."); ptr=root; while(ptr) { b=bal(ptr); if(b<-1||b>1) imbal=ptr; if(temp->data<ptr->data) ptr=ptr->left; else ptr=ptr->right; } if(imbal) { printf("\nImbalance Detected. Proceeding to balance."); b=bal(imbal); if(b>0) { if(bal(imbal->left)<0) { leftrotate(imbal->left); rightrotate(imbal); } else rightrotate(imbal); } if(b<0) { if(bal(imbal->right)>0) { rightrotate(imbal->right); leftrotate(imbal); } else leftrotate(imbal); } } }