#include "binTree.h"
#include <stdio.h>
#include <stdlib.h>
binTree emptyBinTree()
{
binTree t=malloc(sizeof(*t));
t->data=0;
t->leftChild=t->rightChild=NULL;
return t;
}
binTree newBinTree(int key)
{
binTree t=malloc(sizeof(*t));
t->data=key;
t->leftChild=t->rightChild=NULL;
return t;
}
binTree search(binTree root,int key)
{
if(!(root->data))
return NULL;
else
{
if(key==root->data)
return root;
if(key<root->data)
return search(root->leftChild,key);
return search(root->rightChild,key);
}
}
binTree modifiedSearch(binTree tree,int key)
{
binTree t=tree;
if(t->data)
{
while(t->leftChild||t->rightChild)
{
if(key==t->data)
t=NULL;
else
if(key<t->data&&t->leftChild)
t=t->leftChild;
else
if(key>t->data&&t->rightChild)
t=t->rightChild;
else
break;
}
}
return t;
}
void binTreeInsert(binTree t,int key)
{
binTree ptr=newBinTree(key);
binTree temp=modifiedSearch(t,key);
if(temp->data)
{
if(key<temp->data)
temp->leftChild=ptr;
else
temp->rightChild=ptr;
}
else
temp->data=ptr->data;
}
void binTreeDelete(binTree t,int key)
{
binTree a=emptyBinTree();
binTree b=emptyBinTree();
t=search(t,key);
if(t->leftChild==NULL&&t->rightChild==NULL)
{
t=NULL;
free(t);
}
else
{
if(t->leftChild==NULL)
{
b=t;
t=t->rightChild;
b->data=0;
}
if(t->rightChild==NULL)
{
b=t;
t=t->leftChild;
b->data=0;
}
else
{
a=t;
b=t->leftChild;
while(b->rightChild)
{
a=b;
b=b->rightChild;
}
a->data=b->data;
if(a!=t)
t->rightChild=b->leftChild;
else
t->leftChild=b->leftChild;
b->data=0;
}
}
}
void binTreeVisit(binTree t)
{
printf("%4d",t->data);
}
void binTreeInorder(binTree t,visitTy visit)
{
binTree a=t;
if(a)
{
binTreeInorder(a->leftChild,visit);
if(a->data)
visit(a);
binTreeInorder(a->rightChild,visit);
}
}
void swap(int *pm,int *pn) /*必须用指针进行交换*/
{
int temp;
temp=*pm;
*pm=*pn;
*pn=temp;
}
没有合适的资源?快使用搜索试试~ 我知道了~
二叉查找树的C语言实现——插入,删除,查找
共3个文件
c:2个
h:1个
4星 · 超过85%的资源 需积分: 10 52 下载量 43 浏览量
2008-11-21
16:14:03
上传
评论 3
收藏 2KB RAR 举报
温馨提示
二叉查找树的C语言实现,实现功能:插入、删除、查找。
资源详情
资源评论
资源推荐
收起资源包目录
binary_serch_tree.rar (3个子文件)
main.c 1KB
binTree.h 465B
binTree.c 2KB
共 3 条
- 1
lxm_623
- 粉丝: 10
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1