#include <iostream>
using namespace std;
typedef struct node{
int data;
struct node *lchild, *rchild;
}Node, *BiTree;
Node* CreateNode(int i){
Node *q = new Node;
q -> lchild = q -> rchild = NULL;
q -> data = i;
return q;
}
BiTree CreateTree(){
BiTree *p[N] = {NULL};
for(int i = 0; i < N; ++i){
p[i] = CreateNode(i + 1);
}
for(int i = 0; i < N/2; ++i){
p[i] -> lchild = p[i * 2 + 1];
p[i] -> rchild = p[i * 2 + 2];
}
return p[0];
}
//前序遍历
void PreOder(BiTree T){
if(NULL == T){
return;
}
cout << T -> data << " ";
PreOder(p -> lchild);
PreOder(p -> rchild);
}
//中序遍历
void InOder(BiTree T){
if(NULL == T){
return;
}
InOder(T -> lchild);
cout << T -> data << " ";
InOder(T -> rchild);
}
//后续遍历
void PostOder(BiTree T){
if(NULL == T)
return;
PostOder(T -> rchild);
PostOder(T -> lchild);
cout << T -> data << " ";
}
int main(){
BiTree T = CreateTree();
PreOder(T);
cout << endl;
InOder(T);
cout << endl;
PostOder(T);
cout << endl;
return 0;
}
二叉树的建立和三种遍历
最新推荐文章于 2020-04-29 03:34:12 发布