考研要求掌握的C语言(二叉排序树专题)

考研要求

考二叉排序树的建树、查找、中序遍历代码题,删除在选择题出过,大题可能会考

二叉排序树(二叉搜索树)(二叉查找树)的含义

就是左子树的任意节点 小于根节点 小于右子树的任意节点

二叉排序树的建树

版本1:递归版本

建树的大概流程就是进行比较数据,若比根节点小,就去左子树

若比根节点大,就去右子树

动图示意

核心代码

复制代码
//核心函数:构造二叉排序树,此版本为递归版本
int bst_insert(BTree& root, int val)
{
    //每次新来一个先建一个新节点
    BTree newTree = (BTree)calloc(1, sizeof(Tnode));
    newTree->val = val;
    if (root == NULL)
    {
        root = newTree;
        return 1;//插入成功
    }
    if (val > root->val)
    {
        return bst_insert(root->rc, val);
    }
    else if(val < root->val)
    {
        return bst_insert(root->lc, val);
    }
    else
    {
        return 0;//考研不考相等元素的插入
    }
}

【注】理解递归的宏观思路,就是你相信他能帮你完成你想要的事情以及注意结束条件即可

二叉排序树的中序遍历

和之前二叉树专题的一样,就不再过多赘述了

二叉树专题

复制代码
//中序遍历
void mid_print(BTree t)
{
    if (t)
    {
        mid_print(t->lc);
        printf("%d ", t->val);
        mid_print(t->rc);
    }
}

二叉排序树的查找

查找和建树的思想差不多,若比根节点小,就去左子树

若比根节点大,就去右子树

复制代码
//查找不修改,可以不加引用,既使加引用,下方用了临时指针指向,改变临时指针也不会改变tree
void find_tree(BTree tree,ElemType key)
{
    BTree cur = tree;//临时指针
    while (cur)
    {
        if (key > cur->val)
        {
            //右子树
            cur = cur->rc;
        }
        else if (key < cur->val)
        {
            cur = cur->lc;
        }
        else
        {
            printf("在二叉排序树中找到了%d\n",key);
            break;
        }
    }
}

可运行代码全部

复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
//树结构体
typedef struct tree_node {
    ElemType val;
    struct tree_node* lc;
    struct tree_node* rc;
}Tnode, * BTree;
//注意:考研是会考二叉排序树的建树函数的
//二叉排序树也称为二叉搜索树(二叉查找树)
//特点,左子树都小于根节点,小于右子树
//考中序遍历,查找


//核心函数:构造二叉排序树,此版本为递归版本
int bst_insert(BTree& root, int val)
{
    //每次新来一个先建一个新节点
    BTree newTree = (BTree)calloc(1, sizeof(Tnode));
    newTree->val = val;
    if (root == NULL)
    {
        root = newTree;
        return 1;//插入成功
    }
    if (val > root->val)
    {
        return bst_insert(root->rc, val);
    }
    else if(val < root->val)
    {
        return bst_insert(root->lc, val);
    }
    else
    {
        return 0;//考研不考相等元素的插入
    }
}


void create_bst_tree(BTree &root,int *nums,int len)
{
    for (int i = 0; i < len; ++i)
    {
        bst_insert(root, nums[i]);//核心函数
    }
}
//中序遍历
void mid_print(BTree t)
{
    if (t)
    {
        mid_print(t->lc);
        printf("%d ", t->val);
        mid_print(t->rc);
    }
}

//查找
void find_tree(BTree tree,ElemType key)
{
    BTree cur = tree;
    while (cur)
    {
        if (key > cur->val)
        {
            //右子树
            cur = cur->rc;
        }
        else if (key < cur->val)
        {
            cur = cur->lc;
        }
        else
        {
            printf("在二叉排序树中找到了%d\n",key);
            break;
        }
    }
}



int main()
{
    BTree trees = NULL;
    int nums[] = { 15,25,65,2,89 };
    int len = (int)sizeof(nums) / sizeof(nums[0]);
    create_bst_tree(trees, nums,len);
    mid_print(trees);
    puts("");
    find_tree(trees, 65);
    return 0;
}

代码运行图

代码删除在下节,比较复杂

相关推荐
冬夜戏雪1 小时前
实习面经摘录(九)
学习
arvin_xiaoting1 小时前
OpenClaw学习总结_I_核心架构_8:SessionPruning详解
前端·chrome·学习·系统架构·ai agent·openclaw·sessionpruning
前端摸鱼匠2 小时前
【AI大模型春招面试题11】什么是模型的“涌现能力”(Emergent Ability)?出现条件是什么?
人工智能·算法·ai·自然语言处理·面试·职场和发展
globaldomain3 小时前
什么是用于长距离高速传输的TCP窗口扩展?
开发语言·网络·php
MORE_773 小时前
leecode-合并区间-贪心算法
算法·贪心算法
沈阳信息学奥赛培训3 小时前
#undef 指令 (C/C++)
c语言·开发语言·c++
2401_873204653 小时前
分布式系统安全通信
开发语言·c++·算法
妄汐霜3 小时前
小白学习笔记(spring框架的aop和tx)
笔记·学习
Dxy12393102164 小时前
JS发送请求的方法详解
开发语言·javascript·ecmascript
sw1213894 小时前
C++中的代理模式实战
开发语言·c++·算法