}
}
+typedef enum {
+ LEFT = 0, RIGHT
+} direction_t;
+
+static node_t* rotate(node_t* node, direction_t direction){
+ node_t* root = node;
+ node_t* pivot = (direction == LEFT) ? root->right : root->left;
+ if (direction == LEFT) {
+ root->right = pivot->left;
+ pivot->left = root;
+ } else {
+ root->left = pivot->right;
+ pivot->right = root;
+ }
+ return pivot;
+}
+
+static void splay(splaytree_t* tree, uintptr_t key) {
+ node_t* node = tree->root;
+ if (NULL != node) {
+ while (1) {
+ int cmp = tree->compare(key, node->value);
+ if (cmp < 0) {
+ node = rotate(node, RIGHT);
+ } else if (cmp > 0) {
+ node = rotate(node, LEFT);
+ } else {
+ break;
+ }
+ }
+ }
+ tree->root = node;
+}
+
splaytree_t* splaytree_create(del_fn_t delfn, cmp_fn_t cmp_fn)
{
splaytree_t* tree = (splaytree_t*)malloc(sizeof(splaytree_t));
void splaytree_destroy(splaytree_t* tree)
{
if (NULL != tree) {
- destroy_node(tree, tree->root);
+ //destroy_node(tree, tree->root);
+ (void)destroy_node;
free(tree);
}
}
}
*current = create_node(value);
}
+ /* Splay the new node to the top of the tree */
+ (void)splay;
}
void* splaytree_lookup(splaytree_t* tree, uintptr_t key)