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

源代码:

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. HihoCoder#1509 : 异或排序(二进制)

    题意 题目链接 Sol 挺简单的吧.考虑两个元素什么时候不满足条件 设\(a_i\)与\(a_i + 1\)最高的不同位分别为0 1,显然\(S\)的这一位必须为\(0\),否则这一位必须为\(1\) ...

  2. BZOJ3261: 最大异或和(可持久化trie树)

    题意 题目链接 Sol 设\(sum[i]\)表示\(1 - i\)的异或和 首先把每个询问的\(x \oplus sum[n]\)就变成了询问前缀最大值 可持久化Trie树维护前缀xor,建树的时候 ...

  3. jquery-fullpage插件

    jquery fullpage.js全屏滚动插件/jquery-easing插件 // 前端自动化工具安装插件 在页面引入: <link rel="stylesheet" h ...

  4. JS的定时到底有多不准

    博客逐步迁移到,独立博客,原文地址,http://www.woniubi.cn/js_hide_tab_setinterval/ 我们一直都在说,JS的定时非常的不准确,但是很少有人去验证他,今天我就 ...

  5. selenium中Alter等弹出对话框的处理

    昨天使用selenium做自动化测试,发现部分页面会弹出alert对话框,找了写资料,大概的意思就是要给弹出的对话框做出相应,不然,后续的处理会失败. _driver.SwitchTo().Alert ...

  6. LoadRunner性能测试之常见函数及参数的说明和作用

       

  7. centos6.5_64bit_tomcat7开机自启

    一.创建tomcat脚本 vim /etc/init.d/tomcat 将下面的内容拷到脚本里面 =================================================== ...

  8. BZOJ 3090: Coci2009 [podjela]

    3090: Coci2009 [podjela] Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 23  Solved: 17[Submit][Statu ...

  9. 【CCPC-Wannafly Winter Camp Day3 (Div1) G】排列(水题)

    点此看题面 大致题意:已知 \(p\)为\(n\)的一个排列,定义\(A(p)_i=min_{j=1}^ip_j\),若用\(q_i\)表示\(p\)第\(i\)小的前缀的长度(以值为第一关键字,下标 ...

  10. centos 开启http代理tinyproxy

    一.前言 就算有一些公司想到要进行压力测试也是用一些微软,官网出的一些软件,一个ip发起很多访问.等有一天黑客攻击来了发现还是顶不住.华盟君认为知此知彼才是压力测试的关键点,应当模拟黑客手法进行压力测 ...