关于二叉树的遍历问题请教
各位大神, 在二叉树中,如何实现二叉树的中序遍历或后序遍历? 我使用的结构体指针建立的二叉树,但是只实现了前序遍历。 中序遍历和后序遍历不知道如何着手? 请各位大神指点下哈,或者给我个参考资料也行! 谢谢!
把建立的二叉树代码加上! 前序遍历是没问题的, 中序遍历和后序遍历该如何处理吶 大神大神帮帮忙
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define TREE_TYPE int
typedef struct my_tree{
TREE_TYPE i;
struct my_tree *right;
struct my_tree *left;
}tree;
static tree *boot;
void create_tree(TREE_TYPE value)
{
tree *tmp = boot; tree *new,*p;
while(tmp != NULL)
{
if(value == tmp->i) { printf("Value already exists!\n");return; }
if(value > tmp->i)
{
new=tmp; tmp = tmp->right;
}
else
{
new=tmp; tmp = tmp->left;
}
}
p = malloc(sizeof(tree)); p->i=value;
if(value > new->i) new->right=p;
else new->left=p;
p->right=NULL;p->left=NULL;
}
void pre_print_tree(tree *tmp)
{
tree *left_left,*right_right;
printf("%d\t",tmp->i);
left_left = tmp; right_right = tmp;
if(left_left->left != NULL)
{ left_left=left_left->left; pre_print_tree(left_left);}
if(right_right->right!= NULL)
{ right_right=right_right->right; pre_print_tree(right_right);}
}
void main()
{
TREE_TYPE j=1;
boot = malloc(sizeof(tree));
printf("Please enter the date of boot:");
scanf("%d",&(boot->i));
boot->right = NULL; boot->left = NULL;
while(j)
{
printf("Please enter the new data:");
scanf("%d",&j);
if(j == 0) continue;
create_tree(j);
}
pre_print_tree(boot); //Preorder Traversal
printf("\n");
}