| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 817 人关注过本帖
标题:B-树
取消只看楼主 加入收藏
liyifeng
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-5-12
收藏
 问题点数:0 回复次数:1 
B-树
本人找不出下面的程序错在哪里,希望哪位大虾能帮帮忙,,万分感谢!!
/*B-树
 *查找
 *插入
 *删除
 */
#include<stdio.h>
#include<stdlib.h>
#define m 3
typedef struct node
{
    int n;
    int v[2*m];
    struct node *prt;
    struct node *link[2*m+1];
}NODE;

NODE * lookup(NODE *h, int *k, int *flag, int x)
{
    NODE *p,*q;
    p=h;
    q=p;
    *flag=0;
    while (p != NULL && *flag == 0)
    {
        q=p;
        *k=1;
        while (*k < q->n && q->v[*k-1] < x)
        {
            *k=*k+1;
        }
        if (q->v[*k-1] == x)
        {
            *flag=1;
        }
        else
        {
            if (*k == q->n && q->v[*k-1] < x)
            {
                p=q->link[*k];
            }
            else
            {
                p=q->link[*k-1];
                *k=*k-1;
            }
        }
    }
    return q;
}

void insert(NODE **h, int x)
{
    NODE *p,*q,*u,*s;
    int i,k,flag,y,t;
    if (*h == NULL)
    {
        *h=(NODE *)malloc(sizeof(NODE));
        for (i=0; i < 2*m+1; i++)
        {
            (*h)->link[i]=NULL;
        }
        (*h)->prt=NULL;
        (*h)->n=1;
        (*h)->v[0]=x;
        return ;
    }
    q=lookup(*h,&k,&flag,x);
    if (flag == 1)
    {
        return ;
    }
    p=NULL;
    t=0;
    while (t == 0)
    {
        if (q->n == k)
        {
            y=x;
            u=p; /*这里的P表示空指针或是新建的右指针*/
        }
        else
        {
            y=q->v[q->n-1];
            u=q->link[q->n];
            for (i=q->n-1; i > k; i--)
            {
                q->v[i]=q->v[i-1];
                q->link[i+1]=q->link[i];
            }
            q->v[k]=x;
            q->link[k+1]=p;/*这里的P表示空指针或是新建的右指针*/
            if (p != NULL)
            {
                p->prt=q;
            }
        }
        if (q->n < 2*m)
        {
            q->n=q->n+1;
            q->v[q->n-1]=y;
            q->link[q->n]=u;
            if (u != NULL)
            {
                u->prt=q;
            }
            t=1;
        }
        else
        {/*1*/
            p=(NODE *)malloc(sizeof(NODE));
            p->n=m;
            q->n=m;
            p->prt=q->prt;
            x=q->v[m];
            for (i=0; i < m-1; i++)
            {
                p->v[i]=q->v[i+m+1];
                p->link[i]=q->link[i+m+1];
                if (q->link[i+m+1] != NULL)
                {
                    q->link[i+m+1]->prt=p;
                }
            }
            p->v[m-1]=y;
            p->link[m-1]=q->link[2*m];
            p->link[m]=u;
            if (u != NULL)
            {
                u->prt=p;
            }
            for (i=m+1; i < 2*m+1; i++)
            {
                q->link[i]=NULL;
                p->link[i]=NULL;
            }
            if (q->prt == NULL)
            {
                s=(NODE *)malloc(sizeof(NODE));
                s->n=1;
                s->v[0]=x;
                s->link[0]=q;
                s->link[1]=p;
                s->prt=NULL;
                for (i=2; i < 2*m+1; i++)
                {
                    s->link[i]=NULL;
                }
                *h=s;
                p->prt=s;
                q->prt=s;
                t=1;
            }
            else
            {
                q=q->prt;
                k=1;
                while (k <= q->n && q->v[k-1] < x)
                {
                    k++;
                }
                k--;
            }
        }/*1*/
    }/*第一个while*/
    return ;
}

