From 6a60bc82e8b8e6cccd0d4c2214a1f291662215f0 Mon Sep 17 00:00:00 2001 From: xchaos Date: Sun, 12 Oct 2008 22:09:55 +0000 Subject: [PATCH] binary B+ tree - first attempt, compiles and runs git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@100 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- demos/trees.c | 75 +++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/demos/trees.c b/demos/trees.c index 8838e3f..87de034 100644 --- a/demos/trees.c +++ b/demos/trees.c @@ -13,71 +13,58 @@ program int seq=0; int odd=1; - for_ints(newkey, 8,1,-2,745,-32,-64,27,4 ) + print("Input values:"); + for_ints(newkey, 8,1,-2,745,-32,-64,27,4,-300,0,300,40,-30,-40,400, 200 ) { printf("%d. [%d]\n",seq,newkey); leaf=get_mem(Leaf); leaf->seq=seq++; - + + //store(leaf,root,newkey) is declared as: + //init leaf->__next=NULL; leaf->__seek=NULL; leaf->__key=newkey; - - //grow tree + + //store new node without indexing, first + insert(leaf,root,order_by_num,__key); + + //reindex B+ tree + for(leaf=root;leaf->__next;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) - { - leaf=leaf->__seek; - - } - //store new node - leaf=newleaf; - if(prev) + //auto seek + if(!leaf->__seek && leaf->__next) + leaf->__seek=leaf->__next->__next; + + if(leaf->__key<=newkey) { - leaf->__next=prev->__next->__next; - prev->__next=leaf; + if(leaf->__seek && leaf->__seek->__key>newkey) leaf->__seek=leaf->__seek->__next; } else - leaf->__next=root; - - //reindex B+ tree - for(leaf=root;leaf->__next;leaf=leaf->__next) { - //auto seek - if(!leaf->__seek) - leaf->__seek=leaf->__next->__next; - - if(leaf->__key<=newkey) + if(odd && leaf->__seek) { - if(leaf->__seek && leaf->__seek->__key>newkey) - leaf->__seek=leaf->__seek->__next; - } + leaf->__seek==leaf->__seek->__next; + } else { - if(odd && leaf->__seek) - { - leaf->__seek==leaf->__seek->__next; - odd=1-odd; - } - else - { - leaf->__seek=leaf->__next->__seek; - leaf->__next->__seek=NULL; - } - } - } + leaf->__seek=leaf->__next->__seek; + leaf->__next->__seek=NULL; + } + odd=1-odd; + } } } + print("Values were stored as:"); for_each(leaf,root) { - printf("%d. [%d]\n",leaf->seq,leaf->__key); + printf("%d. [%d]",leaf->seq,leaf->__key); + if (leaf->__seek) + printf("-> %d. [%d]\n",leaf->__seek->seq,leaf->__seek->__key); + else + print(""); } } -- 2.30.2