first commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
### B树基础
|
||||
|
||||
B树即B-tree(B树就是B-tree),B是balanced,也就是平衡的意思。
|
||||
|
||||
B树又叫平衡多路查找树,节点的直接点个数可以多于2个,一颗m阶的B树:
|
||||
|
||||
1) 树中每个节点做多含有m个子节点
|
||||
2) 根节点不是叶子节点,至少有2个孩子
|
||||
3) 所有叶子节点在同一层
|
||||
4) 除根节点和叶子节点外,每个分支节点至少有[m/2]个子树
|
||||
5) 有j个孩子的非叶子节点有 j-1 个关键码,关键码递增一次排列
|
||||
|
||||
|
||||
### B树应用
|
||||
|
||||
数据库
|
||||
文件系统
|
||||
|
||||
|
||||
### B树存储结构
|
||||
|
||||
|
||||
|
||||
### B树问题和延伸阅读
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
### 红黑树基础
|
||||
|
||||
一种自平衡的二叉查找树。在每个节点中增加一个存储位表示节点的颜色,可以是red或black
|
||||
|
||||
红黑树比AVL树优秀在哪?
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
5个性质
|
||||
|
||||
性质1. 节点是红色或黑色。
|
||||
性质2. 根是黑色。
|
||||
性质3. 所有叶子都是黑色(叶子是NIL节点)。
|
||||
性质4. 每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点)
|
||||
性质5. 从任一节点到其每个叶子的所有简单路径 都包含相同数目的黑色节点。
|
||||
|
||||
|
||||
|
||||
### 红黑树应用
|
||||
|
||||
|
||||
### 存储结构
|
||||
|
||||
|
||||
### 问题与延伸阅读
|
||||
|
||||
|
||||
*/
|
||||
|
||||
typedef int ElemType
|
||||
|
||||
typedef struct node{
|
||||
|
||||
int color;
|
||||
ElemType key;
|
||||
struct node *lChild,*rChild,*pChild;
|
||||
|
||||
}*RBTree;
|
||||
|
||||
|
||||
int rbtree_insert(RBTree *tree,ElemType key);
|
||||
int rbtree_remove(RBTree *tree,ElemType key);
|
||||
int rbtree_search(RBTree *tree,ElemType key);
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
|
||||
### 后缀树(suffix tree)基础
|
||||
|
||||
又叫后缀trie,与trie最大不同在于:字符串集合由指定的后缀子串组成。
|
||||
|
||||
|
||||
很适合用来操作字符串的子串。 用于字符串的匹配和查询
|
||||
|
||||
|
||||
### 后缀树应用
|
||||
|
||||
|
||||
从目标串T中判断是否包含模式串P(时间复杂度接近KMP算法);
|
||||
从目标串T中查找最长的重复子串;
|
||||
从目标串T1和T2中查找最长公共子串;
|
||||
Ziv-Lampel无损压缩算法;
|
||||
从目标串T中查找最长的回文子串;
|
||||
|
||||
|
||||
|
||||
### 存储结构
|
||||
|
||||
|
||||
### suffix tree 问题与延伸阅读
|
||||
|
||||
后缀数组
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
###环境
|
||||
|
||||
|
||||
###编译
|
||||
|
||||
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,337 @@
|
||||
//
|
||||
// bisearchtree.c
|
||||
// BiSearchTree
|
||||
//
|
||||
// Created by nonstriater on 14-2-22.
|
||||
//
|
||||
//
|
||||
|
||||
#include "bisearchtree.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
/**
|
||||
* 创建二叉查找树
|
||||
*
|
||||
* @return 指向一颗空树的指针
|
||||
*/
|
||||
BiSearchTree *bisearch_tree_new(){
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入节点
|
||||
*
|
||||
* @param tree tree
|
||||
* @param node 节点值
|
||||
*/
|
||||
BiSearchTree *bisearch_tree_insert(BiSearchTree *tree,ElemType node){
|
||||
|
||||
BiSearchTree *t = tree;
|
||||
BiSearchTree *parent = NULL;
|
||||
BiSearchTree *newNode = (BiSearchTree *)malloc(sizeof(BiSearchTree));
|
||||
if (NULL==newNode) {
|
||||
printf("malloc failure\n");
|
||||
return tree;
|
||||
}
|
||||
newNode->key = node;
|
||||
newNode->lChild = NULL;
|
||||
newNode->rChild = NULL;
|
||||
|
||||
if (NULL==t) {
|
||||
|
||||
tree = newNode;
|
||||
printf("insert first node %d \n",node);
|
||||
return tree;
|
||||
}
|
||||
|
||||
// 找合适的位置
|
||||
while (t!=NULL) {
|
||||
|
||||
if( node < t->key){
|
||||
parent = t;
|
||||
t=t->lChild;
|
||||
}else if(node == t->key){
|
||||
printf("insert ignore:the bi search tree has the node with %d\n",node);
|
||||
break;
|
||||
}else{
|
||||
parent = t;
|
||||
t=t->rChild;
|
||||
}
|
||||
}
|
||||
|
||||
if (parent!=NULL) {
|
||||
|
||||
if (node>parent->key) {
|
||||
printf("insert node %d to right of %d\n",node,parent->key);
|
||||
parent->rChild = newNode;
|
||||
|
||||
}else{
|
||||
printf("insert node %d to left of %d\n",node,parent->key);
|
||||
parent->lChild = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
return tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找节点
|
||||
*
|
||||
* @param tree tree
|
||||
* @param node 节点值
|
||||
* (也可以使用递归方式实现)
|
||||
*/
|
||||
int bisearch_tree_search(BiSearchTree *tree,ElemType node){
|
||||
|
||||
BiSearchTree *t;
|
||||
t= tree;
|
||||
while (NULL!=t) {
|
||||
|
||||
if (node<t->key) {
|
||||
t=t->lChild;
|
||||
}
|
||||
else if (node==t->key) {
|
||||
break;
|
||||
}else{
|
||||
t=t->rChild;
|
||||
}
|
||||
|
||||
}
|
||||
if (NULL==t) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//private
|
||||
int _delete_node(BiSearchTree *node,BiSearchTree*parent){
|
||||
|
||||
//该节点为叶子节点,直接删除
|
||||
if (!node->rChild && !node->lChild)
|
||||
{
|
||||
|
||||
free(node);//父节点???不然野指针,造成崩溃
|
||||
}
|
||||
else if(!node->rChild){ //右子树空则只需重接它的左子树
|
||||
BiSearchTree *target=node->lChild;
|
||||
node->key = node->lChild->key;
|
||||
node->lChild=node->lChild->lChild;
|
||||
node->rChild=node->lChild->rChild;
|
||||
|
||||
free(target);
|
||||
}
|
||||
else if(!node->lChild){ //左子树空只需重接它的右子树
|
||||
BiSearchTree *target=node->rChild;
|
||||
node->key = node->rChild->key;
|
||||
node->lChild=node->rChild->lChild;
|
||||
node->rChild=node->rChild->rChild;
|
||||
|
||||
free(target);
|
||||
}
|
||||
else{ //左右子树均不空
|
||||
|
||||
BiSearchTree *parent=node,*target=node->lChild;
|
||||
while (target->rChild) {
|
||||
parent = target;
|
||||
target=target->rChild;
|
||||
}// 找到左子树最大的,是删除节点的直接“前驱”
|
||||
|
||||
node->key = target->key;
|
||||
|
||||
if (target!=node) {
|
||||
parent->rChild = target->lChild;
|
||||
}else{
|
||||
node->lChild = target->lChild;
|
||||
}
|
||||
|
||||
free(target);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//删除节点,需要重建排序树
|
||||
/*
|
||||
1) 删除节点是叶子节点(分支为0),结构不破坏
|
||||
2)删除节点只有一个分支(分支为1),结构也不破坏
|
||||
3)删除节点有2个分支,此时删除节点
|
||||
|
||||
思路一: 选左子树的最大节点,或右子树最小节点替换
|
||||
|
||||
*/
|
||||
|
||||
int bisearch_tree_delete(BiSearchTree **tree,ElemType node){
|
||||
|
||||
if (NULL==tree) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 查找删除目标节点
|
||||
BiSearchTree *target=*tree,*parent=NULL;
|
||||
while (NULL!=target) {
|
||||
if (node<target->key) {
|
||||
parent=target;
|
||||
target=target->lChild;
|
||||
}else if(node==target->key){
|
||||
break;
|
||||
}else{
|
||||
parent=target;
|
||||
target=target->rChild;
|
||||
}
|
||||
}
|
||||
|
||||
if (NULL==target) {
|
||||
printf("树为空,或想要删除的节点不存在\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//该节点为叶子节点,直接删除
|
||||
if (!target->rChild && !target->lChild)
|
||||
{
|
||||
if (NULL==parent) {////只有一个节点的二叉查找树
|
||||
*tree=NULL;
|
||||
}else{
|
||||
if (target->key>parent->key) {
|
||||
parent->rChild=NULL;
|
||||
}else{
|
||||
parent->lChild=NULL;
|
||||
}
|
||||
|
||||
}
|
||||
free(target);//父节点处理,不然野指针,造成崩溃
|
||||
}
|
||||
|
||||
else if(!target->rChild){ //右子树空则只需重接它的左子树
|
||||
BiSearchTree *del=target->lChild;
|
||||
target->key = target->lChild->key;
|
||||
target->lChild=target->lChild->lChild;
|
||||
target->rChild=target->lChild->rChild;
|
||||
|
||||
free(del);
|
||||
}
|
||||
else if(!target->lChild){ //左子树空只需重接它的右子树
|
||||
BiSearchTree *del=target->rChild;
|
||||
target->key = target->rChild->key;
|
||||
target->lChild=target->rChild->lChild;
|
||||
target->rChild=target->rChild->rChild;
|
||||
|
||||
free(del);
|
||||
}
|
||||
else{ //左右子树均不空
|
||||
|
||||
BiSearchTree *p=target,*t=target->lChild;
|
||||
while (t->rChild) {
|
||||
p = t;
|
||||
t=t->rChild;
|
||||
}// 找到左子树最大的,是删除节点的直接“前驱”
|
||||
|
||||
target->key = t->key;
|
||||
|
||||
if (p!=target) {
|
||||
p->rChild = t->lChild;
|
||||
}else{
|
||||
target->lChild = t->lChild;
|
||||
}
|
||||
|
||||
free(t);
|
||||
}
|
||||
return 0;
|
||||
|
||||
|
||||
|
||||
// if (node==tree->key) {
|
||||
// return _delete_node(tree);
|
||||
// }else if(node>tree->key){
|
||||
// return bisearch_tree_delete(tree->rChild,node);
|
||||
// }else{
|
||||
// return bisearch_tree_delete(tree->lChild,node);
|
||||
// }
|
||||
|
||||
|
||||
/*
|
||||
|
||||
// 查找删除目标节点
|
||||
BiSearchTree *t=tree,*parent=NULL;
|
||||
while (NULL!=t) {
|
||||
if (node<t->key) {
|
||||
parent=t;
|
||||
t=t->lChild;
|
||||
}else if(node==t->key){
|
||||
break;
|
||||
}else{
|
||||
parent=t;
|
||||
t=t->rChild;
|
||||
}
|
||||
}
|
||||
|
||||
if (NULL==t) {
|
||||
printf("树为空,或想要删除的节点不存在\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// parent 可能为null
|
||||
|
||||
//删除节点0个分支
|
||||
if (!t->lChild && !t->rChild) {
|
||||
if (t->key>parent->key) {
|
||||
parent->rChild=NULL;
|
||||
}else{
|
||||
parent->lChild=NULL;
|
||||
}
|
||||
free(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//删除节点有1个分支
|
||||
else if (t->lChild && !t->rChild) {
|
||||
if (t->key>parent->key) {
|
||||
parent->rChild=t->lChild;
|
||||
}else{
|
||||
parent->lChild=t->lChild;
|
||||
}
|
||||
free(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
else if (!t->lChild && t->rChild) {
|
||||
if (t->key>parent->key) {
|
||||
parent->rChild=t->rChild;
|
||||
}else{
|
||||
parent->lChild=t->rChild;
|
||||
}
|
||||
free(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//删除节点有2个分支
|
||||
else if (t->lChild && t->rChild) {
|
||||
|
||||
if (t->key->parent->key) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
// 中序遍历节点,也就是从小到大输出
|
||||
void bisearch_tree_inorder_traversal(BiSearchTree *tree){
|
||||
|
||||
if (tree) {
|
||||
bisearch_tree_inorder_traversal(tree->lChild);
|
||||
printf("%d \n",tree->key);
|
||||
bisearch_tree_inorder_traversal(tree->rChild);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// bisearchtree.h
|
||||
// BiSearchTree
|
||||
//
|
||||
// Created by nonstriater on 14-2-22.
|
||||
//
|
||||
//
|
||||
|
||||
/**
|
||||
*
|
||||
二叉查找树(Binary search tree),也叫有序二叉树(Ordered binary tree),排序二叉树(Sorted binary tree)。是指一个空树或者具有下列性质的二叉树:
|
||||
|
||||
1. 若任意节点的左子树不为空,则左子树上所有的节点值小于它的根节点值
|
||||
2. 若任意节点的右子树不为空,则右子树上所有节点的值均大于它的根节点的值
|
||||
3. 任意节点左右子树也为二叉查找树
|
||||
4. 没有键值相等的节点
|
||||
|
||||
*/
|
||||
|
||||
#ifndef BiSearchTree_bisearchtree_h
|
||||
#define BiSearchTree_bisearchtree_h
|
||||
|
||||
typedef int ElemType;
|
||||
typedef struct BiSearchTree{
|
||||
|
||||
ElemType key;
|
||||
struct BiSearchTree *lChild;
|
||||
struct BiSearchTree *rChild;
|
||||
|
||||
|
||||
}BiSearchTree;
|
||||
|
||||
|
||||
/**
|
||||
* 创建二叉查找树
|
||||
*
|
||||
* @return 指向一颗空树的指针
|
||||
*/
|
||||
BiSearchTree *bisearch_tree_new();
|
||||
|
||||
/**
|
||||
* 插入节点
|
||||
*
|
||||
* @param tree tree
|
||||
* @param node 节点值
|
||||
*/
|
||||
BiSearchTree *bisearch_tree_insert(BiSearchTree *tree,ElemType node);
|
||||
|
||||
/**
|
||||
* 查找节点
|
||||
*
|
||||
* @param tree tree
|
||||
* @param node 节点值
|
||||
* @return -1失败 0 成功
|
||||
*/
|
||||
int bisearch_tree_search(BiSearchTree *tree,ElemType node);
|
||||
|
||||
//删除节点
|
||||
int bisearch_tree_delete(BiSearchTree **tree,ElemType node);
|
||||
|
||||
// 遍历节点
|
||||
void bisearch_tree_inorder_traversal(BiSearchTree *tree);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,149 @@
|
||||
//
|
||||
// main.c
|
||||
// BiSearchTree
|
||||
//
|
||||
// Created by nonstriater on 14-2-22.
|
||||
//
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include "bisearchtree.h"
|
||||
|
||||
int main(){
|
||||
|
||||
printf("begining...\n");
|
||||
BiSearchTree *searchTree;
|
||||
searchTree = bisearch_tree_new();
|
||||
searchTree = bisearch_tree_insert(searchTree,19);// 第一次insert
|
||||
bisearch_tree_insert(searchTree,3);
|
||||
bisearch_tree_inorder_traversal(searchTree);
|
||||
|
||||
bisearch_tree_insert(searchTree,122);
|
||||
bisearch_tree_insert(searchTree,55);
|
||||
bisearch_tree_insert(searchTree,65);
|
||||
bisearch_tree_insert(searchTree,180);
|
||||
|
||||
printf("inorder traversal\n");
|
||||
bisearch_tree_inorder_traversal(searchTree);
|
||||
|
||||
|
||||
//=======search ============================================
|
||||
printf("find node...\n");
|
||||
int find = 55;
|
||||
if(bisearch_tree_search(searchTree,find)<0){
|
||||
printf("node %d not exits\n",find);
|
||||
}else{
|
||||
printf("node %d exits\n",find);
|
||||
}
|
||||
|
||||
find = 33;
|
||||
if(bisearch_tree_search(searchTree,find)<0){
|
||||
printf("node %d not exits\n",find);
|
||||
}else{
|
||||
printf("node %d exits\n",find);
|
||||
}
|
||||
|
||||
|
||||
//====== delete,会影响到serachTree值,所以应该传递serachTree的指针 ======================================
|
||||
printf("delete node..\n");
|
||||
find = 4;
|
||||
if(bisearch_tree_delete(&searchTree,find)<0){
|
||||
printf("delete node %d failure\n",find);
|
||||
}else{
|
||||
printf("delete node %d success\n",find);
|
||||
bisearch_tree_inorder_traversal(searchTree);
|
||||
bisearch_tree_insert(searchTree,find);
|
||||
}
|
||||
|
||||
|
||||
find = 65;//叶子
|
||||
if(bisearch_tree_delete(&searchTree,find)<0){
|
||||
printf("delete node failure\n");
|
||||
}else{
|
||||
printf("delete node %d success\n",find);
|
||||
bisearch_tree_inorder_traversal(searchTree);
|
||||
bisearch_tree_insert(searchTree,find);
|
||||
}
|
||||
|
||||
|
||||
|
||||
find = 55;//单分支
|
||||
if(bisearch_tree_delete(&searchTree,find)<0){
|
||||
printf("delete node failure\n");
|
||||
}else{
|
||||
printf("delete node %d success\n ",find);
|
||||
bisearch_tree_inorder_traversal(searchTree);
|
||||
bisearch_tree_insert(searchTree,find);
|
||||
}
|
||||
|
||||
find = 122;//双分支
|
||||
if(bisearch_tree_delete(&searchTree,find)<0){
|
||||
printf("delete node failure\n");
|
||||
}else{
|
||||
printf("delete node %d success\n",find);
|
||||
bisearch_tree_inorder_traversal(searchTree);
|
||||
bisearch_tree_insert(searchTree,find);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
测试结果:
|
||||
|
||||
begining...
|
||||
insert first node 19
|
||||
insert node 3 to left of 19
|
||||
3
|
||||
19
|
||||
insert node 122 to right of 19
|
||||
insert node 55 to left of 122
|
||||
insert node 65 to right of 55
|
||||
insert node 180 to right of 122
|
||||
inorder traversal
|
||||
3
|
||||
19
|
||||
55
|
||||
65
|
||||
122
|
||||
180
|
||||
find node...
|
||||
node 55 exits
|
||||
node 33 not exits
|
||||
delete node..
|
||||
树为空,或想要删除的节点不存在
|
||||
delete node 4 failure
|
||||
delete node 65 success
|
||||
3
|
||||
19
|
||||
55
|
||||
122
|
||||
180
|
||||
insert node 65 to right of 55
|
||||
delete node 55 success
|
||||
3
|
||||
19
|
||||
65
|
||||
122
|
||||
180
|
||||
insert node 55 to left of 65
|
||||
delete node 122 success
|
||||
3
|
||||
19
|
||||
55
|
||||
65
|
||||
180
|
||||
insert node 122 to left of 180
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,334 @@
|
||||
|
||||
/*
|
||||
自平衡二叉查找树(AVL tree): 首先也是二次查找树,其实 任何2个子树的高度差不大于1
|
||||
在删除,插入的过程中不断调整子树的高度,保证平均和最坏情况下都是O(logn)
|
||||
|
||||
Adelson-Velskii 和 Landis 1962年 创造。
|
||||
|
||||
1) 平衡因子 -1 0 1 节点是正常的。平衡因子 = 左子树高度-右字数高度
|
||||
2) 除此之外的节点是不平衡的,需要重新平衡这个树。也就是AVL旋转
|
||||
|
||||
插入节点:
|
||||
|
||||
a: 左旋转(RR型:节点x的右孩子的右孩子上插入新元素)平衡因子由-1 -》-2 时,需要绕节点x左旋转
|
||||
b:右旋转(LL型:节点X的左孩子的左孩子上插入新元素) 平衡因子有1-》2,右旋转
|
||||
c: 先左后右旋转:(LR型:树中节点X的左孩子的右孩子上插入新元素) 平衡因子从1变成2后,就需要 先绕X的左子节点Y左旋转,接着再绕X右旋转
|
||||
d: 先右后左旋转:(RL型:节点X的右孩子的左孩子上插入新元素)
|
||||
|
||||
|
||||
6 6 6 6
|
||||
/ \ / \
|
||||
5 7 3 9
|
||||
/ \ \ /
|
||||
3 8 5 7
|
||||
(LL型) (RR) (LR) (RL)
|
||||
|
||||
|
||||
删除节点:
|
||||
|
||||
|
||||
|
||||
|
||||
可以看到,为了保证高度平衡,插入和删除操作代价增加
|
||||
|
||||
记于2014-2-28 by @nonstriater
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#define LH 1
|
||||
#define EH 0
|
||||
#define RH -1
|
||||
|
||||
|
||||
typedef int KEY_TYPE;
|
||||
typedef struct node{
|
||||
|
||||
KEY_TYPE key;
|
||||
int height; // 平衡因子
|
||||
struct node *lChild;
|
||||
struct node *rChild;
|
||||
|
||||
}AVLTree;
|
||||
|
||||
|
||||
|
||||
//
|
||||
void avltree_rr_rotate(AVLTree **tree){
|
||||
|
||||
AVLTree *right= *tree->rChild;
|
||||
right->lChild = *tree;
|
||||
*tree->rChild = right->lChild;
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
void avltree_ll_rotate(AVLTree **tree){
|
||||
|
||||
AVLTree *left = *tree->lChild;
|
||||
left->rChild = *tree;
|
||||
*tree->lChild = left->rChild
|
||||
}
|
||||
|
||||
//
|
||||
void avltree_lr_rotate(AVLTree **tree){
|
||||
|
||||
|
||||
}
|
||||
|
||||
void avltree_rl_rotate(AVLTree **tree){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void avltree_left_balance(AVLTree **root)
|
||||
{
|
||||
AVLTree *left,*lr;
|
||||
left=(*root)->lChild;
|
||||
switch(left->height)
|
||||
{
|
||||
//检查T的左子树平衡度,并作相应的平衡处理
|
||||
case LH://新节点插入在T的左孩子的左子树上,做单右旋处理
|
||||
(*root)->height=left->height=EH;
|
||||
avltree_ll_rotate(root);
|
||||
break;
|
||||
case RH://新插入节点在T的左孩子的右子树上,做双旋处理
|
||||
lr=left->rChild;
|
||||
switch(lr->height)
|
||||
{
|
||||
case LH:
|
||||
(*root)->height=RH;
|
||||
left->height=EH;
|
||||
break;
|
||||
case EH:
|
||||
(*root)->height=left->height=EH;
|
||||
break;
|
||||
case RH:
|
||||
(*root)->height=EH;
|
||||
left->height=LH;
|
||||
break;
|
||||
}
|
||||
lr->height=EH;
|
||||
L_Rotate(&(*T)->lChild);
|
||||
R_Rotate(T);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void avltree_right_balance(AVLTree **root)
|
||||
{
|
||||
AVLTree right,rl;
|
||||
right=(*root)->rChild;
|
||||
switch(right->height)
|
||||
{
|
||||
case RH://新节点插在T的右孩子的右子树上,要做单左旋处理
|
||||
(*root)->height=right->height=EH;
|
||||
avltree_rr_rotate(root);
|
||||
break;
|
||||
case LH://新节点插在T的右孩子的左子树上,要做双旋处理
|
||||
rl=right->lChild;
|
||||
switch(rl->height)
|
||||
{
|
||||
case LH:
|
||||
(*root)->height=EH;
|
||||
right->height=RH;
|
||||
break;
|
||||
case EH:
|
||||
(*root)->height=right->height=EH;
|
||||
break;
|
||||
case RH:
|
||||
(*root)->height=LH;
|
||||
right->height=EH;
|
||||
break;
|
||||
}
|
||||
rl->height=EH;
|
||||
R_Rotate(&(*root)->rChild);
|
||||
L_Rotate(T);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 插入一个节点key
|
||||
/*
|
||||
|
||||
算法描述:
|
||||
1)如果root为null,则插入一个数据元素为kx 的新结点作为T 的根结点
|
||||
|
||||
2)如果key和root->key相等,不插入
|
||||
|
||||
3)如果key<root->key, 插在root左子树上:
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
AVLTree* avltree_insert(AVLTree* root, KEY_TYPE key){
|
||||
|
||||
|
||||
if (NULL==root)
|
||||
{
|
||||
|
||||
root = (AVLTree *)malloc(sizeof(struct AVLTree));
|
||||
if (!root)
|
||||
{
|
||||
printf("内存分配失败,插入节点失败\n");
|
||||
return root;
|
||||
}
|
||||
root.key = key;
|
||||
root.lChild = NULL;
|
||||
root.rChild = NULL;
|
||||
root.height = 0;
|
||||
}
|
||||
|
||||
|
||||
else if (key=root->key)
|
||||
{
|
||||
printf("节点 %d 已存在 \n", key);
|
||||
}
|
||||
|
||||
else if (key<root->key)//插入左
|
||||
{
|
||||
root->lChild = avltree_insert(root->lChild,key);
|
||||
if (root->lChild->height-root->rChild->height == 2)//不平衡
|
||||
{
|
||||
if (key<root->lChild->key)// LL型
|
||||
{
|
||||
root = avltree_ll_rotate(tree);
|
||||
}
|
||||
|
||||
if (key>root->lChild->key)//LR
|
||||
{
|
||||
root = avltree_lr_rotate(tree);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
else if (key>root->key){
|
||||
|
||||
root->rChild = avltree_insert(root->rChild,key);
|
||||
if (key<root->rChild->key)// RL
|
||||
{
|
||||
root = avltree_rl_rotate(tree);
|
||||
}
|
||||
|
||||
if (key>root->rChild->key)// RR
|
||||
{
|
||||
root = avltree_rr_rotate(tree);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
// 删除一个节点
|
||||
AVLTree* avltree_delete(AVLTree* root, KEY_TYPE key){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 判断是否为AVL树
|
||||
int avltree_isbalance(AVLTree *root){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 查找
|
||||
AVLTree* avltree_search(AVLTree *root,KEY_TYPE key){
|
||||
|
||||
if (root==NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (root->key == key)
|
||||
{
|
||||
return root;
|
||||
}
|
||||
|
||||
else if (root->key>key)
|
||||
{
|
||||
return avltree_search(root->lChild,key);
|
||||
}
|
||||
|
||||
else{
|
||||
|
||||
return avltree_search(root->rChild,key);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 中序遍历
|
||||
void avltree_inorder_traversal(AVLTree* root){
|
||||
|
||||
if (root)
|
||||
{
|
||||
avltree_inorder_traversal(root->lChild);
|
||||
printf(节点值=%d,左右子树的高度差=%d\n,root->key,root->height);
|
||||
avltree_inorder_traversal(root->rChild);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// test
|
||||
|
||||
int main(){
|
||||
|
||||
AVLTree *avlTree=NULL;
|
||||
printf("插入节点,创建一个AVL树...\n");
|
||||
|
||||
int values[] = {11,7,222,456,23,8,65,124,88,2,54};
|
||||
for (int i = 0; i < sizeof(values)/sizeof(int); ++i)
|
||||
{
|
||||
printf("插入节点 %d\n", values[i]);
|
||||
avlTree = avltree_insert(avlTree,values[i]);
|
||||
avltree_inorder_traversal(avltree);
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
printf("中序遍历结果:\n");
|
||||
avltree_inorder_traversal(avlTree);
|
||||
|
||||
|
||||
printf("删除一个存在的节点 %d\n", values[1]);
|
||||
avlTree=avltree_delete(avlTree,values[1]);
|
||||
printf("中序遍历结果:\n");
|
||||
avltree_inorder_traversal(avlTree);
|
||||
|
||||
|
||||
printf("删除一个不存在的节点 %d\n",111 );
|
||||
avltree_delete(avlTree,111);
|
||||
printf("中序遍历结果:\n");
|
||||
avltree_inorder_traversal(avlTree);
|
||||
|
||||
printf("查找一个存在的节点 %d\n", values[3]);
|
||||
avltree_search(avltree,values[3]);
|
||||
|
||||
printf("查找一个不存在的节点 %d\n",51);
|
||||
avltree_search(avltree,51);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
### AVL 实现过程中的问题
|
||||
|
||||
|
||||
|
||||
### AVL 实际使用案例
|
||||
|
||||
* LLVM 的 ImmutableSet,其底层的实现选择为 AVL 树
|
||||
* 《一种基于二叉平衡树的P2P覆盖网络的研究》论文
|
||||
@@ -0,0 +1,155 @@
|
||||
|
||||
/*
|
||||
|
||||
### trie基本
|
||||
|
||||
字典树,英文名Trie树,Trie一词来自retrieve,发音为/tri:/ “tree”,也有人读为/traɪ/ “try”,
|
||||
又称单词查找树,Trie树,是一种树形结构。
|
||||
|
||||
trie,又称为前缀树或字典树,是一种有序树,用于保存关联数组。
|
||||
|
||||
1) 一个节点的所有子孙都有相同的前缀,即为这个节点对应的字符串
|
||||
2) 根节点对应空字符串,即不包含字符
|
||||
3) 不是所有节点都有对应值,只有叶子节点和部分内部节点所对应的键才有相关的值
|
||||
4) 键不是保存在节点中,而是由节点在树中的位置决定。
|
||||
|
||||
|
||||
### trie应用
|
||||
|
||||
典型应用是:
|
||||
|
||||
用于统计,排序和保存大量的字符串(但不仅限于字符串),
|
||||
所以经常被搜索引擎系统用于文本词频统计。
|
||||
排序大量字符串
|
||||
用于索引结构
|
||||
敏感词过滤
|
||||
|
||||
|
||||
它的优点是:
|
||||
节省空间:利用字符串的公共前缀来节约存储空间
|
||||
最大限度地减少无谓的字符串比较
|
||||
|
||||
|
||||
|
||||
|
||||
查询效率比hash table 更优??
|
||||
|
||||
|
||||
|
||||
### trie 存储结构设计
|
||||
|
||||
最简单实现 ---- 26个字母表 a-z 。
|
||||
|
||||
子树用数组存储,浪费空间;
|
||||
如果用链表存储,查询时需要遍历链表,降低查询效率
|
||||
|
||||
|
||||
|
||||
DATrie libdatrie
|
||||
|
||||
double-array trie
|
||||
参考作者的这篇论文 http://linux.thai.net/~thep/datrie/datrie.html
|
||||
|
||||
|
||||
trie树的增加和删除都比较麻烦,但索引本身就是写少读多,是否考虑添加删除的复杂度上升,依靠具体场景决定
|
||||
|
||||
|
||||
|
||||
### trie 问题
|
||||
|
||||
中文支持
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ALPHABET_SIZE 26
|
||||
|
||||
typedef struct node
|
||||
{
|
||||
int count; //count 如果为0,则代表非黄色点,count>0代表是黄色点,同时表示出现次数;
|
||||
char value; //字符
|
||||
struct node * subtries[ALPHABET_SIZE]; //子树
|
||||
} *Trie;
|
||||
|
||||
|
||||
//插入字符串,建立字典树. 返回值 < 0 表示插入失败
|
||||
int trie_insert(Trie *trie,char *c){
|
||||
|
||||
if (NULL==trie)
|
||||
{
|
||||
*trie = (Trie)malloc(sizeof(struct node));
|
||||
if (NULL==trie)
|
||||
{
|
||||
printf("malloc failure...\n");
|
||||
}
|
||||
}
|
||||
|
||||
char *p = c;
|
||||
while(*p!='\0'){
|
||||
if (*p)
|
||||
{
|
||||
/* code */
|
||||
}
|
||||
|
||||
p++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 字符串查找,返回值<0表示没有查找到
|
||||
int trie_query(Trie *trie,char *c){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
void trie_remove(){}
|
||||
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
Trie trie = NULL;
|
||||
char *dict[10]={"int","integer","float","char","nonstriater","weibo"};
|
||||
for (int i = 0; i < sizeof(dict)/sizeof(int); ++i)
|
||||
{
|
||||
if (trie_insert(&trie,dict[i])<0)
|
||||
{
|
||||
printf("%s 插入失败\n", dict[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 查询cha
|
||||
printf("查询cha \n");
|
||||
|
||||
// 查询char
|
||||
printf("查询char \n");
|
||||
|
||||
// 查询hello
|
||||
printf("查询hello \n");
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user