void dell(NODE **h, int x)
{
    NODE *p,*q,*s,*u;
    int i,j,y,k,flag,t;
    q=lookup(*h,&k,&flag,x);
    if (flag == 0)
    {
        return ;
    }
    p=q->link[k];
    if (p != NULL)
    {
        while (p->link[0] != NULL)
        {
            p=p->link[0];
        }
        q->v[k-1]=p->v[0];
        q=p;
        k=1;
    }
    for (i=k; i < q->n; i++)//
    {
        q->v[i-1]=q->v[i];
    }
    q->n=q->n-1;
    while (q != *h && q->n < m)
    {
        p=q->prt;
        j=1;
        while (j < p->n && p->link[j-1] != q)
        {
            j++;
        }
        if (j <= p->n && (p->link[j])->n > m) /*右借*/
        {
            s=p->link[j];
            y=p->v[j-1];
            p->v[j-1]=s->v[0];
            u=s->link[0];
            for (i=1; i < s->n; i++)
            {
                s->v[i-1]=s->v[i];
                s->link[i-1]=s->link[i];
            }
            s->link[s->n-1]=s->link[s->n];
            s->link[s->n]=NULL;
            s->n=s->n-1;
            q->n=q->n+1;
            q->v[q->n-1]=y;
            q->link[q->n]=u;
            if (u != NULL)
            {
                u->prt=q;
            }
        }
        else
        {
            if (j > 1 && p->link[j-2]->n > m) /*左借*/
            {
                s=p->link[j-2];
                y=p->v[j-2];
                u=s->link[s->n];
                s->link[s->n]=NULL;
                p->v[j-2]=s->v[s->n-1];
                s->n=s->n-1;
                q->n=q->n+1;
                q->link[q->n]=q->link[q->n-1];
                for (i=q->n-1; i > 0; i--)
                {
                    q->v[i]=q->v[i-1];
                    q->link[i]=q->link[i-1];
                }
                q->v[0]=y;
                q->link[0]=u;
                if (u != NULL)
                {
                    u->prt=q;
                }
            }
            else /*合并*/
            {
                if (j == p->n+1)
                {
                    q=p->link[j-2];
                    s=p->link[j-1];                                      
                    j--;
                }
                else
                {
                    s=p->link[j];
                }
                q->v[q->n]=p->v[j-1];
                for (i=0; i < s->n; i++)
                {
                    q->v[q->n+i+1]=s->v[i];
                    q->link[q->n+i+1]=s->link[i];
                    if (s->link[i] != NULL)
                    {
                        s->link[i]->prt=q;
                    }
                }//
                q->v[q->n]=p->v[j-1];
                t=q->n+1;
                for (i=0; i < s->n; i++)
                {
                    q->v[t+i]=s->v[i];
                    q->link[t+i]=s->link[i];
                    if (s->link[i] != NULL)
                    {
                        s->link[i]->prt=q;
                    }
                }
                q->n=2*m;
                q->link[t+s->n]=s->link[s->n];
                if (s->link[s->n] != NULL)
                {
                    s->link[s->n]->prt=q;
                }
                free(s);
                for (i=j; i < p->n; i++)
                {
                    p->v[i-1]=p->v[i];
                    p->link[i]=p->link[i+1];
                }
                p->n=p->n-1;
                s=q;
                q=p;
            }
        }
    }
    if (q == (*h) && q->n == 1)
    {
        free(*h);
        (*h)=s;
        (*h)->prt=NULL;printf("error");
        if (s->n == 1)
        {printf("error");
            (*h)=NULL;
            free(s);
            s=NULL;  
        }
    }
    return ;
}

int main()
{
    NODE *h=NULL,*p;
    int i,k,flag;
    for (i=1; i < 51; i++)
    {
    insert(&h,i);
    p=lookup(h,&k,&flag,i);
    printf("%d %d %d \n",p->v[k-1],k,flag);
    }
    for (i=50; i > 2;i--)
    {
        dell(&h,i);
        p=lookup(h,&k,&flag,i);
        printf("%d %d %d  \n",i,k,flag);
    }
    return 0;
}
搜索更多相关主题的帖子: include 
2008-05-12 09:36
liyifeng
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-5-12
收藏
得分:0 
回复 2# 的帖子
呵。注释忘了加上。。
2008-05-16 16:08
快速回复:B-树
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029482 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved