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

/*不知道为什么插入之后结果还是和原来一样  请帮下忙 */
2010-11-24 22:09
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册




2010-11-24 22:11
快速回复:二叉树的插入算法
数据加载中...
 
   



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

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