n叉树(n有最大值)的数组形多叉树的先序递归遍历方法

#include "stdio.h"
#include "stdlib.h"

#define new(Class) (Class*)malloc(sizeof(Class))  

typedef struct node Node;
struct node{
	Node *next[3];  
	char content;
};

Node* createTree(){
	Node* A = new(Node);
	Node* B = new(Node);
	Node* C = new(Node);
	Node* D = new(Node);
	Node* E = new(Node);
	Node* F = new(Node);
	Node* G = new(Node);
	Node* H = new(Node);
	Node* I = new(Node);
	
	A->content = 'A';
	B->content = 'B';
	C->content = 'C';
	D->content = 'D';
	E->content = 'E';
	F->content = 'F';
	G->content = 'G';
	H->content = 'H';
	I->content = 'I';
	
	A->next[0] = B;
	A->next[1] = C;
	A->next[2] = NULL;
	B->next[0] = D;
	B->next[1] = E;
	B->next[2] = F;
	C->next[0] = G;
	C->next[1] = H;
	C->next[2] = NULL;
	D->next[0] = NULL;
	D->next[1] = NULL;
	D->next[2] = NULL;
	E->next[0] = I;
	E->next[1] = NULL;
	E->next[2] = NULL;
	F->next[0] = NULL;
	F->next[1] = NULL;
	F->next[2] = NULL;
	G->next[0] = NULL;
	G->next[1] = NULL;
	G->next[2] = NULL;
	H->next[0] = NULL;
	H->next[1] = NULL;
	H->next[2] = NULL;
	I->next[0] = NULL;
	I->next[1] = NULL;
	I->next[2] = NULL;
	
	return A;
}

void readTree(Node* head){
	int i;
	Node* cursor = head;
	printf("%c\n", cursor->content);
	for(i = 0; i < 3; i++){
		if(cursor->next[i] != NULL){
			readTree(cursor->next[i]);
		}
	}
}


int main(){
	readTree(createTree());
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值