| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1112 人关注过本帖
标题:二叉树的插入算法
取消只看楼主 加入收藏
kidangel666
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:93
专家分:139
注 册:2010-9-15
结帖率:78.95%
收藏
已结贴  问题点数:20 回复次数:0 
二叉树的插入算法
#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);

    }
}

不知道为什么插入之后结果还是和原来一样  请帮下忙
搜索更多相关主题的帖子: 二叉树 算法 
2010-11-23 11:25
快速回复:二叉树的插入算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025289 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved