数据结构-树的三种遍历(递归实现)

#include<bits/stdc++.h>
#define MAXSIZE 100 
#define m 3 
using namespace std;
typedef char datatype;
typedef struct node{
	datatype data;
	int parent;
}node;
typedef struct tree{
	node treelist[MAXSIZE];
	int length,root;
}tree;  //双亲表示法



typedef char datatype;
typedef struct node{
	datatype data;
	struct node *child[m];
}node, *tree;
tree root;  //指针形式的孩子表示法



typedef char datatype;
typedef struct node{
	int child;
	struct node*chnode;
}chnode, *chpoint;
typedef struct{
	datatype data;
	chpoint firstchild;
}node;
typedef struct{
	node treelist [MAXSIZE];
	int length,root;
}tree;//链表形式的孩子表示法 



typedef char datatype;
typedef struct node {
	datatype data;
	struct node *firstchild, *rightsibling;
}node, *pnode;
pnode root; //二叉树表示法 
int main(){
	
	  
	
    return 0;
}
void preorder(tree p){   //指针形式的表示法-前序遍历 
	int i;
	if(p!=NULL){
	printf("%c",p->data);
	for(i=0;i<m;i++){
		preorder(p->child[i]);
	}
}		
}
void postorder(tree p){  //指针形式的表示法-后序遍历
	int i;
	if(p!=NULL){
		for(i=0;i<m;i++){
			postorder(p->child[i]);
			printf("%c", p->data);
		}
	}
}
tree createtree(){  //建立一颗三度的树
int i;
tree p;
char ch;
if((ch=getchar())=='#'){
	p=NULL;
}
else{
	p=(tree)malloc(sizeof(node));
	p->data=ch;
	for(i=0;i<m;i++){
		p->child[i]=createtree(); 
	}
}	
}

void levelorder(tree t){  //指针表示法-层次遍历 
	tree queue[100];
	tree p;
	int f=0,r=1,i;
	queue[0]=t;
	while(f<r){
		p=queue[f];
		printf("%c", p->data);
		f++;
		for(i=0;i<m;i++){
			if(p->child[i]!=NULL){
				queue[r]=p->child[i];
				r++;
			}
		}
	}
	
}




1.指针形式的表示法-前序遍历 

2.指针形式的表示法-后序遍历

3.建立一颗三度的树

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值