二叉查找树简单实现
2017-09-11 22:53
197 查看
二叉查找树其实在实际中起效率非常高,特别在于处理其中的数据。实际工作中我认为会有用处的:
1、中序排列是一个按照从到大的序列来实现的;
2、删除一个元素,会在右子树的最小的一个元素来进行补上其位置,让其中的数据结构不变;
3、删除元素后数据结构会有变化,数据最坏遍历查询是:O(N)平均是O(logN)
实现的代码:
#include <stdio.h>
#include <mm_malloc.h>
typedef int Element;
typedef struct node{
Element data;
struct
node *lchild;
struct
node *rchild;
}node;
#define InISize 20
#define AppSize 50
typedef struct Stack{
int Capacity;
node*base;
node*top;
}Stack;
void IniStack(Stack *s)
{
s->base=(node*)malloc(InISize*sizeof(node));
if(!s->base)
return ;
s->top=s->base;
s->Capacity=InISize;
}
int EmptyStack(Stack *s)
{
if(s->base==s->top)
{
return 1;
}
else
return 2;
}
void ClearStack(Stack *s)
{
s->top=s->base;
}
void Push(Stack *s,node d)
{
if(s->top-s->base>=s->Capacity)
{
s->base=(node *)realloc(s->base,AppSize*sizeof(node));
if(!s->base)
return ;
s->top=s->base+s->Capacity;
s->Capacity=AppSize+InISize;
}
*(s->top)=d;
s->top++;
}
void pop(Stack *s,node *D)
{
if(s->top==s->base)
return ;
s->top--;
*D=*(s->top);
}
void destroyStack(Stack *s)
{
free(s->base);
s->top=s->base;
s->Capacity=0;
}
int StackSize(Stack *s)
{
return (int)(s->top-s->base);
}
//最小的就是左子树的最左的叶子
node * findmin(node * tree)
{
node *p=tree;
while(p->lchild!=NULL)
{
p=p->lchild;
}
return p;
}
//最大的就是右子树的最右的叶子
node * findMax(node * tree)
{
node *p=tree;
while(p->rchild!=NULL)
{
p=p->rchild;
}
return p;
}
node * searchTree_insert(Element x,node *tree)
{
if(tree==NULL)
{
//插入穿件二叉查找树
if((tree=(node * )malloc(sizeof(node)))!=NULL)
{
tree->data=x;
tree->lchild=NULL;
tree->rchild=NULL;
}
else
{
printf("申请数据错误!");
}
}
else
if(x>tree->data)
{
tree->rchild=searchTree_insert(x, tree->rchild);
}
else
if(x<tree->data)
{
tree->lchild=searchTree_insert(x, tree->lchild);
}
return tree;
}
//非递归中序遍历实现
void MidPrint(node * tree)
{
node *p=tree;
Stack S;
IniStack(&S);
while(p||EmptyStack(&S)!=1)
{
if(p)
{
Push(&S, *p);
p=p->lchild;
}
else
{
p=(node *)malloc(sizeof(node));
pop(&S, p);
printf("%d \t",p->data);
p=p->rchild;
}
}
}
void previousPrint(node *tree)
{
node *p=tree;
Stack S;
IniStack(&S);
while(p||EmptyStack(&S)!=1)
{
if(p)
{
printf("%d \t",p->data);
Push(&S, *p);
p=p->lchild;
}
else
{
p=(node *)malloc(sizeof(node));
pop(&S, p);
p=p->rchild;
}
}
}
node * deleteElement(Element X,node *tree)
{
node *p;
if(tree==NULL)
{
printf("error \n");
}
else
if(X>tree->data)
{
tree->rchild=deleteElement(X, tree->rchild);
}
else
if(X<tree->data)
{
tree->lchild=deleteElement(X, tree->lchild);
}
if(tree->lchild&&tree->rchild)
{
p=findmin(tree->rchild);
tree->data=p->data;
tree->rchild=deleteElement(tree->data, tree->rchild);
}
else
{
p=tree;
if(tree->lchild==NULL)
tree=tree->rchild;
if(tree->rchild==NULL)
tree=tree->lchild;
free(p);
}
return tree;
}
int findElement(Element X,node * tree)
{
int res=0;
if(tree==NULL)
{
return res;
}
else
{
if(X>tree->data)
findElement(X, tree->lchild);
if(X<tree->data)
findElement(X, tree->rchild);
if(X==tree->data)
res=1;
}
return res;
}
int main(int argc,
const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
int a[]={12,3,4,5,7,11,45,34,56,2,9,10};
node *p=NULL;
for(int i=0;i<12;i++)
{
p=searchTree_insert(a[i], p);
}
printf("前序序遍历:");
previousPrint(p);
printf("\n");
printf("中序序遍历:");
MidPrint(p);
printf("\n");
int flag=findElement(12, p);
if(flag==1)
printf("\n12 - find");
else
printf("no-find");
printf("\n");
return 0;
}
1、中序排列是一个按照从到大的序列来实现的;
2、删除一个元素,会在右子树的最小的一个元素来进行补上其位置,让其中的数据结构不变;
3、删除元素后数据结构会有变化,数据最坏遍历查询是:O(N)平均是O(logN)
实现的代码:
#include <stdio.h>
#include <mm_malloc.h>
typedef int Element;
typedef struct node{
Element data;
struct
node *lchild;
struct
node *rchild;
}node;
#define InISize 20
#define AppSize 50
typedef struct Stack{
int Capacity;
node*base;
node*top;
}Stack;
void IniStack(Stack *s)
{
s->base=(node*)malloc(InISize*sizeof(node));
if(!s->base)
return ;
s->top=s->base;
s->Capacity=InISize;
}
int EmptyStack(Stack *s)
{
if(s->base==s->top)
{
return 1;
}
else
return 2;
}
void ClearStack(Stack *s)
{
s->top=s->base;
}
void Push(Stack *s,node d)
{
if(s->top-s->base>=s->Capacity)
{
s->base=(node *)realloc(s->base,AppSize*sizeof(node));
if(!s->base)
return ;
s->top=s->base+s->Capacity;
s->Capacity=AppSize+InISize;
}
*(s->top)=d;
s->top++;
}
void pop(Stack *s,node *D)
{
if(s->top==s->base)
return ;
s->top--;
*D=*(s->top);
}
void destroyStack(Stack *s)
{
free(s->base);
s->top=s->base;
s->Capacity=0;
}
int StackSize(Stack *s)
{
return (int)(s->top-s->base);
}
//最小的就是左子树的最左的叶子
node * findmin(node * tree)
{
node *p=tree;
while(p->lchild!=NULL)
{
p=p->lchild;
}
return p;
}
//最大的就是右子树的最右的叶子
node * findMax(node * tree)
{
node *p=tree;
while(p->rchild!=NULL)
{
p=p->rchild;
}
return p;
}
node * searchTree_insert(Element x,node *tree)
{
if(tree==NULL)
{
//插入穿件二叉查找树
if((tree=(node * )malloc(sizeof(node)))!=NULL)
{
tree->data=x;
tree->lchild=NULL;
tree->rchild=NULL;
}
else
{
printf("申请数据错误!");
}
}
else
if(x>tree->data)
{
tree->rchild=searchTree_insert(x, tree->rchild);
}
else
if(x<tree->data)
{
tree->lchild=searchTree_insert(x, tree->lchild);
}
return tree;
}
//非递归中序遍历实现
void MidPrint(node * tree)
{
node *p=tree;
Stack S;
IniStack(&S);
while(p||EmptyStack(&S)!=1)
{
if(p)
{
Push(&S, *p);
p=p->lchild;
}
else
{
p=(node *)malloc(sizeof(node));
pop(&S, p);
printf("%d \t",p->data);
p=p->rchild;
}
}
}
void previousPrint(node *tree)
{
node *p=tree;
Stack S;
IniStack(&S);
while(p||EmptyStack(&S)!=1)
{
if(p)
{
printf("%d \t",p->data);
Push(&S, *p);
p=p->lchild;
}
else
{
p=(node *)malloc(sizeof(node));
pop(&S, p);
p=p->rchild;
}
}
}
node * deleteElement(Element X,node *tree)
{
node *p;
if(tree==NULL)
{
printf("error \n");
}
else
if(X>tree->data)
{
tree->rchild=deleteElement(X, tree->rchild);
}
else
if(X<tree->data)
{
tree->lchild=deleteElement(X, tree->lchild);
}
if(tree->lchild&&tree->rchild)
{
p=findmin(tree->rchild);
tree->data=p->data;
tree->rchild=deleteElement(tree->data, tree->rchild);
}
else
{
p=tree;
if(tree->lchild==NULL)
tree=tree->rchild;
if(tree->rchild==NULL)
tree=tree->lchild;
free(p);
}
return tree;
}
int findElement(Element X,node * tree)
{
int res=0;
if(tree==NULL)
{
return res;
}
else
{
if(X>tree->data)
findElement(X, tree->lchild);
if(X<tree->data)
findElement(X, tree->rchild);
if(X==tree->data)
res=1;
}
return res;
}
int main(int argc,
const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
int a[]={12,3,4,5,7,11,45,34,56,2,9,10};
node *p=NULL;
for(int i=0;i<12;i++)
{
p=searchTree_insert(a[i], p);
}
printf("前序序遍历:");
previousPrint(p);
printf("\n");
printf("中序序遍历:");
MidPrint(p);
printf("\n");
int flag=findElement(12, p);
if(flag==1)
printf("\n12 - find");
else
printf("no-find");
printf("\n");
return 0;
}
相关文章推荐
- LintCode_二叉查找树中搜索区间_简单的递归实现
- 二叉查找树的简单实现
- 简单数据结构实现——二叉查找树
- 二叉查找树的简单实现
- 二叉查找树的简单模板实现
- 二叉查找树的简单实现
- 二叉查找树的简单模板实现
- 二叉查找树的简单实现(C语言版)
- 一个简单的二叉查找树实现
- 二叉查找树(简单C实现)
- 一颗二叉查找树的简单实现
- 二叉查找树简单实现
- 二叉查找树的插入和删除简单实现
- 二叉查找树的简单C++实现
- java 数据结构 二叉查找树的简单实现
- 二叉查找树简单实现
- 简单数据结构实现——二叉查找树
- [数据结构]二叉查找树 简单实现
- 二叉查找树的简单实现
- cocos2d-x简单游戏<打飞机>代码实现|第四部分:主场景<Helloworld.m>