|
|
#2
寒风中的细雨2010-11-24 22:09
#include <stdio.h>
#include <malloc.h> #include <stdlib.h> struct TreeNode { struct TreeNode *Lsubtree; //左孩子 char Data; struct TreeNode *Rsubtree; //右孩子 }; void main() { void PreOrderBTree(struct TreeNode*); //中序遍历 struct TreeNode* GreatBTree(struct TreeNode*); //创建二叉树 struct TreeNode* Tree = NULL,*q,*p; int i = 0; char ch; printf("输入节点,例如ABD..E..C.F..\n"); Tree = GreatBTree(Tree); fflush(stdin); //清楚键盘流 q = (struct TreeNode*)malloc(sizeof(struct TreeNode)); //申请空间 printf("输入要插入的节点:\n"); scanf("%c",&ch); fflush(stdin); q ->Data = ch; // 对申请空间之后的q赋值 q->Lsubtree = q->Rsubtree = NULL; p = Tree; //令结构体指针p指向二叉树的根节点 /*while ( i = 0)*/ //循环(ch小于二叉树的DATA则插入二叉树的左孩子,反之插入右孩子) while( i == 0 ) { if(p->Data >ch) //如果二叉树根节点的数据大于需要插入的数ch { if (p->Lsubtree ==NULL) //若左孩子为空,赋值停止循环 { p->Lsubtree = q; i = 1; } else { p = p->Lsubtree; //根节点的左孩子不为空 继续循环 } } else // 如果二叉树根节点的数据小于需要插入的数ch { if (p->Rsubtree == NULL) //根节点的右孩子为空,赋值,停止循环 { p->Rsubtree = q; i = 1; } else { p = p->Rsubtree; //根节点的右孩子不为空,继续循环 } } } PreOrderBTree(Tree); //先序遍历 system("pause"); } struct TreeNode* GreatBTree(struct TreeNode *tree) //创建二叉树 { char ch; ch = getchar(); getchar(); /* if (ch == '\n' ) { return tree; } */if (ch =='.' ) //输入'.'的时候为空树 { tree = NULL; } else { tree = (struct TreeNode *)malloc(sizeof(struct TreeNode)); tree->Data = ch; tree->Lsubtree=GreatBTree(tree ->Lsubtree); tree->Rsubtree=GreatBTree(tree ->Rsubtree); } return tree; } void PreOrderBTree (struct TreeNode* Tree) //先序遍历 { if(Tree != NULL) { printf("%c ",Tree->Data); PreOrderBTree(Tree->Lsubtree); PreOrderBTree(Tree->Rsubtree); } } /*不知道为什么插入之后结果还是和原来一样 请帮下忙 */ |
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct TreeNode
{
struct TreeNode *Lsubtree; //左孩子
char Data;
struct TreeNode *Rsubtree; //右孩子
};
void main()
{
void PreOrderBTree(struct TreeNode*); //中序遍历
struct TreeNode* GreatBTree(struct TreeNode*); //创建二叉树
struct TreeNode* Tree = NULL,*q,*p;
int i;
char ch;
printf("输入节点,例如ABD..E..C.F..\n");
Tree = GreatBTree(Tree);
fflush(stdin); //清楚键盘流
q = (struct TreeNode*)malloc(sizeof(struct TreeNode)); //申请空间
printf("输入要插入的节点:\n");
scanf("%c",&ch);
fflush(stdin);
q ->Data = ch; // 对申请空间之后的q赋值
q->Lsubtree = q->Rsubtree = NULL;
p = Tree; //令结构体指针p指向二叉树的根节点
while ( i = 0) //循环(ch小于二叉树的DATA则插入二叉树的左孩子,反之插入右孩子)
{
if(p->Data >ch) //如果二叉树根节点的数据大于需要插入的数ch
{
if (p->Lsubtree ==NULL) //若左孩子为空,赋值停止循环
{
p->Lsubtree = q;
i = 1;
}
else
{
p = p->Lsubtree; //根节点的左孩子不为空 继续循环
}
}
else // 如果二叉树根节点的数据小于需要插入的数ch
{
if (p->Rsubtree == NULL) //根节点的右孩子为空,赋值,停止循环
{
p->Rsubtree = q;
i = 1;
}
else
{
p = p->Rsubtree; //根节点的右孩子不为空,继续循环
}
}
}
PreOrderBTree(Tree); //先序遍历
system("pause");
}
struct TreeNode* GreatBTree(struct TreeNode *tree) //创建二叉树
{
char ch;
ch = getchar();
if (ch == '\n' )
{
return tree;
}
else if (ch =='.' ) //输入'.'的时候为空树
{
tree = NULL;
}
else
{
tree = (struct TreeNode *)malloc(sizeof(struct TreeNode));
tree->Data = ch;
tree->Lsubtree=GreatBTree(tree ->Lsubtree);
tree->Rsubtree=GreatBTree(tree ->Rsubtree);
}
return tree;
}
void PreOrderBTree (struct TreeNode* Tree) //先序遍历
{
if(Tree != NULL)
{
printf("%c ",Tree->Data);
PreOrderBTree(Tree->Lsubtree);
PreOrderBTree(Tree->Rsubtree);
}
}
不知道为什么插入之后结果还是和原来一样 请帮下忙