二叉查找树(BST)专题

news/2024/9/23 9:27:04/

二叉查找树专题

    • 二叉查找树的基本操作
        • 查找
        • 插入
        • 删除
    • 二叉查找树的性质

代码来源:晴神《算法笔记》!!

二叉查找树的基本操作

查找

void search(node* root, int x){if(root == NULL){printf("search failed\n");return;}if(root->data == x)printf("%d", x);else if(x < root->data)search(root->lchild, x)elsesearch(root->rchild, x);	
} 

插入

void insert(node* &root, int x){if(root == NULL){root = newnode(x);return;}if(root->data == x){  //结点已经存在 return;}else if(x < root->data)insert(root->lchild, x);else insert(root->rchild, x);
}

删除

node* findmax(node* root){  //直接前驱 while(root->rchild != NULL){root = root->rchild;}return root;
} 
node* findmin(node* root){  //直接后继 while(root->lchild != NULL){root = root->lchild;}return root;
}
void deletenode(node* &root, int x){   //加引号 if(root == NULL){return;}if(root->data == x){if(root->lchild == NULL && root->rchild == NULL)root = NULL;else if(root->lchild != NULL){node* pre = findmax(root->lchild);root->data = pre->data;deletenode(root->lchild, pre->data);}else{node* next = findmin(root->rchild);root->data = next->data;deletenode(root->rchild, next->data);}}else if(x < root->data)deletenode(root->lchild, x);else deletenode(root->rchild, x);
}

二叉查找树的性质

对二叉查找树进行中序遍历,遍历的结果是有序的。


http://www.ppmy.cn/news/289326.html

相关文章

1086 Tree Traversals Again (25分)

1 题目 1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations a…

HDU 6030(矩阵快速幂+规律)

传送门 题目描述&#xff1a; Happy Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1146 Accepted Submission(s): 491 Problem Description Little Q wants to buy a necklace for his girlfriend…

IntelliJ IDEA pycharm webstorm 激活

前端&#xff0c;后端&#xff0c;爬虫全套激活&#xff1a; IntelliJ IDEA 2017.2.5 破解 激活 http://xidea.online/?/download/intellij-idea 最新的pycharm 破解 激活 http://www.imsxm.com/jetbrains-license-server.html http://www.imsxm.com 最新的webstorm破解 激活 …

开博寄语

在这个特殊的日子开通了博客&#xff0c;今天是阴历的中元节&#xff0c;又是我的生日&#xff0c;已届不惑&#xff0c;混迹IT20多年&#xff0c;欢喜过、失落过、迷惘过&#xff0c;在这里开一片小小的天地&#xff0c;记录自己的心路历程&#xff0c;与有缘人共勉&#xff0…

1068 Find More Coins (30分)

文章目录 1 题目2 解析2.1 题意2.2 思路 3 参考代码 1 题目 1068 Find More Coins (30分) Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds o…

听音室-HIFI入门之400多张发烧碟中选出的精品

折腾这些年&#xff0c;也算对这个Hi-Fi,即所谓的音响发烧有些认识&#xff0c;这里大谈得比较多的是音响器材&#xff0c;Hi-end器材动辄几十万&#xff0c;有的上百万&#xff0c;为什么我们花这么多精力和金钱的投入&#xff1f;是因为高保真的音乐才是我们追求的本质&#…

pcm5102a解码芯片音质评测_WHAT HIFI推荐2020年最值得购买解码器:11款器材上榜

下面是《What Hi-Fi》推荐的2020年最值得购买解码器,来看看有哪些上榜器材吧! 一、和弦Chord Qutest便携式解码器 查看 和弦Chord Qutest便携式解码器 评测>> 目前该产品有“HIFI说影音城”商家在售,点击查看>> 口碑评说: “听完了上述曲目,我可以明显感受到Q…

[zz] 高端HIFI发烧音频DAC解码芯片排名

音频“解码器”中最核心、重要的器件&#xff0c;无非就是“解码”&#xff08;DAC&#xff0c;数模转换&#xff09;芯片了&#xff0c;大家常常很关注音频DAC芯片的选用&#xff0c;也热衷于对其优劣的讨论。 本文尝试对当前最优秀的高端音频DAC芯片的结构、技术和性能等做简…