栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

树的存储结构(二叉树表示法)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

树的存储结构(二叉树表示法)

#include
#include
#include
#include
using namespace std;
int cnt=0;
typedef struct CSNode{
	char data;
	struct CSNode *firstchild,*nextsibling; 
}CSNode,*CSTree;
CSTree CreateCSTree(){
	char ch;
	CSTree T;
	cin>>ch;
	if(ch=='#')return NULL;
	else{
		T=(CSTree)malloc(sizeof(CSNode));
		T->data=ch;
		T->firstchild=CreateCSTree();
		T->nextsibling=CreateCSTree();
	}
}

void VisitCSTree(CSTree T) {
	cout<<"当前结点的值"<data<<",";
	cout<<"它的兄弟结点:"; 
	if (T->firstchild) {
		cout << T->firstchild->data; 
		CSTree p = T->firstchild;
		while (p->nextsibling) {
			cout << " , " << p->nextsibling->data;
			p = p->nextsibling;
		}
		
	}
	cout << endl;
}

void PreOrderVisitTree(CSTree T) {
	if (T)
	{
		VisitCSTree(T);
		PreOrderVisitTree(T->firstchild);
		PreOrderVisitTree(T->nextsibling);
	}
}

//树的深度
int Height(CSTree T){
	int hc,hs;
	if(T==NULL)return 0;
	else{
		hc=Height(T->firstchild);
		hs=Height(T->nextsibling);
		if(hc+1>hs)return hc+1;//第一子女树严格大于右兄弟树(如果等于返回的也是右兄弟树的高度) 
		else return hs;
	}
}
//叶子结点数
void countleaves(CSTree T){
	if(T==NULL)return ;
	if(T->firstchild==NULL)cnt++;
	countleaves(T->firstchild);
	
	countleaves(T->nextsibling);
}
int leaves(CSTree T){
	if(T==NULL)return 0; 
	if(T->firstchild==NULL)return 1+leaves(T->nextsibling);
	else return leaves(T->firstchild)+leaves(T->nextsibling);
} 

int main()
{
	CSTree T;
	T=CreateCSTree();
//	VisitCSTree(T);
	PreOrderVisitTree(T);
	cout<<"树高:"<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/657660.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号