#include <iostream>
#include <cstring>
using namespace std;
//树的结构体
typedef struct node{
char data[20];
struct node *left, *right;
}*ptree, pnode;
//搜索二叉树的根节点
ptree creategen(char c[])
{
ptree t;
t = (ptree)malloc(sizeof(pnode));
strcpy(t->data,c);
t->left = NULL;
t->right = NULL;
return t;
}
//创建搜索二叉树
void create(ptree root, char c[])
{
ptree temp, t;
t = root;
if (strcmp(t->data,c)==0)
{
printf("你所输入的元素已经存在:\n");
exit(1);
}
if (strcmp(t->data,c)<0)
{
if (t->left == NULL)
{
temp = (ptree)malloc(sizeof(pnode));
strcpy(temp->data, c);;
temp->left = NULL;
temp->right = NULL;
t->left = temp;
}
else
{
create(t->left, c);
}
}
if (strcmp(t->data,c)>0)
{
if (t->right == NULL)
{
temp = (ptree)malloc(sizeof(pnode));
strcpy(temp->data,c);
temp->left = NULL;
temp->right = NULL;
t->right = temp;
}
else
{
create(t->right, c);
}
}
return;
}
//递归先序遍历二叉树
void preorder(ptree t)
{
if (t)
{
printf("%s\n", t->data);
preorder(t->left);
preorder(t->right);
}
}
//递归中序遍历二叉搜索树
void mindorder(ptree t)
{
if (t)
{
mindorder(t->left);
printf("%s\n", t->data);
mindorder(t->right);
}
}
//递归后序遍历二叉搜索树
void aftorder(ptree t)
{
if (t)
{
aftorder(t->left);
aftorder(t->right);
printf("%s\n", t->data);
}
}
int main()
{
ptree bt;
int n;
char ch[20];
printf("创建二叉搜索树的根节点:\n");
scanf("%s", ch);
getchar();
bt = creategen(ch);
printf("输入二叉树的子节点数:\n");
scanf("%d", &n);
getchar();
while (n--)
{
memset(ch,NULL,sizeof(ch));
scanf("%s", ch);
getchar();
create(bt, ch);
}
printf("先序遍历二叉搜索树是:\n");
preorder(bt);
printf("\n");
printf("中序遍历二叉搜索树是:\n");
mindorder(bt);
printf("\n");
printf("后序遍历二叉搜索树是\n");
aftorder(bt);
printf("\n");
return 0;
}
#include <cstring>
using namespace std;
//树的结构体
typedef struct node{
char data[20];
struct node *left, *right;
}*ptree, pnode;
//搜索二叉树的根节点
ptree creategen(char c[])
{
ptree t;
t = (ptree)malloc(sizeof(pnode));
strcpy(t->data,c);
t->left = NULL;
t->right = NULL;
return t;
}
//创建搜索二叉树
void create(ptree root, char c[])
{
ptree temp, t;
t = root;
if (strcmp(t->data,c)==0)
{
printf("你所输入的元素已经存在:\n");
exit(1);
}
if (strcmp(t->data,c)<0)
{
if (t->left == NULL)
{
temp = (ptree)malloc(sizeof(pnode));
strcpy(temp->data, c);;
temp->left = NULL;
temp->right = NULL;
t->left = temp;
}
else
{
create(t->left, c);
}
}
if (strcmp(t->data,c)>0)
{
if (t->right == NULL)
{
temp = (ptree)malloc(sizeof(pnode));
strcpy(temp->data,c);
temp->left = NULL;
temp->right = NULL;
t->right = temp;
}
else
{
create(t->right, c);
}
}
return;
}
//递归先序遍历二叉树
void preorder(ptree t)
{
if (t)
{
printf("%s\n", t->data);
preorder(t->left);
preorder(t->right);
}
}
//递归中序遍历二叉搜索树
void mindorder(ptree t)
{
if (t)
{
mindorder(t->left);
printf("%s\n", t->data);
mindorder(t->right);
}
}
//递归后序遍历二叉搜索树
void aftorder(ptree t)
{
if (t)
{
aftorder(t->left);
aftorder(t->right);
printf("%s\n", t->data);
}
}
int main()
{
ptree bt;
int n;
char ch[20];
printf("创建二叉搜索树的根节点:\n");
scanf("%s", ch);
getchar();
bt = creategen(ch);
printf("输入二叉树的子节点数:\n");
scanf("%d", &n);
getchar();
while (n--)
{
memset(ch,NULL,sizeof(ch));
scanf("%s", ch);
getchar();
create(bt, ch);
}
printf("先序遍历二叉搜索树是:\n");
preorder(bt);
printf("\n");
printf("中序遍历二叉搜索树是:\n");
mindorder(bt);
printf("\n");
printf("后序遍历二叉搜索树是\n");
aftorder(bt);
printf("\n");
return 0;
}