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

源代码:

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. node.js 模块和其下载资源的镜像设置

    以前安装 electron 时总是失败,然后就在淘宝镜像上下载好相应版本的文件放到用户目录来解决问题. 后来研究发现 npm 不仅可以设置 node.js 模块仓库的代理, 同样可以设置像 elect ...

  2. 1.字符串池化(intern)机制及拓展学习

    1.字符串intern机制 用了这么久的python,时刻和字符串打交道,直到遇到下面的情况: a = "hello" b = "hello" print(a ...

  3. datatables之翻页、搜索、排序事件

    $(document).ready(function() { $('#table').on('order.dt', function() { console.log('排序'); }).on('sea ...

  4. JavaScript写入文件到本地

    工作中有时需要通过 JavaScript 保存文件到本地,我们都知道 JavaScript 基于安全的考虑,是不允许直接操作本地文件的.IE 可以通过 VB 插件的方式进行,而 Chrome 和 fi ...

  5. Eclipse 如何修改 Web 项目的名称

    Eclipse 切换到  Navigator 视图,能显现出项目下所有的文件便于修改. 1.修改该项目目录下:.project文件 <projectDescription><name ...

  6. Mautic-2.2.0 (Ubuntu 16.04)

    平台: Ubuntu 类型: 虚拟机镜像 软件包: mautic-2.2.0 business intelligence commercial ecommerce mautic open-source ...

  7. redhat7.3忘记root密码后如何重置root密码

    redhat7系如果忘记root密码,重置密码方法与redhat6系不同! 1.开机启动系统,在grub选择启动内核项时 按‘e’进入编辑模式 2.这时看到的参数并不全,要按上下键滚动显示, 3.在l ...

  8. Mercurial (hg)

    附上两个站点: http://z42.readthedocs.org/zh/latest/devtools/hg.html http://bucunzai.net/hginit/ Mercurial( ...

  9. struts2表单提交Date数据无法接收

    问题:在Struts2环境下,提交含有Date类型数据表单,但是在action中没有接收到:String就可以直接接收到: --网络搜索后,说Struts2可以自己转,但是目前没发现有: 然后在狂搜, ...

  10. win8下使用IIS服务器及自定义服务器端包含模块(SSI)步骤

    配置完过段时间就容易忘记,特此记录. 1.开启IIS服务器. 默认没有安装,需要先安装. 打开控制面板--> 打开“程序和功能”--> 左侧选择“启用或关闭windows功能”--> ...