diff options
Diffstat (limited to 'src/sptree.c')
-rw-r--r-- | src/sptree.c | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/sptree.c b/src/sptree.c index 08d5203..378f216 100644 --- a/src/sptree.c +++ b/src/sptree.c @@ -70,6 +70,8 @@ pvoid ctl_sptree_initX(ppvoid sp, uint k_size, uint v_size, ctl_spt_cmp f){ head->val_size = v_size; head->root = NULL; head->func = f; + if(*sp != NULL) + *sp = pVoid(head); return pVoid(head); } pvoid ctl_sptree_freeX(ppvoid sp){ @@ -100,8 +102,9 @@ uint ctl_sptree_getValSizeX(ppcvoid sp){ pvoid ctl_sptree_findX(ppvoid sp, pcvoid key){ SplayTree* head = getHeader(*sp); int t = 1; - if(head->root != NULL) + if(head->root != NULL){ head->root = acces(head->func, head->root, key, &t); + } if(t == 0) return head->root->val; else @@ -165,8 +168,8 @@ void ctl_sptree_splitX(ppvoid sp , pcvoid key, ppvoid l, ppvoid r){ SplayTreeNode* righ; if(head->root != NULL){ split(head->func, head->root, key, &left, &righ); - getHeader(*r)->root = left; - getHeader(*l)->root = righ; + getHeader(*l)->root = left; + getHeader(*r)->root = righ; head->root = NULL; } ctl_sptree_freeX(sp); @@ -230,11 +233,12 @@ ctl_priv SplayTreeNode* split(ctl_spt_cmp f, SplayTreeNode* nd, pcvoid k, if(ret >= 0){ *l = nd; *r = nd->rchd; + disconnR(nd); }else{ *l = nd->lchd; *r = nd; + disconnL(nd); } - disconnL(nd); return (ret == 0 ? *l : NULL); } ctl_priv SplayTreeNode* merge(SplayTreeNode* l, SplayTreeNode* r){ @@ -308,9 +312,10 @@ ctl_priv void splay_r(SplayTreeNode* m, SplayTreeNode* f, SplayTreeNode* g){ /********************* # for debug ************************/ #include <stdio.h> +#include <unistd.h> ctl_priv void dump_node(SplayTreeNode* now){ if(now == NULL) return ; - printf("now = (%5.1f, %2d) ", *(double*)now->key, *(int*)now->val); + printf("now = (%5.1f, %2d) %lld ", *(double*)now->key, *(int*)now->val, now); if(now->lchd == NULL) printf("lchd = ( NULL) "); else printf("lchd = (%5.1f, %2d) ", *(double*)now->lchd->key, *(int*)now->lchd->val); if(now->rchd == NULL) printf("rchd = ( NULL) "); @@ -320,4 +325,5 @@ ctl_priv void dump_node(SplayTreeNode* now){ printf("\n"); dump_node(now->lchd); dump_node(now->rchd); + fflush(stdout); } |