数据结构——二叉查找树

  二叉查找树或者是一棵空树,或者是具有下列性质的二叉树:
1、每个结点都有一个作为查找依据的关键码,所有结点的关键码互不相同。
2、左子树(如果存在)上所有结点的关键码都小于根结点的关键码。
3、右子树(如果存在)上所有结点的关键码都大于根结点的关键码。
4、左子树和右子树也是二叉查找树。

BSTreeMain.c

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

int  main( int  argc,  char   ** argv)
{
    
int i;
    ElemType n, a[
10]={4132169101487};
    BSTree tmp, head;

    head 
= NULL;
    tmp 
= NULL; 
    
for(i = 0; i < 10; i++)
        TreeInsert(
&head, TreeNodeCreate(a[i]));
    InOrderTreeWalk(head);
    printf(
" ");

    
while(scanf("%d",&n)==1 && n)
    
{
        tmp 
= TreeSearch(head, n);    
        PrintNodeKey(tmp);
        
if(tmp != NULL)
        
{
            TreeDelete(
&head, tmp);
        }


        InOrderTreeWalk(head);
        printf(
" ");
    }


    
return 0;
}

bstree.h
#ifndef _BSTREE_H
#define  _BSTREE_H

typedef 
int  ElemType;
typedef 
struct  BSTNode {
    ElemType key;
    
struct BSTNode *left;
    
struct BSTNode *right;
    
struct BSTNode *p;
}
BSTNode,  * BSTree;

BSTree TreeNodeCreate(ElemType k);
void  InOrderTreeWalk(BSTree t);
BSTree TreeSearch(BSTree t, ElemType k);
BSTree TreeMinimum(BSTree t);
BSTree TreeMaximum(BSTree t);
BSTree TreeRredecessor(BSTree t);
BSTree TreeSuccessor(BSTree t);
int  TreeInsert(BSTree  * t, BSTree z);
int  TreeDelete(BSTree  * t, BSTree z);
void  PrintNodeKey(BSTree bst);

#endif

bstree.c
#include  < stdio.h >
#include 
< stdlib.h >
#include 
" bstree.h "

void  InOrderTreeWalk(BSTree t)
{
    
if(t != NULL)
    
{
        InOrderTreeWalk(t
->left);
        printf(
"%d ", t->key);
        InOrderTreeWalk(t
->right);
    }

}

/*
 *递归版本
 
*/

BSTree TreeSearch(BSTree t, ElemType k)
{
    
if(t == NULL || k == t->key)
        
return t;
    
if(k < t->key)
        
return TreeSearch(t->left, k);
    
else
        
return TreeSearch(t->right, k);
}

/*
 *非递归版本
 
*/

BSTree IterativeTreeSearch(BSTree t, ElemType k)
{
    
while(t != NULL && k != t->key)
    
{
        
if(k < t->key)
            t 
= t->left;
        
else
            t 
= t->right;
    }


    
return t;
}


BSTree TreeMinimum(BSTree t)
{
    
while(t->left != NULL)
        t 
= t->left;


    
return t;
}


BSTree TreeMaximum(BSTree t)
{
    
while(t->right != NULL)
        t 
= t->right;


    
return t;
}


BSTree TreeSuccessor(BSTree t)
{
    BSTree y;

    
if(t->right != NULL)
        
return TreeMinimum(t->right);
    y 
= t->p;
    
while(y != NULL && t == y->right)
    
{
        t 
= y;
        y 
= y->p;
    }


    
return y;
}


BSTree TreeRredecessor(BSTree t)
{
    BSTree y;

    
if(t->left != NULL)
        
return TreeMaximum(t->left);
    y 
= t->p;
    
while(y != NULL && t == y->left)
    
{
        t 
= y;
        y 
= y->p;
    }


    
return y;
}


int  TreeInsert(BSTree  * t, BSTree z)
{
    BSTree x, y;

    y 
= NULL;
    x 
= *t;
    
while(x != NULL)
    
{
        y 
= x;
        
if(z->key < x->key)
            x 
= x->left;
        
else
            x 
= x->right;
    }

    z
->= y;
    
if(y == NULL)
        
*= z;
    
else if(z->key < y->key)
        y
->left = z;
    
else
        y
->right = z;

    
return 1;
}


int  TreeDelete(BSTree  * t, BSTree z)
{
    BSTree x, y;

    
if(z->left == NULL || z->right == NULL)
        y 
= z;
    
else
        y 
= TreeSuccessor(&(**t));
    
if(y->left != NULL)
        x 
= y->left;
    
else
        x 
= y->right;
    
if(x != NULL)
        x 
->= y->p;
    
if(y->== NULL)
        (
*t) = x;
    
else if(y == y->p->left)
        y
->p->left = x;
    
else
        y
->p->right = x;
    
if(y != z)
        z
->key = y->key;
    free(y);
    y = NULL;

    
return 1;
}


BSTree TreeNodeCreate(ElemType k)
{
    BSTree tmp;

    tmp 
= (BSTree)malloc(sizeof(BSTNode));
    tmp
->key = k;
    tmp
->left = NULL;
    tmp
->= NULL;
    tmp
->right = NULL;

    
return tmp;
}


void  PrintNodeKey(BSTree bst)
{
    
if(bst == NULL)
        printf(
"It's a NULL node! ");
    
else
        printf(
"%d ", bst->key);
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值