Code: Select all
struct avlNode_t *avlInsert(struct avlNode_t *startNode, struct avlNode_t *newNode, int (*comperator)(struct avlNode_t*, struct avlNode_t*)) {
if(unlikely(startNode == 0)) {
newNode->left= 0;
newNode->right= 0;
newNode->height= 0;
return newNode;
}
if(comperator(startNode,newNode) >= 0) {
startNode->left= avlInsert(startNode->left,newNode,comperator);
if(unlikely((startNode->left->height - startNode->right->height) > 1)) {
if(startNode->left->right != 0) {
startNode= avlRotateLeftRight(startNode);
} else {
startNode= avlRotateRight(startNode);
}
}
} else {
startNode->right= avlInsert(startNode->right,newNode,comperator);
if(unlikely((startNode->right->height - startNode->left->height) > 1)) {
if(startNode->right->left != 0) {
startNode= avlRotateRightLeft(startNode);
} else {
startNode= avlRotateLeft(startNode);
}
}
}
if(startNode->left == 0)
startNode->height= 1 + startNode->right->height;
else if(startNode->right == 0)
startNode->height= 1 + startNode->left->height;
else
startNode->height= 1 + MAX(startNode->left->height,startNode->right->height);
return startNode;
}
struct avlNode_t *avlDelete(struct avlNode_t *node, struct avlNode_t *del, int (*comperator)(struct avlNode_t*, struct avlNode_t*)) {
struct avlNode_t *new;
if(unlikely(node == 0))
return 0;
if(unlikely(node == del)) {
//found the node
if(node->left == 0) {
if(node->right == 0) {
//we have no children
return 0;
} else {
//we have one child
return node->right;
}
} else if(node->right == 0) {
//we have one child
return node->left;
} else {
//we have two children
if(node->left->height > node->right->height) {
//get the max node
new= avlMax(node->left);
new->left= avlDelete(node->left,new,comperator);
new->right= node->right;
} else {
//get the min node
new= avlMin(node->right);
new->left= node->left;
new->right= avlDelete(node->right,new,comperator);
}
node= new;
}
} else if(comperator(node,del) > 0) {
node->left= avlDelete(node->left,del,comperator);
if(unlikely((node->left->height - node->right->height) > 1)) {
if(node->left->right != 0) {
node= avlRotateLeftRight(node);
} else {
node= avlRotateRight(node);
}
}
} else {
node->right= avlDelete(node->right,del,comperator);
if(unlikely((node->right->height - node->left->height) > 1)) {
if(node->right->left != 0) {
node= avlRotateRightLeft(node);
} else {
node= avlRotateLeft(node);
}
}
}
if(node->left == 0)
if(node->right == 0)
node->height= 0;
else
node->height= 1 + node->right->height;
else if(node->right == 0)
node->height= 1 + node->left->height;
else
node->height= 1 + MAX(node->left->height,node->right->height);
return node;
}
struct avlNode_t *avlRotateLeft(struct avlNode_t *node) {
struct avlNode_t *newNode= node->right;
node->right= newNode->left;
if(node->left == 0)
if(node->right == 0)
node->height= 0;
else
node->height= 1 + node->right->height;
else if(node->right == 0)
node->height= 1 + node->left->height;
else
node->height= 1 + MAX(node->left->height,node->right->height);
newNode->left= node;
return newNode;
}
struct avlNode_t *avlRotateLeftRight(struct avlNode_t *node) {
node->left= avlRotateLeft(node->left);
return avlRotateRight(node);
}
struct avlNode_t *avlRotateRight(struct avlNode_t *node) {
struct avlNode_t *newNode= node->left;
node->left= newNode->right;
if(node->left == 0)
if(node->right == 0)
node->height= 0;
else
node->height= 1 + node->right->height;
else if(node->right == 0)
node->height= 1 + node->left->height;
else
node->height= 1 + MAX(node->left->height,node->right->height);
newNode->right= node;
return newNode;
}
struct avlNode_t *avlRotateRightLeft(struct avlNode_t *node) {
node->right= avlRotateRight(node->right);
return avlRotateLeft(node);
}