二叉搜索树利用其特有的二叉树性质,使其搜索更方便

源代码:

struct node {
int val;
node *left, *right;
}; //the function of insert
node *insert(node *n, int key) {
if (n == NULL) {
node *t = new node;
t->val = key;
t->left = t->right = NULL;
return t;
}
else {
if (key < n->val) n->left = insert(n->left, key);
else n->right = insert(n->right, key);
return n;
}
}
//the function of find_key
bool find(node *n, int key) {
if (n == NULL) return false;
else if (key == n->val) return true;
else if (key > n->val) return find(n->right, key);
else return find(n->left, key);
} //the function of remove
node *remove(node *n, int key) {
if (n == NULL) return NULL;
else if (key < n->val) n->left = remove(n->left, key);
else if (key > n->val) n->right = remove(n->right, key);
else if (n->left == NULL) {
node *q = n->right;
delete n;
return q;
}
else if (n->left->right == NULL) {
node *q = n->left;
q->right = n->right;
delete n;
return q;
}
else {
node *q;
for (q = n->left; q->right->right != NULL; q = q->right);
node *r = q->right;
q->right = r->left;
r->left = n->left;
r->right = n->right;
delete n;
return r;
}
return n;
}

  利用STL实现

C++ 二叉搜索树的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  4. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  5. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  6. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  7. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  8. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  10. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. better-scroll 遇到的问题 2

    问题的描述: 在歌曲列表页面使用了scroll插件,搜索了很多歌曲,页面出现滚动,选择播放一首歌曲,弹出播放器,将播放器最小化,页面回到歌曲列表,并且页面的底部出播放歌曲的信息(在没有播放歌曲的时候是 ...

  2. laravel下的ORM数据映射之自由畅想

    此处以Model::get()方法和Model::first()方法为例 public static function get($data=[]){//默认是空数组 if(count($data)== ...

  3. ComponentOne、Spread、ActiveReports 5折起 加入惊喜惠

    慧都十周年,GrapeCity也来共襄盛举,旗下三大产品产品线齐齐参与.界面控件套包ComponentOne.Excel表格控件Spread与报表开发工具ActiveReports,指定授权5折起加入 ...

  4. StringBuffer和StringBuilder区别?

    1. String是不可变类,改变String变量中的值,相当于开辟了新的空间存放新的string变量 2. StringBuffer 可变的类,可以通过append方法改变变量的值,且StringB ...

  5. Oracle VM VirtualBox 共享文件夹设置

    在Windows平台下,这货完全没有VMware好用,但在Linux平台就很好用. 学校机房的电脑打开虚拟机就不能插优盘,一插优盘就卡死,所以,只好用共享文件夹了. 1.在虚拟机外部新建一个文件夹 假 ...

  6. CFG的定义

    最近在CMU上NLP,好吧 对于见了很多年的CFG(Context-Free Grammar)发现又搞不懂是什么了 教材上写的是: mathematical system for modeling c ...

  7. IOS ScrollView的使用 and delegate

    ScrollView常用的属性设置 //设置内容尺寸 // CGFloat contentH=self.lastBtn.frame // .origin.y+self.lastBtn.frame.si ...

  8. POJ-2377 Bad Cowtractors---最大生成树

    题目链接: https://vjudge.net/problem/POJ-2377 题目大意: 给一个图,求最大生成树权值,如果不连通输出-1 思路: kruskal算法变形,sort按边从大到小排序 ...

  9. LA 3902 网络

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  10. gearman安装实录

    花了5个小时装好了gearman,问题不断,坑爹的服务器yum还坏了,悲催. 服务器系统:centos5.3 64位 gearman版本:1.1.8 安装包(相关依赖)下载 1.gearman安装包 ...