C++ 二叉搜索树
二叉搜索树利用其特有的二叉树性质,使其搜索更方便
源代码:
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++ 二叉搜索树的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [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 ...
- [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 ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [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 ...
- [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. 这道 ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [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 ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- #include stdio.h(4)
#include <stdio.h> int main() { //****************1.数组*************** //什么是数组:专门用来存放数据的 /* 格式 ...
- angular的多个模块执行 angular里 字符串与对象的互转
1.disable : true ,禁用 2.$timeout 计时器 $interval.cancel(timer); 3.app.run(); 可以不使用控制器就开启数据,但适合$rootsco ...
- 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面
我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...
- Linux安装 NTFS 支持
my system kernel Version is 2.6.18-128.el5 在这个地址下载了 kernel-module-ntfs-2.6.18-128.el5-2.1.27-0.rr.10 ...
- Udacity carnd2 Sensor Fusion, Extended Karman Filter (English)
Extended Karman Filter Zhenglei 2018 January This is a project to estimate the car position from Lid ...
- MHA故障切换方案
mha故障切换当master 挂掉后会自动切换到slave01上去,可以看 tailf /var/mha_log/manager.log4个问题:1,切换后且原master01mysql服务启动OK, ...
- framework7 可以拉动右侧工具栏和点击当前item就可以出发事件的HTML结构
<li class="swipeout"> <div class="swipeout-content item-content"> &l ...
- Android(java)学习笔记66:Android Studio中build.gradle简介
1.首先我们直接上代码介绍: // Top-level build file where you can add configuration options common to all sub-pro ...
- P1266 速度限制
P1266 速度限制 第一次接触这种分层spfa 类似于dp 个人理解 #include<cstdio> #include<iostream> #include<algo ...
- 字节流, FileOutputStream类,FileInputStream类,复制文件,字符流
字节输出流OutputStream OutputStream此抽象类,是表示输出字节流的所有类的超类.操作的数据都是字节 基本方法: 子类可继承调用以上方法 FileOutputStream类 构造方 ...