递归实现二叉查找树

一、头文件:

#ifndef _BIN_TREE_TEST_
#define _BIN_TREE_TEST_

struct treenode;
typedef struct treenode*  searchTree;
typedef int elementType;


searchTree MakeEmpty(searchTree t);
searchTree Find(elementType x,searchTree t);
searchTree FindMin(searchTree t);
searchTree FindMax(searchTree t);
searchTree Insert(elementType x,searchTree t);
searchTree Delete(elementType x,searchTree t);
elementType Retrieve(searchTree position);

void PrintElement_pre(searchTree t);
void PrintElement_mid(searchTree t);
void PrintElement_aft(searchTree t);


struct treenode
{
    elementType data;
    searchTree left;
    searchTree right;
};

#endif



二、C文件:

#include <stdio.h>
#include <stdlib.h>
#include "bin_tree.h"


searchTree MakeEmpty(searchTree t)
{
    if(t)
    {
	MakeEmpty(t->left);
	MakeEmpty(t->right);
  	free(t);
    }

    return NULL;
}

searchTree Find(elementType x,searchTree t)
{
    if(t == NULL)
    	return NULL;
    if(x < t->data)
	return Find(x,t->left);
    else if(x > t->data)
	return Find(x,t->right);
    return t;
}

searchTree FindMin(searchTree t)
{
    if(t == NULL)
    	return NULL;
    else if(t->left == NULL)
	return t;
    else 
	return FindMin(t->left);
}

searchTree FindMax(searchTree t)
{
    if(t == NULL)
	return NULL;
    else if(t->right == NULL)
	return t;
    else 
	return FindMax(t->right);
}

searchTree Insert(elementType x,searchTree t)
{
    if(t == NULL)
    {
	t = (searchTree)malloc(sizeof(struct treenode));
	if(t == NULL)
	{
	    printf("alloca space error\n");
	    exit(-2);
	}

	t->data = x;
	t->left = NULL;
	t->right = NULL;
	//return t;
    }    

    else if(x < t->data)
    {
 	t->left = Insert(x,t->left);
 //	Insert(x,t->left);
    }
    else if(x > t->data)
    {
	t->right = Insert(x,t->right);
	//Insert(x,t->right);
    }
    return t;
}

searchTree Delete(elementType x,searchTree t)
{
    searchTree tmpnode = NULL;

    if(t == NULL)
	return NULL;
    else if(x < t->data)
	t->left = Delete(x,t->left);
    else if(x > t->data)
	t->right = Delete(x,t->right);
    else if(t->left && t->right)
    {	
	tmpnode = FindMin(t->right);
	t->data = tmpnode->data;
	t->right = Delete(tmpnode->data,tmpnode);
    }	
    else 
    {
	tmpnode = t;
       	if(t->right == NULL)
	    t = t->left;
	else if( t->left == NULL)
	    t = t->right;

	free(tmpnode);	
    }	

    return t;
}

elementType Retrieve(searchTree position)
{
    return position->data;
}



void PrintElement_pre(searchTree t)
{
    static int i = 0;
    if(t != NULL)
    {
	PrintElement_pre(t->left);
	printf("in pre,the %d element is %d\n",++i,t->data);
	PrintElement_pre(t->right);
    }
}


void PrintElement_mid(searchTree t)
{
    static int i = 0;
    if(t != NULL)
    {
	printf("int mid,the %d element is %d\n",++i,t->data);
	PrintElement_mid(t->left);
	PrintElement_mid(t->right);
    }
}

void PrintElement_aft(searchTree t)
{
    static int i = 0;
    if(NULL != t)
    {
	PrintElement_aft(t->left);
	PrintElement_aft(t->right);
	printf("in aft,the %d element is %d\n",++i,t->data);
    }
}

