| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 522 人关注过本帖
标题:哪位大虾能用c语言编写个建立平衡二叉树的函数!!!!!
只看楼主 加入收藏
思恋到心碎
Rank: 2
等 级:论坛游民
帖 子:13
专家分:27
注 册:2010-10-22
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
哪位大虾能用c语言编写个建立平衡二叉树的函数!!!!!
如题目所述
搜索更多相关主题的帖子: 二叉树 c语言 函数 编写 
2010-12-12 12:01
wujieru
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:1
帖 子:1108
专家分:1939
注 册:2010-10-9
收藏
得分:20 
我不会 请高手作答
2010-12-12 12:09
思恋到心碎
Rank: 2
等 级:论坛游民
帖 子:13
专家分:27
注 册:2010-10-22
收藏
得分:0 
我自己来回答吧,自己写了个:
#include<stdio.h>
#include<stdlib.h>

typedef struct BF
{
    int data;
    int parent;
    int left;
    int right;
    int bf;
}BF;

typedef struct Bitree
{
    int data;
    struct Bitree *right;
    struct Bitree *left;
}Bitree;


typedef struct
{
    Bitree *base;
    int top;
    int bottom;
    int size;
}queue;


BF c[100];




void inistall(queue &q)
{
    q.base=(Bitree *)malloc(sizeof(Bitree)*100);
    q.top=q.bottom=0;
    q.size=100;
}

void push(Bitree t,queue &q)
{
    if(q.top==100)
        printf("queue full\n");
   
    q.base[q.top++]=t;
   
}

int empty(queue q)
{
    if(q.top==q.bottom)
        return 1;
    else
        return 0;
}

void pop(Bitree *&temp,queue &q)
{
    if(q.top==q.bottom)
        printf("queue null\n");
   
    else
        temp=&(q.base[q.bottom++]);
   
}

void travel(Bitree *t)
{
    queue q;
    Bitree *temp;
    inistall(q);
    push(*t,q);
    while(!empty(q))
    {
        pop(temp,q);
        printf("%d ",temp->data);
        if(temp->left)
            push(*temp->left,q);
        if(temp->right)
            push(*temp->right,q);
    }
   
}



////////////////////////////

int Deep(Bitree *T)
{
    int k,left,right;
    if(!T)k=0;
    else
    {
        right=Deep(T->right);
        left=Deep(T->left);
        if(right>left)k=1+right;
        else k=1+left;
        
    }
    return k;
   
}


int locate(int n)
{
    int i;
    for(i=0;i<100;i++)
        if(c[i].data==n)
            return i;
}


void B1(Bitree *T)
{
    Bitree *p,*q;
    int n1,n2;
    if(T)
    {
        p=T;
        q=p->left;
        n1=locate(p->data);
        if(q!=NULL)
        {n2=locate(q->data);
        c[n1].left=n2;
        c[n2].parent=n1;
        }
        else
        {
            c[n1].left=-1;
        }
        
        
        q=p->right;
        n1=locate(p->data);
        if(q!=NULL)
        {n2=locate(q->data);
        c[n1].right=n2;
        c[n2].parent=n1;
        }
        else
        {
            c[n1].right=-1;
        }
        
        B1(T->left);
        B1(T->right);
        
    }
   
   
}

void B2(Bitree *T)
{
    int n,left,right;
    if(T)
    {
        left=Deep(T->left);
        right=Deep(T->right);
        n=locate(T->data);
        c[n].bf=left-right;
        B2(T->left);
        B2(T->right);
        
    }
   
}

void B(Bitree *T)
{
    int i;
    for(i=0;i<100;i++)
        c[i].parent=c[i].left=c[i].right=-1;
    B1(T);
    B2(T);
}

void inistall(int a[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        c[i].data=a[i];
        c[i].left=c[i].right=c[i].parent=-1;
        c[i].bf=0;
    }
   
   
}

void find(Bitree *T,int data,Bitree *&p)
{
    if(T)
    {
        if(T->data==data)
            p=T;
        find(T->left,data,p);
        find(T->right,data,p);
    }
   
}

