栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【BUCT数据结构类库】7.3--程序题--动态查找——二叉排序树实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【BUCT数据结构类库】7.3--程序题--动态查找——二叉排序树实现

#include
using namespace std;

//二叉排序树
typedef struct Node
{
    int key;
    Node *left;
    Node *right;
}*Tree;

Tree insert(Tree root, int key)
{
    if (root == NULL)
    {
        root = new Node;
        root->key = key;
        root->left = NULL;
        root->right = NULL;
        cout<<"插入函数中的root:    "<key)
        root->left=insert(root->left, key);
    else
        root->right=insert(root->right, key);
}


//输出排序结果
void print(Node *root)
{
    if (root == NULL)
        return;
    print(root->left);
    cout << root->key << " ";
    print(root->right);
}

int main(){
    Tree root = NULL;
    int key;
    cout<<"请输入数据,输入-1结束"<> key)
        if (key != -1)
            {root=insert(root, key); //这里不是每一次都要赋值,因为每次都是指向根节点的指针
            cout<<"主函数里的root:  "< 

运行结果

请输入数据,输入-1结束
1 2 4 24 6 5 4 3 2 55 6 4 3 24 6 -1
插入函数中的root:    0x1c1910
主函数里的root:  0x1c1910    
插入函数中的root:    0x1c1960
主函数里的root:  0x1c1910    
插入函数中的root:    0x1c1d40
主函数里的root:  0x1c1910    
插入函数中的root:    0x1c1d90
主函数里的root:  0x1c1910    
插入函数中的root:    0x1c3ef0
主函数里的root:  0x1c1910    
插入函数中的root:    0x1c3f40
主函数里的root:  0x1c1910    
插入函数中的root:    0x1c3f90
主函数里的root:  0x1c1910    
插入函数中的root:    0x1c3fe0
主函数里的root:  0x1c1910
插入函数中的root:    0x1c4030
主函数里的root:  0x1c1910
插入函数中的root:    0x1c4080
主函数里的root:  0x1c1910
插入函数中的root:    0x1c40d0
主函数里的root:  0x1c1910
插入函数中的root:    0x1c4120
主函数里的root:  0x1c1910
插入函数中的root:    0x1c4170
主函数里的root:  0x1c1910
插入函数中的root:    0x1c41c0
主函数里的root:  0x1c1910
插入函数中的root:    0x1c4210
主函数里的root:  0x1c1910
1 2 2 3 3 4 4 4 5 6 6 6 24 24 55

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/691046.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号