mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
修改排序算法,添加插值、斐波那契、哈希、二叉树、红黑树、2-3树、B/B+树查找算法
This commit is contained in:
parent
91d24f13d0
commit
296d3cf894
30
Algorithm/BSTSearch.h
Normal file
30
Algorithm/BSTSearch.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
二叉搜索树的查找算法:
|
||||
|
||||
在二叉搜索树b中查找x的过程为:
|
||||
|
||||
1. 若b是空树,则搜索失败,否则:
|
||||
2. 若x等于b的根节点的数据域之值,则查找成功;否则:
|
||||
3. 若x小于b的根节点的数据域之值,则搜索左子树;否则:
|
||||
4. 查找右子树。
|
||||
|
||||
*/
|
||||
|
||||
// 在根指针T所指二叉查找树中递归地查找其关键字等于key的数据元素,若查找成功,
|
||||
// 则指针p指向該数据元素节点,并返回TRUE,否则指针指向查找路径上访问的最终
|
||||
// 一个节点并返回FALSE,指针f指向T的双亲,其初始调用值为NULL
|
||||
Status SearchBST(BiTree T, KeyType key, BiTree f, BiTree &p){
|
||||
|
||||
if(!T) { //查找不成功
|
||||
p=f;
|
||||
return false;
|
||||
}
|
||||
else if (key == T->data.key) { //查找成功
|
||||
p=T;
|
||||
return true;
|
||||
}
|
||||
else if (key < T->data.key) //在左子树中继续查找
|
||||
return SearchBST(T->lchild, key, T, p);
|
||||
else //在右子树中继续查找
|
||||
return SearchBST(T->rchild, key, T, p);
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
// 蛮力字符串匹配
|
||||
int BruteForceStringMatch(vector<char>& Total, vector<char>& Part)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < Total.size() - Part.size(); ++i) {
|
||||
j = 0;
|
||||
while (j < Part.size() && Part[j] == Total[i + j]) {
|
||||
++j;
|
||||
if (j == Part.size())
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
71
Algorithm/FibonacciSearch.cpp
Normal file
71
Algorithm/FibonacciSearch.cpp
Normal file
|
@ -0,0 +1,71 @@
|
|||
// 斐波那契查找
|
||||
|
||||
#include "stdafx.h"
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
const int max_size=20;//斐波那契数组的长度
|
||||
|
||||
/*构造一个斐波那契数组*/
|
||||
void Fibonacci(int * F)
|
||||
{
|
||||
F[0]=0;
|
||||
F[1]=1;
|
||||
for(int i=2;i<max_size;++i)
|
||||
F[i]=F[i-1]+F[i-2];
|
||||
}
|
||||
|
||||
/*定义斐波那契查找法*/
|
||||
int FibonacciSearch(int *a, int n, int key) //a为要查找的数组,n为要查找的数组长度,key为要查找的关键字
|
||||
{
|
||||
int low=0;
|
||||
int high=n-1;
|
||||
|
||||
int F[max_size];
|
||||
Fibonacci(F);//构造一个斐波那契数组F
|
||||
|
||||
int k=0;
|
||||
while(n>F[k]-1)//计算n位于斐波那契数列的位置
|
||||
++k;
|
||||
|
||||
int * temp;//将数组a扩展到F[k]-1的长度
|
||||
temp=new int [F[k]-1];
|
||||
memcpy(temp,a,n*sizeof(int));
|
||||
|
||||
for(int i=n;i<F[k]-1;++i)
|
||||
temp[i]=a[n-1];
|
||||
|
||||
while(low<=high)
|
||||
{
|
||||
int mid=low+F[k-1]-1;
|
||||
if(key<temp[mid])
|
||||
{
|
||||
high=mid-1;
|
||||
k-=1;
|
||||
}
|
||||
else if(key>temp[mid])
|
||||
{
|
||||
low=mid+1;
|
||||
k-=2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(mid<n)
|
||||
return mid; //若相等则说明mid即为查找到的位置
|
||||
else
|
||||
return n-1; //若mid>=n则说明是扩展的数值,返回n-1
|
||||
}
|
||||
}
|
||||
delete [] temp;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int a[] = {0,16,24,35,47,59,62,73,88,99};
|
||||
int key=100;
|
||||
int index=FibonacciSearch(a,sizeof(a)/sizeof(int),key);
|
||||
cout<<key<<" is located at:"<<index;
|
||||
return 0;
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
|
||||
## 文件(文本)查找
|
||||
|
||||
### 代码
|
||||
|
||||
[文件(文本)查找代码](search.cpp)
|
||||
|
||||
### 功能说明
|
||||
|
||||
本程序实现对英文文本中关键字的查找
|
||||
|
||||
返回关键字出现的位置(第几个词)
|
||||
|
||||
### 代码简述
|
||||
|
||||
从`output.txt`文件读入数据(英文文本)到 `vector<string>` 中存储
|
||||
|
||||
通过用户输入关键字(`keyword`)进行查找
|
||||
|
||||
输出查找到的关键字出现的位置(第几个词)
|
|
@ -1,5 +0,0 @@
|
|||
Just arrived in Kathmandu tonight and needed to eat,
|
||||
we checked Trip advisor and came here because it was close to hotel.
|
||||
Certainly deserves its number 1 rating.
|
||||
Food is so tasty and the atmosphere is very relaxed.
|
||||
I would definitely recommend.
|
|
@ -1,105 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define OK 0
|
||||
#define ERROR -1
|
||||
#define INPUTNAME "input.txt"
|
||||
|
||||
// 查找
|
||||
int search(vector<string>& v_data, string keyword) {
|
||||
int i = 0;
|
||||
|
||||
// 遍历每个元素直到找到关键字
|
||||
for (; i < v_data.size(); i++) {
|
||||
|
||||
// 比较关键字与每个词是否相等
|
||||
if (0 == v_data[i].compare(keyword)){
|
||||
|
||||
// 相等则返回关键字的位序
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
|
||||
// 文件读取
|
||||
int FileRead(vector<string>& v_data) {
|
||||
|
||||
ifstream f_in(INPUTNAME);
|
||||
|
||||
//判断读取失败
|
||||
if (!f_in) {
|
||||
cout << "文件读取失败!" << endl;
|
||||
system("pause");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
string word;
|
||||
|
||||
// 文件逐个读取
|
||||
while (!f_in.eof()) {
|
||||
f_in >> word;
|
||||
v_data.push_back(word);
|
||||
}
|
||||
|
||||
// 关闭文件
|
||||
f_in.close();
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
// 界面
|
||||
void Interface(string& keyword) {
|
||||
cout << "-------------------- 文件查找 --------------------" << endl;
|
||||
cout << "【说明】:本程序实现对文件内容的查找" << endl;
|
||||
cout << "即从input.txt读入,查找关键字的首次出现位置" << endl;
|
||||
cout << "--------------------------------------------------" << endl;
|
||||
cout << "请输入关键字:" << endl;
|
||||
cout << "--------------------------------------------------" << endl;
|
||||
cin >> keyword;
|
||||
cout << "--------------------------------------------------" << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
vector<string> v_data;
|
||||
|
||||
// 文件读取
|
||||
if (ERROR == FileRead(v_data))
|
||||
return ERROR;
|
||||
|
||||
int index = -1;
|
||||
string keyword;
|
||||
|
||||
// 界面
|
||||
Interface(keyword);
|
||||
|
||||
// 合法性检测
|
||||
if (keyword.empty()) {
|
||||
cout << "请输入合法的关键字!" << endl;
|
||||
system("pause");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
// 查找
|
||||
index = search(v_data, keyword);
|
||||
|
||||
//未找到输出
|
||||
if (ERROR == index) {
|
||||
cout << "未找到此关键字!" << endl;
|
||||
system("pause");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
//输出找到的关键字索引
|
||||
cout << "此关键字位于第 " << index + 1 << " 个词的位置!" << endl;
|
||||
|
||||
system("pause");
|
||||
|
||||
return OK;
|
||||
}
|
Binary file not shown.
|
@ -1,24 +0,0 @@
|
|||
|
||||
## 文件(文本)排序
|
||||
|
||||
### 代码
|
||||
|
||||
[文件(文本)排序代码](sort.cpp)
|
||||
|
||||
### 功能说明
|
||||
|
||||
本程序实现选择排序和冒泡排序两个排序算法
|
||||
|
||||
并且有从小到大和从大到小两种排序方式
|
||||
|
||||
用户可进行选择需要的方式
|
||||
|
||||
### 代码简述
|
||||
|
||||
从 `output.txt` 文件读入数据(数字)到 `vector<int>` 中存储
|
||||
|
||||
通过用户输入的排序算法(`i_algorithm`)和排序方式(`i_mode`)
|
||||
|
||||
选择对于的选择排序(`SelectSort()`)或者冒泡排序(`BubbleSort()`)进行排序
|
||||
|
||||
排序后输出 `vector<int>` 到 `output.txt`
|
|
@ -1 +0,0 @@
|
|||
33 22 66 99 11 68 39 89 107 749 20 6
|
|
@ -1 +0,0 @@
|
|||
6 11 20 22 33 39 66 68 89 99 107 749
|
|
@ -1,194 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define OK 0
|
||||
#define ERROR -1
|
||||
#define INPUTNAME "input.txt"
|
||||
#define OUTPUTNAME "output.txt"
|
||||
|
||||
// 选择排序
|
||||
int SelectSort(vector<int>& v_data, int b_mode) {
|
||||
|
||||
size_t i_num = v_data.size(), temp = 0;
|
||||
|
||||
// 从小到大排
|
||||
if (0 == b_mode) {
|
||||
size_t min;
|
||||
for (size_t i = 0; i < i_num - 1; i++) {
|
||||
min = i;
|
||||
for (size_t j = i + 1; j < i_num; j++)
|
||||
if (v_data[min] > v_data[j])
|
||||
min = j;
|
||||
if (min != i) {
|
||||
temp = v_data[min];
|
||||
v_data[min] = v_data[i];
|
||||
v_data[i] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 从大到小排
|
||||
else {
|
||||
size_t max;
|
||||
for (size_t i = 0; i < i_num - 1; i++) {
|
||||
max = i;
|
||||
for (size_t j = i + 1; j < i_num; j++)
|
||||
if (v_data[max] < v_data[j])
|
||||
max = j;
|
||||
if (max != i) {
|
||||
temp = v_data[max];
|
||||
v_data[max] = v_data[i];
|
||||
v_data[i] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
// 冒泡排序
|
||||
int BubbleSort(vector<int>& v_data, int b_mode) {
|
||||
|
||||
size_t num = v_data.size(), temp = 0;
|
||||
|
||||
// 从小到大排
|
||||
if (0 == b_mode) {
|
||||
for (size_t i = 0; i < num - 1; i++) {
|
||||
for (size_t j = 0; j < num - i - 1; j++) {
|
||||
if (v_data[j] > v_data[j + 1]) {
|
||||
temp = v_data[j];
|
||||
v_data[j] = v_data[j + 1];
|
||||
v_data[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 从大到小排
|
||||
else {
|
||||
for (size_t i = 0; i < num - 1; i++) {
|
||||
for (size_t j = 0; j < num - i - 1; j++) {
|
||||
if (v_data[j] < v_data[j + 1]) {
|
||||
temp = v_data[j];
|
||||
v_data[j] = v_data[j + 1];
|
||||
v_data[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
// 文件读取
|
||||
int FileRead(vector<int>& v_data) {
|
||||
|
||||
ifstream f_in(INPUTNAME);
|
||||
|
||||
//判断读取失败
|
||||
if (!f_in) {
|
||||
cout << "文件读取失败!" << endl;
|
||||
system("pause");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
int i_temp;
|
||||
|
||||
// 文件逐个读取
|
||||
while (!f_in.eof()) {
|
||||
f_in >> i_temp;
|
||||
v_data.push_back(i_temp);
|
||||
}
|
||||
|
||||
// 关闭文件
|
||||
f_in.close();
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
// 文件写入
|
||||
int FileWrite(vector<int>& v_data) {
|
||||
|
||||
ofstream f_out(OUTPUTNAME);
|
||||
|
||||
// 判断读取失败
|
||||
if (!f_out) {
|
||||
cout << "文件写入失败!" << endl;
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
// 文件逐个写入
|
||||
for (int i = 0; i < v_data.size(); i++)
|
||||
f_out << v_data[i] << " ";
|
||||
|
||||
f_out.close();
|
||||
return OK;
|
||||
}
|
||||
|
||||
// 界面
|
||||
void Interface(int& i_algorithm, int& i_mode) {
|
||||
cout << "-------------------- 文件排序 --------------------" << endl;
|
||||
cout << "【说明】:本程序实现对文件内容的排序" << endl;
|
||||
cout << "即从input.txt读入,排序后写入到output.txt" << endl;
|
||||
cout << "--------------------------------------------------" << endl;
|
||||
cout << "请选择排序算法:" << endl;
|
||||
cout << "【0】选择排序" << endl;
|
||||
cout << "【1】冒泡排序" << endl;
|
||||
cout << "--------------------------------------------------" << endl;
|
||||
cin >> i_algorithm;
|
||||
cout << "--------------------------------------------------" << endl;
|
||||
cout << "请选择排序方式:" << endl;
|
||||
cout << "【0】从小到大" << endl;
|
||||
cout << "【1】从大到小" << endl;
|
||||
cout << "--------------------------------------------------" << endl;
|
||||
cin >> i_mode;
|
||||
cout << "--------------------------------------------------" << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
vector<int> v_data;
|
||||
|
||||
// 文件读取
|
||||
if (ERROR == FileRead(v_data))
|
||||
return ERROR;
|
||||
|
||||
int i_algorithm, i_mode;
|
||||
|
||||
// 界面
|
||||
Interface(i_algorithm, i_mode);
|
||||
|
||||
// 排序算法选择检测
|
||||
if (0 != i_algorithm && 1 != i_algorithm) {
|
||||
cout << "排序算法选择错误!" << endl;
|
||||
system("pause");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
// 排序方式选择检测
|
||||
if (0 != i_mode && 1 != i_mode) {
|
||||
cout << "排序方式选择错误!" << endl;
|
||||
system("pause");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
// 排序
|
||||
if (i_algorithm)
|
||||
BubbleSort(v_data, i_mode);
|
||||
else
|
||||
SelectSort(v_data, i_mode);
|
||||
|
||||
// 文件写入
|
||||
if (ERROR == FileWrite(v_data))
|
||||
return ERROR;
|
||||
|
||||
cout << "排序完成,数据已写入:" << OUTPUTNAME << endl;
|
||||
cout << "--------------------------------------------------" << endl;
|
||||
|
||||
system("pause");
|
||||
|
||||
return OK;
|
||||
}
|
Binary file not shown.
11
Algorithm/InsertionSearch.h
Normal file
11
Algorithm/InsertionSearch.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
//插值查找
|
||||
int InsertionSearch(int a[], int value, int low, int high)
|
||||
{
|
||||
int mid = low+(value-a[low])/(a[high]-a[low])*(high-low);
|
||||
if(a[mid]==value)
|
||||
return mid;
|
||||
if(a[mid]>value)
|
||||
return InsertionSearch(a, value, low, mid-1);
|
||||
if(a[mid]<value)
|
||||
return InsertionSearch(a, value, mid+1, high);
|
||||
}
|
330
DataStructure/RedBlackTree.cpp
Normal file
330
DataStructure/RedBlackTree.cpp
Normal file
|
@ -0,0 +1,330 @@
|
|||
#define BLACK 1
|
||||
#define RED 0
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class bst {
|
||||
private:
|
||||
|
||||
struct Node {
|
||||
int value;
|
||||
bool color;
|
||||
Node *leftTree, *rightTree, *parent;
|
||||
|
||||
Node() : value(0), color(RED), leftTree(NULL), rightTree(NULL), parent(NULL) { }
|
||||
|
||||
Node* grandparent() {
|
||||
if(parent == NULL){
|
||||
return NULL;
|
||||
}
|
||||
return parent->parent;
|
||||
}
|
||||
|
||||
Node* uncle() {
|
||||
if(grandparent() == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if(parent == grandparent()->rightTree)
|
||||
return grandparent()->leftTree;
|
||||
else
|
||||
return grandparent()->rightTree;
|
||||
}
|
||||
|
||||
Node* sibling() {
|
||||
if(parent->leftTree == this)
|
||||
return parent->rightTree;
|
||||
else
|
||||
return parent->leftTree;
|
||||
}
|
||||
};
|
||||
|
||||
void rotate_right(Node *p){
|
||||
Node *gp = p->grandparent();
|
||||
Node *fa = p->parent;
|
||||
Node *y = p->rightTree;
|
||||
|
||||
fa->leftTree = y;
|
||||
|
||||
if(y != NIL)
|
||||
y->parent = fa;
|
||||
p->rightTree = fa;
|
||||
fa->parent = p;
|
||||
|
||||
if(root == fa)
|
||||
root = p;
|
||||
p->parent = gp;
|
||||
|
||||
if(gp != NULL){
|
||||
if(gp->leftTree == fa)
|
||||
gp->leftTree = p;
|
||||
else
|
||||
gp->rightTree = p;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void rotate_left(Node *p){
|
||||
if(p->parent == NULL){
|
||||
root = p;
|
||||
return;
|
||||
}
|
||||
Node *gp = p->grandparent();
|
||||
Node *fa = p->parent;
|
||||
Node *y = p->leftTree;
|
||||
|
||||
fa->rightTree = y;
|
||||
|
||||
if(y != NIL)
|
||||
y->parent = fa;
|
||||
p->leftTree = fa;
|
||||
fa->parent = p;
|
||||
|
||||
if(root == fa)
|
||||
root = p;
|
||||
p->parent = gp;
|
||||
|
||||
if(gp != NULL){
|
||||
if(gp->leftTree == fa)
|
||||
gp->leftTree = p;
|
||||
else
|
||||
gp->rightTree = p;
|
||||
}
|
||||
}
|
||||
|
||||
void inorder(Node *p){
|
||||
if(p == NIL)
|
||||
return;
|
||||
|
||||
if(p->leftTree)
|
||||
inorder(p->leftTree);
|
||||
|
||||
cout << p->value << " ";
|
||||
|
||||
if(p->rightTree)
|
||||
inorder(p->rightTree);
|
||||
}
|
||||
|
||||
string outputColor (bool color) {
|
||||
return color ? "BLACK" : "RED";
|
||||
}
|
||||
|
||||
Node* getSmallestChild(Node *p){
|
||||
if(p->leftTree == NIL)
|
||||
return p;
|
||||
return getSmallestChild(p->leftTree);
|
||||
}
|
||||
|
||||
bool delete_child(Node *p, int data){
|
||||
if(p->value > data){
|
||||
if(p->leftTree == NIL){
|
||||
return false;
|
||||
}
|
||||
return delete_child(p->leftTree, data);
|
||||
} else if(p->value < data){
|
||||
if(p->rightTree == NIL){
|
||||
return false;
|
||||
}
|
||||
return delete_child(p->rightTree, data);
|
||||
} else if(p->value == data){
|
||||
if(p->rightTree == NIL){
|
||||
delete_one_child (p);
|
||||
return true;
|
||||
}
|
||||
Node *smallest = getSmallestChild(p->rightTree);
|
||||
swap(p->value, smallest->value);
|
||||
delete_one_child (smallest);
|
||||
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void delete_one_child(Node *p){
|
||||
Node *child = p->leftTree == NIL ? p->rightTree : p->leftTree;
|
||||
if(p->parent == NULL && p->leftTree == NIL && p->rightTree == NIL){
|
||||
p = NULL;
|
||||
root = p;
|
||||
return;
|
||||
}
|
||||
|
||||
if(p->parent == NULL){
|
||||
delete p;
|
||||
child->parent = NULL;
|
||||
root = child;
|
||||
root->color = BLACK;
|
||||
return;
|
||||
}
|
||||
|
||||
if(p->parent->leftTree == p){
|
||||
p->parent->leftTree = child;
|
||||
} else {
|
||||
p->parent->rightTree = child;
|
||||
}
|
||||
child->parent = p->parent;
|
||||
|
||||
if(p->color == BLACK){
|
||||
if(child->color == RED){
|
||||
child->color = BLACK;
|
||||
} else
|
||||
delete_case (child);
|
||||
}
|
||||
|
||||
delete p;
|
||||
}
|
||||
|
||||
void delete_case(Node *p){
|
||||
if(p->parent == NULL){
|
||||
p->color = BLACK;
|
||||
return;
|
||||
}
|
||||
if(p->sibling()->color == RED) {
|
||||
p->parent->color = RED;
|
||||
p->sibling()->color = BLACK;
|
||||
if(p == p->parent->leftTree)
|
||||
rotate_left(p->sibling());
|
||||
else
|
||||
rotate_right(p->sibling());
|
||||
}
|
||||
if(p->parent->color == BLACK && p->sibling()->color == BLACK
|
||||
&& p->sibling()->leftTree->color == BLACK && p->sibling()->rightTree->color == BLACK) {
|
||||
p->sibling()->color = RED;
|
||||
delete_case(p->parent);
|
||||
} else if(p->parent->color == RED && p->sibling()->color == BLACK
|
||||
&& p->sibling()->leftTree->color == BLACK && p->sibling()->rightTree->color == BLACK) {
|
||||
p->sibling()->color = RED;
|
||||
p->parent->color = BLACK;
|
||||
} else {
|
||||
if(p->sibling()->color == BLACK) {
|
||||
if(p == p->parent->leftTree && p->sibling()->leftTree->color == RED
|
||||
&& p->sibling()->rightTree->color == BLACK) {
|
||||
p->sibling()->color = RED;
|
||||
p->sibling()->leftTree->color = BLACK;
|
||||
rotate_right(p->sibling()->leftTree);
|
||||
} else if(p == p->parent->rightTree && p->sibling()->leftTree->color == BLACK
|
||||
&& p->sibling()->rightTree->color == RED) {
|
||||
p->sibling()->color = RED;
|
||||
p->sibling()->rightTree->color = BLACK;
|
||||
rotate_left(p->sibling()->rightTree);
|
||||
}
|
||||
}
|
||||
p->sibling()->color = p->parent->color;
|
||||
p->parent->color = BLACK;
|
||||
if(p == p->parent->leftTree){
|
||||
p->sibling()->rightTree->color = BLACK;
|
||||
rotate_left(p->sibling());
|
||||
} else {
|
||||
p->sibling()->leftTree->color = BLACK;
|
||||
rotate_right(p->sibling());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void insert(Node *p, int data){
|
||||
if(p->value >= data){
|
||||
if(p->leftTree != NIL)
|
||||
insert(p->leftTree, data);
|
||||
else {
|
||||
Node *tmp = new Node();
|
||||
tmp->value = data;
|
||||
tmp->leftTree = tmp->rightTree = NIL;
|
||||
tmp->parent = p;
|
||||
p->leftTree = tmp;
|
||||
insert_case (tmp);
|
||||
}
|
||||
} else {
|
||||
if(p->rightTree != NIL)
|
||||
insert(p->rightTree, data);
|
||||
else {
|
||||
Node *tmp = new Node();
|
||||
tmp->value = data;
|
||||
tmp->leftTree = tmp->rightTree = NIL;
|
||||
tmp->parent = p;
|
||||
p->rightTree = tmp;
|
||||
insert_case (tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void insert_case(Node *p){
|
||||
if(p->parent == NULL){
|
||||
root = p;
|
||||
p->color = BLACK;
|
||||
return;
|
||||
}
|
||||
if(p->parent->color == RED){
|
||||
if(p->uncle()->color == RED) {
|
||||
p->parent->color = p->uncle()->color = BLACK;
|
||||
p->grandparent()->color = RED;
|
||||
insert_case(p->grandparent());
|
||||
} else {
|
||||
if(p->parent->rightTree == p && p->grandparent()->leftTree == p->parent) {
|
||||
rotate_left (p);
|
||||
rotate_right (p);
|
||||
p->color = BLACK;
|
||||
p->leftTree->color = p->rightTree->color = RED;
|
||||
} else if(p->parent->leftTree == p && p->grandparent()->rightTree == p->parent) {
|
||||
rotate_right (p);
|
||||
rotate_left (p);
|
||||
p->color = BLACK;
|
||||
p->leftTree->color = p->rightTree->color = RED;
|
||||
} else if(p->parent->leftTree == p && p->grandparent()->leftTree == p->parent) {
|
||||
p->parent->color = BLACK;
|
||||
p->grandparent()->color = RED;
|
||||
rotate_right(p->parent);
|
||||
} else if(p->parent->rightTree == p && p->grandparent()->rightTree == p->parent) {
|
||||
p->parent->color = BLACK;
|
||||
p->grandparent()->color = RED;
|
||||
rotate_left(p->parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DeleteTree(Node *p){
|
||||
if(!p || p == NIL){
|
||||
return;
|
||||
}
|
||||
DeleteTree(p->leftTree);
|
||||
DeleteTree(p->rightTree);
|
||||
delete p;
|
||||
}
|
||||
public:
|
||||
|
||||
bst() {
|
||||
NIL = new Node();
|
||||
NIL->color = BLACK;
|
||||
root = NULL;
|
||||
}
|
||||
|
||||
~bst() {
|
||||
if (root)
|
||||
DeleteTree (root);
|
||||
delete NIL;
|
||||
}
|
||||
|
||||
void inorder() {
|
||||
if(root == NULL)
|
||||
return;
|
||||
inorder (root);
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void insert (int x) {
|
||||
if(root == NULL){
|
||||
root = new Node();
|
||||
root->color = BLACK;
|
||||
root->leftTree = root->rightTree = NIL;
|
||||
root->value = x;
|
||||
} else {
|
||||
insert(root, x);
|
||||
}
|
||||
}
|
||||
|
||||
bool delete_value (int data) {
|
||||
return delete_child(root, data);
|
||||
}
|
||||
private:
|
||||
Node *root, *NIL;
|
||||
};
|
34
README.md
34
README.md
|
@ -1251,13 +1251,12 @@ typedef struct BiTNode
|
|||
[选择排序](Algorithm/SelectionSort.h) | O(n<sup>2</sup>)|O(n<sup>2</sup>)|O(1)|数组不稳定、链表稳定
|
||||
[插入排序](Algorithm/InsertSort.h) | O(n<sup>2</sup>)|O(n<sup>2</sup>)|O(1)|稳定
|
||||
[快速排序](Algorithm/QuickSort.h) | O(n*log<sub>2</sub>n) | O(n<sup>2</sup>) | O(log<sub>2</sub>n) | 不稳定
|
||||
[堆排序](Algorithm/HeapSort.h) | O(n*log<sub>2</sub>n)|O(n<sup>2</sup>)|O(1)|不稳定
|
||||
[堆排序](Algorithm/HeapSort.cpp) | O(n*log<sub>2</sub>n)|O(n<sup>2</sup>)|O(1)|不稳定
|
||||
[归并排序](Algorithm/MergeSort.h) | O(n*log<sub>2</sub>n) | O(n*log<sub>2</sub>n)|O(1)|稳定
|
||||
[希尔排序](Algorithm/ShellSort.h) | O(n*log<sup>2</sup>n)|O(n<sup>2</sup>)|O(1)|不稳定
|
||||
[计数排序](Algorithm/CountSort.h) | O(n+m)|O(n+m)|O(n+m)|稳定
|
||||
[桶排序](Algorithm/BucketSort.h) | O(n)|O(n)|O(m)|稳定
|
||||
[计数排序](Algorithm/CountSort.cpp) | O(n+m)|O(n+m)|O(n+m)|稳定
|
||||
[桶排序](Algorithm/BucketSort.cpp) | O(n)|O(n)|O(m)|稳定
|
||||
[基数排序](Algorithm/RadixSort.h) | O(k*n)|O(n<sup>2</sup>)| |稳定
|
||||
[文件排序](Algorithm/FileSort) |
|
||||
|
||||
> * 均按从小到大排列
|
||||
> * k:代表数值中的"数位"个数
|
||||
|
@ -1271,8 +1270,13 @@ typedef struct BiTNode
|
|||
---|---|---|---
|
||||
[顺序查找](Algorithm/SequentialSearch.h) | O(n) | O(1) | 无序或有序
|
||||
[二分查找(折半查找)](Algorithm/BinarySearch.h) | O(log<sub>2</sub>n)| O(1) | 有序
|
||||
[蛮力字符串匹配](Algorithm/BruteForceStringMatch.h) | O(n*m) |
|
||||
[文件查找](Algorithm/FileSearch) |
|
||||
[插值查找](Algorithm/InsertionSearch.h) | O(log<sub>2</sub>(log<sub>2</sub>n)) | O(1) | 有序
|
||||
[斐波那契查找](Algorithm/FibonacciSearch.cpp) | O(log<sub>2</sub>n) | O(1) | 有序
|
||||
[哈希查找](DataStructure/HashTable.cpp) | O(1) | O(n) | 无序或有序
|
||||
[二叉查找树(二叉搜索树查找)](Algorithm/BSTSearch.h) |O(log<sub>2</sub>n) | |
|
||||
[红黑树](DataStructure/RedBlackTree.cpp) |O(log<sub>2</sub>n) | |
|
||||
2-3树 | O(log<sub>2</sub>n - log<sub>3</sub>n) | |
|
||||
B树/B+树 |O(log<sub>2</sub>n) | |
|
||||
|
||||
## Problems
|
||||
|
||||
|
@ -1286,22 +1290,8 @@ typedef struct BiTNode
|
|||
|
||||
### Leetcode Problems
|
||||
|
||||
#### Array
|
||||
|
||||
* [1. Two Sum](Problems/LeetcodeProblems/1-two-sum.h)
|
||||
* [4. Median of Two Sorted Arrays](Problems/LeetcodeProblems/4-median-of-two-sorted-arrays.h)
|
||||
* [11. Container With Most Water](Problems/LeetcodeProblems/11-container-with-most-water.h)
|
||||
* [26. Remove Duplicates from Sorted Array](Problems/LeetcodeProblems/26-remove-duplicates-from-sorted-array.h)
|
||||
* [53. Maximum Subarray](Problems/LeetcodeProblems/53-maximum-subarray.h)
|
||||
* [66. Plus One](Problems/LeetcodeProblems/66-plus-one.h)
|
||||
* [88. Merge Sorted Array](Problems/LeetcodeProblems/88-merge-sorted-array.h)
|
||||
* [118. Pascal's Triangle](Problems/LeetcodeProblems/118-pascals-triangle.h)
|
||||
* [119. Pascal's Triangle II](Problems/LeetcodeProblems/119-pascals-triangle-ii.h)
|
||||
* [121. Best Time to Buy and Sell Stock](Problems/LeetcodeProblems/121-best-time-to-buy-and-sell-stock.h)
|
||||
* [122. Best Time to Buy and Sell Stock II](Problems/LeetcodeProblems/122-best-time-to-buy-and-sell-stock-ii.h)
|
||||
* [169. Majority Element](Problems/LeetcodeProblems/169-majority-element.h)
|
||||
* [283. Move Zeroes](Problems/LeetcodeProblems/283-move-zeroes.h)
|
||||
|
||||
* [Github . haoel/leetcode](https://github.com/haoel/leetcode)
|
||||
* [Github . pezy/LeetCode](https://github.com/pezy/LeetCode)
|
||||
|
||||
## 操作系统
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user