int hunt(Bitree *p,Bitree *q)
{
    if(p)
    {
        if(p->data==q->data)
            return 1;
        else
        {
            if(hunt(p->left,q))
                return 1;
            else
                return hunt(p->right,q);
        }
        
    }
    else return 0;
   
}


int kind(Bitree *p,Bitree *q)
{
    Bitree *r,*s;
    int k;
    if(p->right!=NULL)
    {
        r=p->right;
        s=r->right;
        if((s!=NULL)&&(hunt(s,q)))
            k=2;//RR
        s=r->left;
        if((s!=NULL)&&(hunt(s,q)))
            k=3;//RL
    }
    if(p->left!=NULL)
    {
        r=p->left;
        s=r->right;
        if((s!=NULL)&&(hunt(s,q)))
            k=4;//LR
        s=r->left;
        if((s!=NULL)&&(hunt(s,q)))
            k=1;//LL
    }
    return k;
}


void deal(Bitree *&T,int data)
{
    int i,k=0,j;
    Bitree *p,*q,*r,*temp;
    i=locate(data);
    while(c[i].bf<2&&c[i].bf>-2&&i!=-1)
        i=c[i].parent;
    if((c[i].bf>1||c[i].bf<-1)&&(i!=-1))
    {
        find(T,c[i].data,p);
        j=c[i].parent;
        find(T,data,q);
        if(j!=-1)
        {
            
            find(T,c[j].data,r);
            k=kind(p,q);//1:LL 2:RR 3:RL 4:LR
            switch(k)
            {
            case 1:r->left=p->left;
                p->left=r->left->right;
                r->left->right=p;
                break;
            case 2:r->right=p->right;
                p->right=r->right->left;
                r->right->left=p;
                break;
            case 3:temp=p->right->left;
                p->right->left=temp->right;
                temp->right=p->right;
                p->right=temp;
                p->right=temp->left;
                temp->left=p;
                r->right=temp;
                break;
            case 4:temp=p->left->right;
                p->left->right=temp->left;
                temp->left=p->left;
                p->left=temp;
                p->left=temp->right;
                temp->right=p;
                r->left=temp;
                break;
            default:break;
            }
            B(T);
        }
        else//A为头结点
        {   
            k=kind(p,q);
            switch(k)
            {
            case 1:T=p->left;
                p->left=T->right;
                T->right=p;
                break;
            case 2:T=p->right;
                p->right=T->left;
                T->left=p;
                break;
            case 3:temp=p->right->left;
                p->right->left=temp->right;
                temp->right=p->right;
                p->right=temp;
                p->right=temp->left;
                temp->left=p;
                T=temp;
                break;
            case 4:temp=p->left->right;
                p->left->right=temp->left;
                temp->left=p->left;
                p->left=temp;
                p->left=temp->right;
                temp->right=p;
                T=temp;
                break;
            default:break;
            }
            B(T);
            
        }
        
    }
   
}

void creat(Bitree *&T)
{
    int i,k,D[100],n,j;
    Bitree *p,*q,*R,*S,*G;
    printf("请输入数据个数\n");
    scanf("%d",&n);
    getchar();
   
    for(i=0;i<n;i++)
    {
        printf("第%d个数据:\n",i+1);
        scanf("%d",&D[i]);
    }
    inistall(D,n);
   
    for(i=0;i<n;i++)
    {
        
        if(i==0)
        {
            p=q=T=(Bitree *)malloc(sizeof(Bitree));
            p->data=D[i];
            p->left=p->right=NULL;
            B(T);
            continue;
        }
        
        q=(Bitree *)malloc(sizeof(Bitree));
        q->data=D[i];
        q->left=q->right=NULL;
        p=T;
        while(p!=NULL)
        {
            G=p;
            if(D[i]>p->data)
                p=p->right;
            else
                p=p->left;
        }
        
        if(D[i]>G->data)
            G->right=q;
        else
            G->left=q;
        B(T);
        
        deal(T,D[i]);
        
    }//for
   
}
void main()
{
    Bitree *T;
    creat(T);
    printf("生成的平衡二叉树为:\n");
    travel(T);
}
2010-12-12 16:14
快速回复:哪位大虾能用c语言编写个建立平衡二叉树的函数!!!!!
数据加载中...
 
   



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

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