二叉查找树简单实现
来源:互联网 发布:vscode 小程序插件 编辑:程序博客网 时间:2024/05/29 07:01
二叉查找树其实在实际中起效率非常高,特别在于处理其中的数据。实际工作中我认为会有用处的:
1、中序排列是一个按照从到大的序列来实现的;
2、删除一个元素,会在右子树的最小的一个元素来进行补上其位置,让其中的数据结构不变;
3、删除元素后数据结构会有变化,数据最坏遍历查询是:O(N)平均是O(logN)
实现的代码:
#include <stdio.h>
#include <mm_malloc.h>
typedef int Element;
typedef struct node{
Element data;
structnode *lchild;
structnode *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;
}
- 二叉查找树简单实现
- 二叉查找树的简单模板实现
- 二叉查找树的简单实现
- 二叉查找树的简单C++实现
- 二叉查找树(简单C实现)
- 一个简单的二叉查找树实现
- [数据结构]二叉查找树 简单实现
- 简单查找二叉树的实现
- 二叉查找树的简单实现
- 二叉查找树的简单实现
- 二叉查找树实现
- 二叉查找树实现
- 二叉查找树实现
- 二叉查找树实现
- 简单数据结构实现——二叉查找树
- 二叉查找树的插入和删除简单实现
- 简单数据结构实现——二叉查找树
- 二叉查找树的简单实现(C语言版)
- next-permutation
- psql- 致命错误 角色 highggo 不存在
- JavaScript必须熟悉知道的基础整理(一)
- bzoj 1653: [Usaco2006 Feb]Backward Digit Sums(全排列)
- 数据类型与运算符
- 二叉查找树简单实现
- 简单实现部分HTML表单校验功能
- 循环语句&条件语句
- 第二章 Oralce 11g 体系结构
- J2EE Web核心技术课后答案
- kali的安装
- ssm框架系列(8)-关联映射
- SQL语句使用03------连接
- 链表