怎么建立一个非空二叉树的链表存储结构,根节点为T,写出二叉树非递归遍历算法
来源:百度知道 编辑:UC知道 时间:2024/06/03 10:04:30
用c\c++实现的,并且要能输入数据,并得出结果的
//C++实现
#include "stdio.h"
#include "stdlib.h"
#define ERROR -1
#define OK 1
typedef char TElemType;
typedef int status;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rcgild;
}BiTNode,*BiTree;
status CreateBiTree(BiTree &T);
void PreOrderTraverse(BiTree T,void (*visit)(TElemType e));
void PrintElem(TElemType e);
void main(){
BiTree T;
printf("Enter the creat tree TElemType:");
CreateBiTree(T); //建立二叉树 输入数据 比如:ABC##D##E##
printf("The tree is :\n");
PreOrderTraverse(T,PrintElem); //先序遍历 (函数指针)
printf("\n");
}
status CreateBiTree(BiTree &T){ //先序递归 建立二叉树
TElemType ch;
scanf("%c",&ch);
if(ch=='#')
T=NULL;
else{
T=(BiTNode*)malloc(sizeof(BiTNode));
T->data =