二叉树遍历

/*
先序建立二叉树,中序输出二叉树
author:calm
*/

#include<stdio.h>
#include<malloc.h>
#define OK 1
#define FALSE 0
typedef int status;
typedef struct Node
{
 status data;
 struct Node *LTree,*RTree;
}BinaryTree;

/*PreCreateTree先序创建BinaryTree*/
void PreCreateTree(BinaryTree *head)
{
 int ch=0;
 scanf("%d",&ch);
 if(ch=='#')
 {
  head=NULL;
 }
 else
 {
  head=(BinaryTree *)malloc(sizeof(BinaryTree));
  head->data=ch;
  PreCreateTree(head->LTree);
  PreCreateTree(head->RTree);
 }
}
void print(int data)
{
 printf("%d ",data);
}
void InOrderTraverse(BinaryTree *head,void (*fun)(int num))
{
 fun(head->data);
 InOrderTraverse(head->LTree,fun);
 InOrderTraverse(head->RTree,fun);
}

void main()
{
 BinaryTree *head=NULL;
 printf("Input:");
 PreCreateTree(head);
 putchar('\n');
 InOrderTraverse(head,print);
 putchar('\n');
 getchar();
}

原文地址:https://www.cnblogs.com/calm/p/1151844.html