#include <stdio.h>
#include <stdlib.h>
#define DATATYPE char
#define MAXSIZE 100
typedef struct node{
DATATYPE data;
struct node *lchild,*rchild;
}LINKBINTREE;
LINKBINTREE *create()
{
int num,father,j = 0;
LINKBINTREE *pArr[MAXSIZE],*p;
char s[] = "abcdefghijk\0";
while(s[j]!='\0'){
p = (LINKBINTREE*)malloc(sizeof(LINKBINTREE));
p->rchild = NULL;
p->lchild = NULL;
p->data = s[j++];
pArr[j] = p;
if(j!=1){
father = j/2;//父节点
if(j%2==0){
pArr[father]->lchild = p;//左子树
}else
pArr[father]->rchild = p;
}
}
return pArr[1];//返回根节点即可找到剩余的节点
}
void preOrder(LINKBINTREE *bTree){
// 先序遍历,根左右
if(bTree){
printf("%c",bTree->data);
preOrder(bTree->lchild);
preOrder(bTree->rchild);
}
}
void midOrder(LINKBINTREE *bTree){
// 中序遍历,左根右
if(bTree){
midOrder(bTree->lchild);
printf("%c",bTree->data);
midOrder(bTree->rchild);
}
}
void postOrder(LINKBINTREE *bTree){
// 后序遍历,左右根
if(bTree){
postOrder(bTree->lchild);
postOrder(bTree->rchild);
printf("%c",bTree->data);
}
}
int main()
{
LINKBINTREE *bt = create();
preOrder(bt);
}
二叉树非递归遍历
链接: https://blog.csdn.net/weixin_44818115/article/details/112284460