| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 549 人关注过本帖
标题:<求助啊>三色二叉树问题
只看楼主 加入收藏
zxr0015
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-7-5
收藏
 问题点数:0 回复次数:1 
<求助啊>三色二叉树问题
一棵二叉树可以按照如下规则表示成一个由0、1、2组成的字符序列,我们称之为“二叉树序列S”:
 
例如,下图所表示的二叉树可以用二叉树序列S=21200110来表示。
 
任务是要对一棵二叉树的节点进行染色。每个节点可以被染成红色、绿色或蓝色。并且,一个节点与其子节点的颜色必须不同,如果该节点有两个子节点,那么这两个子节点的颜色也必须不相同。给定一棵二叉树的二叉树序列,请求出这棵树中最多和最少有多少个点能够被染成绿色


求大神给出下列程序的注释,(每一步都是干啥的)我真的一头雾水啊....感激不尽啊:::

#include <iostream>
#include <string>
#include <stdlib.h>
#define DISABLE -2
#define RED 1
#define GREEN 2
#define BLUE 3
#define NOCOLOR 0
typedef struct bitnode
{
 int color;
 struct bitnode * lchild,*rchild,*fathernode,*prenode;
 
}bitnode,*BITree;
BITree prenode=NULL;
using namespace std;
int curpos=0,sonstr=0;
int *toint;
int ball_color[3];
string convert;
void outputmaxmin()
{
    int max=ball_color[0]>ball_color[1]?(ball_color[0]>ball_color[2]?ball_color[0]:ball_color[2]):(ball_color[1]>ball_color[2]?ball_color[1]:ball_color[2]);
    cout<<max<<endl;
    int min=ball_color[0]<ball_color[1]?(ball_color[0]<ball_color[2]?ball_color[0]:ball_color[2]):(ball_color[1]<ball_color[2]?ball_color[1]:ball_color[2]);
    cout<<min<<endl;
}
void computecolornode(BITree temp_tree)
{
    if(temp_tree)
    {
        ball_color[temp_tree->color-1]++;
        computecolornode(temp_tree->lchild);
        computecolornode(temp_tree->rchild);

    }
}

void colornode(BITree &temp_tree)
{
    if(temp_tree)
    {
        if(temp_tree->fathernode==NULL)
        {
            temp_tree->color=RED;
        }
        else if(temp_tree->fathernode->lchild==temp_tree||temp_tree->fathernode->lchild==NULL)
        {
            if(temp_tree->fathernode->color==RED)
                temp_tree->color=GREEN;
            if(temp_tree->fathernode->color==GREEN)
                temp_tree->color=RED;
            if(temp_tree->fathernode->color==BLUE)
                temp_tree->color=RED;
        }
        else
        {
            if(temp_tree->fathernode->color==RED&&temp_tree->fathernode->lchild->color==GREEN)
                temp_tree->color=BLUE;
            if(temp_tree->fathernode->color==BLUE&&temp_tree->fathernode->lchild->color==RED)
                temp_tree->color=GREEN;
            if(temp_tree->fathernode->color==GREEN&&temp_tree->fathernode->lchild->color==RED)
                temp_tree->color=BLUE;
        }
        colornode(temp_tree->lchild);
        colornode(temp_tree->rchild);

    }

}
string inputnode()
{
return convert.substr(sonstr++,1);
}
BITree xdd=NULL;
void constructbintree(BITree &temp_tree,string temp)
{
    if(!("0"))
        temp_tree=NULL;
    else
    {
        temp_tree=(bitnode *)malloc(sizeof(bitnode));
        temp_tree->color=NOCOLOR;
        temp_tree->prenode=prenode;
        xdd=prenode;
        if(xdd==NULL)
            temp_tree->fathernode=NULL;
        else
        {
            while(xdd!=NULL)
            {
                if(xdd->lchild==temp_tree||xdd->rchild==temp_tree)
                {
                    temp_tree->fathernode=xdd;
                    break;
                }
                xdd=xdd->prenode;
            }
        }
        prenode=temp_tree;
        constructbintree(temp_tree->lchild,inputnode());
        constructbintree(temp_tree->rchild,inputnode());

    }
    return;
}

void makestring(int toint[],int length,string &convert)
{
    convert="1";
    int * temp=new int[length];
    int cur=0,var=0;
    while(cur<length)
    {
        if(toint[cur]>0)
        {
            convert+="1";
            temp[cur]=toint[cur]-1;

        }
        if(toint[cur]==0)
        {
            convert+="00";
            temp[cur]=DISABLE;
            var=cur-1;
            for(;var>=0;var--)
            {
                if(temp[var]!=DISABLE)
                {
                    if(temp[var]==1)
                    {
                        convert+="1";
                        if(toint[var]==2)
                        {
                            temp[var]=DISABLE;
                        }
                        else
                        {
                            temp[var]--;
                        }
                        break;
                    }
                    else
                    {
                        convert+="0";
                        temp[var]=DISABLE;
                    }
                }
            }

        }
        cur++;
    }

}
int main()
{
 string seq;
 BITree temp_tree;
 cout<<"请输入二叉树序列"<<endl;
 cin>>seq;
 toint=new int[seq.length()];
 for(int i=0;i<seq.length();i++)
 {
  toint[i]=atoi(seq.substr(i,1).c_str());
 }
makestring(toint,seq.length(),convert);
constructbintree(temp_tree,inputnode());
colornode(temp_tree);
computecolornode(temp_tree);
outputmaxmin();
 return 0;
}
搜索更多相关主题的帖子: 颜色 二叉树 
2012-07-05 13:37
zxr0015
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-7-5
收藏
得分:0 
我只有
makestring(toint,seq.length(),convert);
constructbintree(temp_tree,inputnode());
computecolornode(temp_tree);
这三个函数不太懂,求大神只需说明一下这三个函数的作用,
2012-07-05 16:01
快速回复:<求助啊>三色二叉树问题
数据加载中...
 
   



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

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