int main(int argc,char *argv[])
{
    searchTree t = NULL;
    searchTree position = NULL;
    static int first_insert = 1;
    //t = MakeEmpty(t);
    int i = 0;
    int x;
    int a[20] = {500,697,864,123,685,987,23,68,99,45,
		 66,456,568,999,1024,333,589,12,6,36};
    while(i < 20)
    {
	position = Insert(a[i],t);
	i++;
	if(first_insert)
	{
	    t = position;
	    first_insert = 0;
 	}
	printf("the insert address is %p\n",position);
	printf("the insert data is :%d\n",Retrieve(position));
    }

    printf("the t address is %p,the t->data is %d \n",t,t->data);

    printf("\n");    
    PrintElement_pre(t);
    printf("\n");    
    PrintElement_mid(t);
    printf("\n");    
    PrintElement_aft(t);
    printf("\n");    

    position = FindMin(t);
    printf("the min data is %d\n",position->data);
    position = FindMax(t);
    printf("the max data is %d\n",position->data);	

    i = 0;
    while( i < 20)
    {
        position = Delete(a[i],t);
	sleep(1);
	printf("the delete data is :%d\n",a[i]);
	printf("the position is %p\n",position);
	i++;
    }
    
}



三、打印输出:

the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the insert address is 0x80f3008
the insert data is :500
the t address is 0x80f3008,the t->data is 500 

in pre,the 1 element is 6
in pre,the 2 element is 12
in pre,the 3 element is 23
in pre,the 4 element is 36
in pre,the 5 element is 45
in pre,the 6 element is 66
in pre,the 7 element is 68
in pre,the 8 element is 99
in pre,the 9 element is 123
in pre,the 10 element is 333
in pre,the 11 element is 456
in pre,the 12 element is 500
in pre,the 13 element is 568
in pre,the 14 element is 589
in pre,the 15 element is 685
in pre,the 16 element is 697
in pre,the 17 element is 864
in pre,the 18 element is 987
in pre,the 19 element is 999
in pre,the 20 element is 1024

int mid,the 1 element is 500
int mid,the 2 element is 123
int mid,the 3 element is 23
int mid,the 4 element is 12
int mid,the 5 element is 6
int mid,the 6 element is 68
int mid,the 7 element is 45
int mid,the 8 element is 36
int mid,the 9 element is 66
int mid,the 10 element is 99
int mid,the 11 element is 456
int mid,the 12 element is 333
int mid,the 13 element is 697
int mid,the 14 element is 685
int mid,the 15 element is 568
int mid,the 16 element is 589
int mid,the 17 element is 864
int mid,the 18 element is 987
int mid,the 19 element is 999
int mid,the 20 element is 1024

in aft,the 1 element is 6
in aft,the 2 element is 12
in aft,the 3 element is 36
in aft,the 4 element is 66
in aft,the 5 element is 45
in aft,the 6 element is 99
in aft,the 7 element is 68
in aft,the 8 element is 23
in aft,the 9 element is 333
in aft,the 10 element is 456
in aft,the 11 element is 123
in aft,the 12 element is 589
in aft,the 13 element is 568
in aft,the 14 element is 685
in aft,the 15 element is 1024
in aft,the 16 element is 999
in aft,the 17 element is 987
in aft,the 18 element is 864
in aft,the 19 element is 697
in aft,the 20 element is 500

the min data is 6
the max data is 1024
the delete data is :500
the position is 0x80f3008
the delete data is :697
the position is 0x80f3008
the delete data is :864
the position is 0x80f3008
the delete data is :123
the position is 0x80f3008
the delete data is :685
the position is 0x80f3008
the delete data is :987
the position is 0x80f3008
the delete data is :23
the position is 0x80f3008
the delete data is :68
the position is 0x80f3008
the delete data is :99
the position is 0x80f3008
the delete data is :45
the position is 0x80f3008
the delete data is :66
the position is 0x80f3008
the delete data is :456
the position is 0x80f3008
the delete data is :568
the position is 0x80f3008
the delete data is :999
the position is 0x80f3008
the delete data is :1024
the position is 0x80f3008
the delete data is :333
the position is 0x80f3008
the delete data is :589
the position is 0x80f3068
the delete data is :12
the position is 0x80f3008
the delete data is :6
the position is 0x80f3008
the delete data is :36
the position is 0x80f3008


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值