BST的实现(二叉搜索树)

void Inorder(struct Tree *T); //中序 

void Preorder(struct Tree *T); //前序

void Postorder(struct Tree *T); //后序
struct Tree * InsertTree(struct Tree * T, int z);
struct Tree * Delete(struct Tree *T, int z);
struct Tree * FindMin(struct Tree *T);
struct Tree * FindMax(struct Tree *T);
DataType Maximum(struct Tree *T);
DataType Minimum(struct Tree *T);
bool Search(struct Tree * T, int z);

 

就这些功能

#include <iostream>
#include <cstdlib>
using namespace std;
typedef int DataType;
struct Tree
{
    //struct Tree *Parent;
    struct Tree *Left;
    struct Tree *Right;
    DataType key;
};
void Inorder(struct Tree *T);        //中序 
void Preorder(struct Tree *T);        //前序
void Postorder(struct Tree *T);        //后序 
struct Tree * InsertTree(struct Tree * T, int z);
struct Tree * Delete(struct Tree *T, int z);
struct Tree * FindMin(struct Tree *T);
struct Tree * FindMax(struct Tree *T);
DataType Maximum(struct Tree *T);
DataType Minimum(struct Tree *T);
bool Search(struct Tree * T, int z);
struct Tree *s;


int main()
{
    s=InsertTree(s, 12);
    s=InsertTree(s, 2);
    s=InsertTree(s, 5);
    s=InsertTree(s, 13);
    s=InsertTree(s, 17);
    s=InsertTree(s, 18);
    s=InsertTree(s, 15);
    s=InsertTree(s, 19);
    cout << "It's the result of Preorder tree walk.\n";
    Preorder(s);
    cout << "It's the result of inorder tree walk.\n";
    Inorder(s);
    cout << "It's the result of Postorder tree walk.\n";
    Postorder(s);
    cout << "The maximum element is ";
    cout << Maximum(s) << endl;
    cout << "The minimum element is ";
    cout << Minimum(s) << endl;
    Delete(s, 17);
    cout << "It's the result of Preorder tree walk.\n";
    Preorder(s);
    cout << "It's the result of inorder tree walk.\n";
    Inorder(s);
    cout << "It's the result of Postorder tree walk.\n";
    Postorder(s);
    return 0;
} 

bool Search(struct Tree * T, int z)
{
    
    if(z<T->key)
    Search(T->Left, z);
    else if(z>T->key)
    Search(T->Right, z);
    else if(z==T->key)
    return true;
    else
    return false;
}



struct Tree * InsertTree(struct Tree * T, int z)
{
    if(T==NULL)
    {
        T=(struct Tree *)malloc(sizeof(struct Tree*));
        T->key=z;
        T->Left=T->Right=NULL;
    }
    if(z<T->key)
    T->Left=InsertTree(T->Left, z);
    else if(z>T->key)
    T->Right=InsertTree(T->Right, z);
    return T;
    
}

void Inorder(struct Tree *T)
{
    /*if(T!=NULL)
    {
        if(T->Left)
        Inorder(T->Left);
        cout << T->key << endl;
        if(T->Right)
        Inorder(T->Right);
    }*/
    if(T)
    {
        Inorder(T->Left);
        cout << T->key << endl;
        Inorder(T->Right);
    }
} 

void Preorder(struct Tree *T)
{
    if(T!=NULL)
    {
        cout << T->key << endl;
        if(T->Left)
        Preorder(T->Left);
        if(T->Right)
        Preorder(T->Right);
    }
}


void Postorder(struct Tree *T)
{
    if(T!=NULL)
    {
        if(T->Left)
        Postorder(T->Left);
        if(T->Right)
        Postorder(T->Right);
        cout << T->key << endl;
    }
}




DataType Minimum(struct Tree *T)
{
    while(T->Left)
    T=T->Left;
    return T->key;
}


DataType Maximum(struct Tree *T)
{
    while(T->Right)
    T=T->Right;
    return T->key;
    
}

struct Tree * Delete(struct Tree *T, int z)
{
    struct Tree *Tmp;
    if(T==NULL)
    cout << "We don't have enough node to delete!\n";
    else 
    if(z<T->key)
    T->Left=Delete(T->Left, z);
    else 
    if(z>T->key)
    T->Right=Delete(T->Right, z);
    else if(T->Left&&T->Right)
    {
        Tmp=FindMin(T->Right);
        T->key=Tmp->key;
        T->Right=Delete(T->Right, T->key);
    }
    else 
    {
        Tmp=T;
        if(T->Left==NULL)
        T=T->Right;
        else if(T->Right==NULL)
        T=T->Left;
        free(Tmp);
    }
    return T;
}


struct Tree * FindMin(struct Tree *T)
{
    while(T->Left)
    T=T->Left;
    return T;
}

struct Tree * FindMax(struct Tree *T)
{
    while(T->Right)
    T=T->Right;
    return T;
}
View Code

 

转载于:https://www.cnblogs.com/jmzIT/p/10244948.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值