some work
authorxchaos <xchaos@4bb87942-c103-4e5a-b51c-0ebff58f8515>
Tue, 20 May 2008 17:38:49 +0000 (17:38 +0000)
committerxchaos <xchaos@4bb87942-c103-4e5a-b51c-0ebff58f8515>
Tue, 20 May 2008 17:38:49 +0000 (17:38 +0000)
git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@72 4bb87942-c103-4e5a-b51c-0ebff58f8515

demos/trees.c

index dff0ddafdeea0213c17f41f1ed2a0e3b4c2f500b..8838e3f66153cdc701b3fb7aeccb7f0dc0b590d1 100644 (file)
@@ -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);
+ }
 }
This page took 0.179285 seconds and 4 git commands to generate.