1.ertree.h
#ifndef _TREE_H
#define _TREE_H
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
typedef struct node
{
char ch;
struct node *left,*right;
}TREE;
TREE* initTREE();
void printBeforeTREE(TREE* tree);
void printMidTREE(TREE* tree);
TREE* insertfindTREE(TREE* tree,char cha);
void printTREE(TREE* tree);
#endif;
2.ertree.c
#include "tree.h"
#include "stack.h"
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
TREE* inputTREE(TREE* tree)
{
if(tree==NULL)
tree=(TREE*)malloc(sizeof(TREE));
char ch;
ch=getche();
TREE* tree=NULL;
if(ch=='#')
return NULL;
tree->ch=ch;
return tree;
}
TREE* initTREE()
{
TREE* tree=NULL;
char ch;
ch=getche();
if(ch=='#')
return NULL;
tree=(TREE*)malloc(sizeof(TREE));
tree->ch=ch;
tree->left=initTREE();
tree->right=initTREE();
return tree;
}
//查找插入 BST
TREE* insertfindTREE(TREE* tree,char cha){
if(tree==NULL)
{ tree=(TREE*)malloc(sizeof(TREE));
tree->left=tree->right=NULL;
tree->ch=cha;
}else if(tree->ch<cha)
{
tree->right=insertfindTREE(tree->right,cha);
}else
{
tree->left=insertfindTREE(tree->left,cha);
}
return tree;
}
void printTREE(TREE* tree)
{
if(tree!=NULL)
{
printTREE(tree->left);
printTREE(tree->right);
printf("%c\t",tree->ch);
}
}
void printBeforeTREE(TREE* tree)
{
if(tree==NULL)
return ;
printf("%c\t",tree->ch);
printBeforeTREE(tree->left);
printBeforeTREE(tree->right);
}
void printMidTREE(TREE* tree)
{
TREE *p=tree;
STACK * sta =initSTACK();
push(sta,&tree);
while(!isEmpty(sta))
{
while(p)
{
p=p->left;
push(sta,&p);
}
pop(sta,&p);
if(!isEmpty(sta))
{
pop(sta,&p);
printf("%c\t",p->ch);
p=p->right;
push(sta,&p);
}
}
3.main.c
//typedef int ElementType;
//#define ElementType TREE ;
#include "tree.h"
#include "stack.h"
int main()
{
TREE *tree=initTREE();
printTREE(tree);
/*int a;
STACK* sta=initSTACK();
a=2;
push(sta,&a);
a=3;
push(sta,&a);
a=4;
push(sta,&a);
while(!isEmpty(sta))
{pop(sta,&a);
printf("%d\n",a);
}
*/
return 0;
}