# 数据结构之二叉树的遍历

/**************************************************************
> File Name: Tree.c
> Author: chengfeiyan
> Mail: 1493789272@qq.com
> Created Time: 2018年08月11日 星期六 16时30分59秒
**************************************************************/

#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *right, *left;
};
typedef struct node TreeNode;

TreeNode *CreatTree(int *a)
{
int i;
TreeNode *node[11] = {0};

for(i = 0; i < 10; i++)
{
node[i] = (TreeNode *)malloc(sizeof(TreeNode));
if(NULL == node[i])
{
printf("Malloc Error!\n");
}

node[i]->data = a[i];
node[i]->right = NULL;
node[i]->left = NULL;
}

for(i = 0; i < 10 / 2; i++)
{
node[i]->left = node[2 * i + 1];
node[i]->right = node[2 * i + 2];
}
return node[0];
}

int PreOrder(TreeNode *T)
{
if(NULL == T)
{
return 1;
}
printf("%d ", T->data);
PreOrder(T->left);
PreOrder(T->right);

return 0;
}

int InOrder(TreeNode *T)
{
if(NULL == T)
{
return 1;
}
InOrder(T->left);
printf("%d ", T->data);
InOrder(T->right);

return 0;
}

int PostOrder(TreeNode *T)
{
if(NULL == T)
{
return 1;
}
PostOrder(T->left);
PostOrder(T->right);
printf("%d ", T->data);

return 0;
}

int main()
{
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
TreeNode *root;

root = CreatTree(a);

PreOrder(root);
printf("\n");
InOrder(root);
printf("\n");
PostOrder(root);
printf("\n");
return 0;
}