二叉树代码
//头文件
#ifndef __TEST_H__
#define __TEST_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef int Datatype;
typedef struct tree
{
Datatype data;
struct tree *left;
struct tree *right;
}Tree, *pTree;
pTree creat();
void xshow(pTree head);
void zshow(pTree head);
void hshow(pTree head);
#endif
//主函数
#include "test.h"
int main(int argc, char const *argv[])
{
pTree head = creat();
xshow(head);
printf("\n");
zshow(head);
printf("\n");
hshow(head);
printf("\n");
return 0;
}
//功能函数
#include "test.h"
//创建先序二叉树
pTree creat()
{
int vaile = 0;
scanf("%d", &vaile);
if(-1 == vaile)
return NULL;
pTree head = (pTree)malloc(sizeof(Tree));
if(NULL == head)
{
printf("创建失败。\n");
return NULL;
}
head->data = vaile;
head->left = creat();
head->right = creat();
return head;
}
//
//先序遍历
void xshow(pTree head)
{
if(head != NULL)
{
printf("%d ", head->data);
xshow(head->left);
xshow(head->right);
}
return;
}
//中序遍历
void zshow(pTree head)
{
if(head != NULL)
{
zshow(head->left);
printf("%d ", head->data);
zshow(head->right);
}
return;
}
//先序遍历
void hshow(pTree head)
{
if(head != NULL)
{
hshow(head->left);
hshow(head->right);
printf("%d ", head->data);
}
return;
}