From f94ee7710aeb641fe590fe14c5f9dd7ee8f6828b Mon Sep 17 00:00:00 2001 From: xchaos Date: Tue, 20 May 2008 17:38:49 +0000 Subject: [PATCH] some work git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@72 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- demos/trees.c | 81 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/demos/trees.c b/demos/trees.c index dff0dda..8838e3f 100644 --- a/demos/trees.c +++ b/demos/trees.c @@ -2,7 +2,7 @@ def_mem(Leaf) { - int value; + int seq; array(Leaf); }; @@ -10,10 +10,15 @@ program { Leaf leaf,root=NULL; int newkey; + int seq=0; + int odd=1; - for_ints(newkey, 8,1,-2,745,-32,-64,27,4 ) printf(" [%d]",i) + for_ints(newkey, 8,1,-2,745,-32,-64,27,4 ) { + printf("%d. [%d]\n",seq,newkey); + leaf=get_mem(Leaf); + leaf->seq=seq++; //init leaf->__next=NULL; @@ -22,37 +27,57 @@ program //grow tree { - void *prev=NULL, *newleaf=leaf; - //find where to store - for(leaf=root; leaf && leaf->__key<=newkey ; leaf=leaf->__next) + void *prev=NULL, *newleaf=leaf; + //find where to store + for(leaf=root;leaf && leaf->__key<=newkey;leaf=leaf->__next) + { + prev=leaf; + if(leaf->__seek && leaf->__seek->key<=newkey) { - prev=leaf; - if(leaf->__seek && leaf->__seek->key<=newkey) leaf=leaf->__seek; - } - //store new node - leaf=newleaf; - if(prev) - { - leaf->__next=prev->__next->__next; - prev->__next=leaf; - } - else - leaf->__next=root; + leaf=leaf->__seek; + + } + //store new node + leaf=newleaf; + if(prev) + { + leaf->__next=prev->__next->__next; + prev->__next=leaf; + } + else + leaf->__next=root; + + //reindex B+ tree + for(leaf=root;leaf->__next;leaf=leaf->__next) + { //auto seek - if(leaf->__next->__next;) + if(!leaf->__seek) leaf->__seek=leaf->__next->__next; - - //reindex B+ tree - for(leaf=root; leaf->__next ; leaf=leaf->__next) + + if(leaf->__key<=newkey) { - if (leaf->__seek) + if(leaf->__seek && leaf->__seek->__key>newkey) + leaf->__seek=leaf->__seek->__next; + } + else + { + if(odd && leaf->__seek) + { + leaf->__seek==leaf->__seek->__next; + odd=1-odd; + } + else { - if (leaf->__key <= newkey) - leaf->__seek=leaf->__seek->__next; + leaf->__seek=leaf->__next->__seek; + leaf->__next->__seek=NULL; } - else - leaf->__seek=leaf->__next->__seek; - } + } + } } - } + } + + for_each(leaf,root) + { + printf("%d. [%d]\n",leaf->seq,leaf->__key); + } } -- 2.30.2