树与森林的转换

来源:互联网 发布:正规淘宝客服在家兼职 编辑:程序博客网 时间:2024/04/28 18:09

以前在学习数据结构的时候,学到了树与森林的转换,下面贴出相应的代码!

       

#include <iostream>#include <cstdlib> using namespace std;typedef char ELEMTYPE;typedef struct TreeNode{ELEMTYPE data;struct TreeNode *lchild;struct TreeNode *nextsibling;}NodeType,*CSTree;    //孩子兄弟表示法#define MAXSIZE 15   //定义的一个树的最大结点个数CSTree    q[MAXSIZE+1];    //初始化void init_cstree(CSTree &tree){        tree->lchild = NULL;    tree->nextsibling = NULL;}//创建树void creat_cstree(CSTree &tree,const char* name){int count=0;    FILE *fin=fopen(name,"r");      char fa=' ',ch=' ';    for( fscanf(fin,"%c%c\n",&fa,&ch); ch!='#'; fscanf(fin,"%c%c\n",&fa,&ch) )     {              CSTree p=(CSTree)malloc(sizeof(CSTree));         init_cstree(p);        p->data=ch;        q[++count]=p;        if('#' == fa)            tree=p;        else        {            CSTree s = (CSTree)malloc(sizeof(CSTree));            int i;            for(i=1;i<=MAXSIZE;i++)            {                if(q[i]->data == fa)                    {                    s=q[i];                   //获取双亲结点                    break;                }            }             if(! (s->lchild) )        //如果该双亲结点还没有接孩子结点                s->lchild=p;    //获取孩子结点            else        //如果该双亲结点已经接了孩子结点            {                CSTree temp=s->lchild;                while(NULL != temp->nextsibling)                {                    temp=temp->nextsibling;                }                temp->nextsibling=p;  //获取兄弟结点            }        }    }     fclose(fin);}//bool Print_File_Specified_Node(CSTree &tree,char ch,int flag){  /*if(tree->data==ch)   {  if(tree->lchild!=NULL) cout<<tree->lchild->data<<" ";  else cout<<"# ";  if(tree->nextsibling!=NULL) cout<<tree->nextsibling->data<<" ";  else cout<<"# ";  return 1;  } if(tree->lchild!=NULL){return Print_File_Specified_Node(tree->lchild,ch);} if(tree->nextsibling!=NULL){return Print_File_Specified_Node(tree->nextsibling,ch);}*/  if(flag==0)  {  if(tree->data==ch)   {  if(tree->lchild!=NULL)   {  cout<<tree->lchild->data<<" ";   if(tree->lchild->nextsibling)     {flag=flag+1;   Print_File_Specified_Node(tree->lchild->nextsibling,ch,flag);   }  }  else cout<<"空";  }  else  {if(tree->lchild==NULL&&tree->nextsibling==NULL) return 0;else{if(tree->lchild!=NULL) Print_File_Specified_Node(tree->lchild,ch,0);if(tree->nextsibling!=NULL) Print_File_Specified_Node(tree->nextsibling,ch,0);}  }  }  else  {  cout<<tree->data<<" ";  if(tree->nextsibling)   {  flag=flag+1;      Print_File_Specified_Node(tree->nextsibling,ch,flag);  }  return 1;  } return 0;}//按照孩子兄弟的方法输出依赖关系void print_tree(CSTree &tree){cout<<endl<<tree->data<<"("; if(tree->lchild!=NULL){cout<<tree->lchild->data<<",";} else cout<<"#,";        if(tree->nextsibling!=NULL){cout<<tree->nextsibling->data<<")";}elsecout<<"#"<<")";if(tree->lchild!=NULL){print_tree(tree->lchild);}    if(tree->nextsibling!=NULL){print_tree(tree->nextsibling);}}//前序遍历void print_cstree(CSTree &tree){        cout<<tree->data<<"  ";        if(tree->lchild!=NULL)            print_cstree(tree->lchild);        if(tree->nextsibling!=NULL)            print_cstree(tree->nextsibling);}//判断该树是否为完全二叉树bool complete_binary_tree(CSTree &tree){if(tree->lchild==NULL&&tree->nextsibling!=NULL) return 0;if(tree->lchild!=NULL)            return complete_binary_tree(tree->lchild);    if(tree->nextsibling!=NULL)            return complete_binary_tree(tree->nextsibling);return 1;}//输出叶结点void print_LeafNode(CSTree &tree){if(tree->lchild==NULL&&tree->nextsibling==NULL) cout<<tree->data<<" ";if(tree->lchild!=NULL) print_LeafNode(tree->lchild);    if(tree->nextsibling!=NULL) print_LeafNode(tree->nextsibling);}int main(){    CSTree    cstree[3];for(int i=0;i<3;i++){    cstree[i]=(CSTree)malloc(sizeof(CSTree));    init_cstree(cstree[i]);}    creat_cstree(cstree[0],"Tree1.txt");creat_cstree(cstree[1],"Tree2.txt");creat_cstree(cstree[2],"Tree3.txt");    //输出树cout<<"前序遍历输出树1:"<<endl;print_cstree(cstree[0]);  //前序遍历输出一颗树cout<<endl;cout<<"前序遍历输出树2:"<<endl;print_cstree(cstree[1]);cout<<endl;cout<<"前序遍历输出树3:"<<endl;print_cstree(cstree[2]);    cout<<endl;cout<<"树的孩子兄弟法输出树1:"<<endl;    print_tree(cstree[0]);   //按照树的孩子兄弟法输出一颗树,输出符号为"#"代表该节点处的值为空,也就是意味着该结点为cout<<endl;cout<<"树的孩子兄弟法输出树2:"<<endl;print_tree(cstree[1]);cout<<endl;cout<<"树的孩子兄弟法输出树3:"<<endl;print_tree(cstree[2]);    cout<<endl;/**************************输出由文件指定的子节点的值******************/FILE* fp;fp=fopen("File_Specified_Node.txt","r");char ch=' ';for(fscanf(fp,"%c ",&ch);ch!='@';fscanf(fp,"%c ",&ch)){cout<<"文件指定结点"<<ch<<"的子结点为:";for(int i=0;i<3;i++){         if(Print_File_Specified_Node(cstree[i],ch,0)) break;}cout<<endl;}fclose(fp);/**************************将森林转为二叉树****************************/cstree[0]->nextsibling=cstree[1];cstree[1]->nextsibling=cstree[2];/**********************************************************************/printf("将3颗树的森林转化为二叉树结构\n");print_tree(cstree[0]);    //由于孩子兄弟的表示方法与二叉树的表示方法是类似等价的,现在以第一颗树的头结点作为整个二叉树的头结点cout<<endl;/*************************判断该二叉树是否为完全二叉树**************************///扫描每一个结点若某个结点存在右孩子而不存在左孩子的话,则该二叉树为非完全二叉树bool right=complete_binary_tree(cstree[0]);if(!right) cout<<"该森林转换后的二叉树不是完全二叉树"<<endl;else       cout<<"该森林转换后的二叉树为完全二叉树"<<endl;    /*************************输出叶结点********************************/cout<<"转换的二叉树的叶结点为:";print_LeafNode(cstree[0]);cout<<endl;/*******************************************************************/system("pause");    return 0;}

0 0
原创粉丝点击