Merge pull request #311 from liuzhang/master

二叉搜索树和字典树 C版本
This commit is contained in:
wangzheng0822 2019-05-20 16:01:20 +08:00 committed by GitHub
commit fc7503056a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 305 additions and 0 deletions

91
c-cpp/24_tree/Trie.c Normal file
View File

@ -0,0 +1,91 @@
/*************************************************************************
> Author: Liu Zhang
> Mail: lz-850610@163.com
> Time: 2019-05-10
> Desc:
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct Node {
char data;
struct Node *children[26];
Status end;
} Trie, *TriePtr;
void Init(TriePtr *T)
{
(*T) = (TriePtr)malloc(sizeof(Trie));
(*T)->data = '/';
(*T)->end = FALSE;
}
void Insert(TriePtr T, char *str) {
int index;
char c;
while(c = *str++)
{
index = c - 'a';
if (T->children[index] == NULL)
{
TriePtr Node;
Node = (TriePtr)malloc(sizeof(Trie));
Node->data = c;
Node->end = FALSE;
T->children[index] = Node;
}
T = T->children[index];
}
T->end = TRUE;
}
Status Search(TriePtr T, char *str) {
int index;
char c;
while(c = *str++)
{
index = c - 'a';
if (T->children[index] == NULL)
{
return FALSE;
}
T = T->children[index];
}
if (T->end) {
return TRUE;
} else {
return FALSE;
}
}
int main(int argc, char const *argv[])
{
TriePtr T;
Init(&T);
char *str = "hello";
char *str2 = "hi";
Insert(T, str);
printf("str is search %d\n", Search(T, str));
printf("str2 is search %d\n", Search(T, str2));
return 0;
}

View File

@ -0,0 +1,214 @@
/*************************************************************************
> Author: Liu Zhang
> Mail: lz-850610@163.com
> Time: 2019-05-10
> Desc:
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef char ElemType;
typedef struct node {
ElemType data;
struct node *lchild, *rchild;
} BTree, *BTreePtr;
/**************** 插入 **********************/
Status Insert(BTreePtr *T, ElemType e) {
BTreePtr p;
if (*T == NULL) {
*T = (BTreePtr)malloc(sizeof(BTree));
(*T)->data = e;
return TRUE;
} else {
p = *T;
while ( p != NULL) {
if (e > p->data) {
if (p->rchild == NULL) {
p->rchild = (BTreePtr) malloc (sizeof(BTree));
p->rchild->data = e;
return TRUE;
}
p = p->rchild;
} else {
if (p->lchild == NULL)
{
p->lchild = (BTreePtr) malloc (sizeof(BTree));
p->lchild->data = e;
return TRUE;
}
p = p->lchild;
}
}
}
return FALSE;
}
/**************** 删除 **********************/
Status Delete(BTreePtr T, ElemType e) {
BTreePtr p, pp, minP, minPP, child;
child = NULL;
p = T;
pp = NULL;
while ( (p != NULL) && (p->data != e) ) {
pp = p;
if (e > p->data) {
p = p->rchild;
} else {
p = p->lchild;
}
}
if (p == NULL) return FALSE;
//双节点
if ((p->lchild != NULL) && (p->rchild != NULL))
{
minPP = p;
minP = p->rchild;
while (minP->lchild != NULL) {
minPP = minP;
minP = minP->lchild;
}
p->data = minP->data;
free(minP);
minPP->lchild = NULL;
return TRUE;
}
//有一个节点
if ((p->lchild != NULL) || (p->rchild != NULL)) {
if (p->lchild) {
child = p->lchild;
} else {
child = p->rchild;
}
free(p);
}
//没有节点
if (pp->lchild == p) {
pp->lchild = child;
} else {
pp->rchild = child;
}
return TRUE;
}
/**************** 查找 **********************/
Status Find(BTreePtr T, ElemType e) {
if (T == NULL) return FALSE;
while ((T != NULL) && (T->data != e)) {
if (e > T->data) {
T = T->rchild;
} else {
T = T->lchild;
}
}
if (T) {
return TRUE;
} else {
return FALSE;
}
}
/**************** 最大值 **********************/
ElemType FindMax(BTreePtr T) {
ElemType max;
while(T != NULL) {
max = T->data;
T = T->rchild;
}
return max;
}
/**************** 最小值 **********************/
ElemType FindMin(BTreePtr T) {
ElemType min;
while(T != NULL) {
min = T->data;
T = T->lchild;
}
return min;
}
void PreOrderTraverse(BTreePtr T)//前序遍历二叉树
{
if (T == NULL) return;
if(T)
{
printf("%d ",T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
void DestroyTree(BTreePtr T) {
if (T)
{
if (T->lchild)
{
DestroyTree(T->lchild);
}
if(T->rchild)
{
DestroyTree(T->rchild);
}
free(T);
T = NULL;
}
}
/***************** 执行测试 *************************/
int main(int argc, char const *argv[])
{
BTreePtr T;
T = NULL;
int a[] = {33, 16, 50, 13, 18, 34, 58, 15, 17, 25, 51, 66, 19, 27, 55};
int i;
for (i = 0; i < 15; i++) {
Insert(&T, a[i]);
}
printf("Max is %d\n", FindMax(T));
printf("Min is %d\n", FindMin(T));
Delete(T, 18);
Delete(T, 13);
PreOrderTraverse(T);
DestroyTree(T);
return 0;
}