树与森林的转换

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

       

#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<<")";
		}
		else
			cout<<"#"<<")";
	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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值