凹入表打印、多叉树转二叉树遍历

在这里插入图片描述


```c

```c
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

const int nmax=100;
int f[nmax+1];
int s[nmax+1][nmax+1];
int tot;
char a[nmax];

int GetDepth(int x){
	if(!s[x][0])//如果x号父亲没有儿子了 
		return 1; //please finish the following parts,深度为1 
	
	int ans=1;
	for(int i=1;i<=s[x][0];i++) //please finish the following parts,s[x][0]存的是x号父亲的儿子个数 
		ans=max(ans,GetDepth(s[x][i])+1);//深度为原来的ans和所有子树的深度+1(父亲)中间的最大值 
	
	return ans;
}
  
void Print(int tot, int idx, char c)//tot是getdepth的返回值 ans
{
	for(int i=1;i<idx;i++)
		cout<<'#';
	cout<<c;
	for(int i=0;i<tot-idx;i++)
		cout<<'#';
	cout<<endl;
}
void DFS(int x,int d){
	Print(tot,d,a[x]);//d对应的是index,a[x]对应的是char,d是怎么得到的呢?一开始输入的是1 
	
	if(!s[x][0])
		return;        //please finish the following parts

	
	for(int i=1;i<=s[x][0];i++)//调用一次DFS打印一次 i遍历的范围是s[1][0](即1号父亲的子树个数) 
		DFS(s[x][i],d+1);	//please finish the following parts 后面的子树也会作为父亲,递归遍历 
		//Print(tot,d+1,a[s[x][i]]) 递归一次index+1,说明往下一层了,所以输出的字母要往后一格 
	return;
}

int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){//注意循环是从1开始的 
		cin>>a[i]>>f[i];//a和f存的是什么?a存的是字母,f存的是父亲的编号 
		int p=f[i];//p是第i个元素父亲的编号 
		s[p][0]++;//s存的是什么?//s[p][0]记录的是p父亲儿子的个数吗? 
		s[p][s[p][0]]=i;//p父亲的第s[p][0]个儿子输入的编号是i 
	}
	
	tot=GetDepth(1);

	
	DFS(1,1);
	
	return 0;
}

在这里插入图片描述

#include<iostream>
using namespace std;

struct Node {
    char data;
    int parent; //r和lchild的意义是什么?记录在node在数组中的位置 
	int lchild, rchild;//左儿子右兄弟 
} nodes[101];//用数组存,不是链表! 
int n, lastchild[101];//lastchild是干嘛用的? 

void buildTree(){
  cin >> n;
  for (int i = 1; i <= n; i++)
	  cin >> nodes[i].data >> nodes[i].parent;
  for (int i = 2; i <= n; i++){
	int f = nodes[i].parent;//f是新创建结点的父亲编号 
	 
	if (lastchild[f] == 0)//lastchild[f]=0意味着没有兄弟了,要往下创建儿子了 
		nodes[f].lchild = lastchild[f] = i;//lastchild[f]的值记录着正在创建的最后一个子节点 
	else{
		int j = lastchild[f]; //please finish the following parts
		nodes[j].rchild = lastchild[f] = i;//lastchild[f]不等于0,创建兄弟,即右儿子 
	}//i每加一次就要创建一个左儿子或右儿子,意思是第i个元素是上一个元素(若是兄弟,则是i-1,若是父亲则f(f=nodes[i].parent))的左子树或者右子树 
  }
}

void Preorder(Node &root){ //please finish the following parts
	cout << root.data;//注意判断条件 
	if (root.lchild != 0) Preorder(nodes[root.lchild]);
	if (root.rchild != 0) Preorder(nodes[root.rchild]);
}
void Midorder(Node &root){ //please finish the following parts
	if (root.lchild != 0) Midorder(nodes[root.lchild]);
	cout << root.data;
	if (root.rchild != 0) Midorder(nodes[root.rchild]);
}
void Postorder(Node &root){ //please finish the following parts
	if (root.lchild != 0) Postorder(nodes[root.lchild]);
	if (root.rchild != 0) Postorder(nodes[root.rchild]);
	cout << root.data;
}

int main(){
  buildTree();
  Preorder(nodes[1]); cout<<"\n";
  Midorder(nodes[1]); cout<<"\n";
  Postorder(nodes[1]);
  return 0;
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值