数据结构C++版 王红梅 OJ习题

1031: 二叉排序树(1)-递归方式

Description

/*已知二叉排序树BiSortTree的部分代码如下,勿改动。

请在此基础上补充实现递归查找算法SearchBST和InsertBST。*/

#include <iostream>

using namespace std;

//BiNode

template <typename DataType>

struct BiNode

{

DataType data;

BiNode *lchild, *rchild;

};



//BiSortTree

template <typename DataType>

class BiSortTree

{

public:

BiSortTree(DataType r[],int n);    //建立关键字序列r[0]~r[n-1]的二叉排序树

~BiSortTree( );       //析构函数,释放二叉排序树中所有结点,同二叉链表的析构函数

    void InsertBST(BiNode<DataType>*&bt,DataType key);    //插入key

BiNode<DataType>* SearchBST(BiNode<DataType> *bt,DataType key);   //查找值为k的结点,返回值为k所在结点的地址

void InOrderBST(BiNode<DataType> *bt);   //中序遍历二叉树(递归)

    BiNode<DataType> *GetRoot();//获取root值



private:

    BiNode<DataType> *root;    //二叉排序树(即二叉链表)的根指针

    void Release(BiNode<DataType> *&bt);   //析构函数调用

};



//构造函数,将r[0]~r[n-1]各个元素依次插入,生成一棵二叉排序树

template <typename DataType>

BiSortTree<DataType>::BiSortTree(DataType r[],int n)//构造函数

{

root=nullptr;  //初始化空二叉树

int i;

for(i=0;i<n;i++){   //进行n次插入

InsertBST(root,r[i]);//将结点s插入到二叉排序树中

}

}



//析构函数,调用Release释放内存

template <typename DataType>

BiSortTree<DataType>::~BiSortTree( )

{

    Release(root);

}



//释放二叉排序树的存储空间,调用析构函数实现

template <typename DataType>

void BiSortTree<DataType>::Release(BiNode<DataType>* &bt)

{

if (bt){

Release(bt->lchild);   //释放左子树

Release(bt->rchild);   //释放右子树

delete bt;

}

}



template <typename DataType>

BiNode<DataType> *BiSortTree<DataType>::GetRoot()

{

return root;

}



template <typename DataType>

void  BiSortTree<DataType>::InOrderBST(BiNode<DataType>*bt)

{

if(bt==nullptr) return;

else

{

InOrderBST(bt->lchild);

cout<<bt->data<<" ";

InOrderBST(bt->rchild);

}

}



//在下面补充实现相关算法





//二叉排序树的主函数

int main( ){

    int a[10],n;

n=0;

while(1)

{

cin>>a[n];

if(!a[n])break;  //输入数据以0结束

n++;

}

BiSortTree<int> bst(a,n);//构造二叉排序树

    BiNode<int> *root;

root=bst.GetRoot();

cout<<"Inorder:";

bst.InOrderBST(root);//中序遍历二叉排序树(得到递增有序序列)

cout<<endl;

int x;

cin>>x;

BiNode<int> *s;

    s=bst.SearchBST(root,x);

if(s==nullptr) cout<<"Find "<<x<<" failure";

else cout<<"Find "<<x<<" success";

cout<<endl;

return 0;

}

Input

Output

Sample Input

3 42 4 32 42 42 32 56 37 0 32

Sample Output

Inorder:3 4 32 37 42 56 
Find 32 success
//
// Created by Legends丶Hu on 2020/2/6.
//
#include <iostream>

using namespace std;
template<class T>
struct BiNode {
    T data;
    BiNode *lchild, *rchild;
};

//BiSortTree
template<class T>
class BiSortTree {
public:
    BiSortTree(T r[], int n);    //建立关键字序列r[0]~r[n-1]的二叉排序树
    ~BiSortTree();       //析构函数,释放二叉排序树中所有结点,同二叉链表的析构函数
    void InsertBST(BiNode<T> *&root, BiNode<T> *s);    //插入结点*s
    BiNode<T> *SearchBST(BiNode<T> *root, T k);   //查找值为k的结点,返回值为k所在结点的地址
    void InOrderBST(BiNode<T> *root);   //中序遍历二叉树(递归)
    BiNode<T> *GetRoot()  //获取root值
    {
        return root;
    }

private:
    BiNode<T> *root;    //二叉排序树(即二叉链表)的根指针
    void Release(BiNode<T> *&root);   //析构函数调用
};

//构造函数,将r[0]~r[n-1]各个元素依次插入,生成一棵二叉排序树
template<class T>
BiSortTree<T>::BiSortTree(T r[], int n)//构造函数
{
    root = nullptr;  //初始化空二叉树
    int i;
    for (i = 0; i < n; i++) {   //进行n次插入
        BiNode<T> *s;
        s = new BiNode<T>;
        s->data = r[i];
        s->lchild = s->rchild = nullptr;
        InsertBST(root, s);//将结点s插入到二叉排序树中
    }
}

//析构函数,调用Release释放内存
template<class T>
BiSortTree<T>::~BiSortTree() {
    Release(root);
}

//释放二叉排序树的存储空间,调用析构函数实现
template<class T>
void BiSortTree<T>::Release(BiNode<T> *&root) {
    if (root) {
        Release(root->lchild);   //释放左子树
        Release(root->rchild);   //释放右子树
        delete root;
    }
}

template<class T>
void BiSortTree<T>::InsertBST(BiNode<T> *&root, BiNode<T> *s) {
    if (root == nullptr) root = s;
    else if (root->data < s->data)
        InsertBST(root->rchild, s);
    else if(root->data > s->data)
        InsertBST(root->lchild, s);
    return;
}

template<class T>
BiNode<T> *BiSortTree<T>::SearchBST(BiNode<T> *root, T k) {
    if (root) {
        if (root->data < k)
            return SearchBST(root->rchild, k);
        else if (root->data > k)
            return SearchBST(root->lchild, k);
        else return root;
    }
    return nullptr;
}

template<class T>
void BiSortTree<T>::InOrderBST(BiNode<T> *root) {
    if (root) {
        InOrderBST(root->lchild);
        cout << root->data << " ";
        InOrderBST(root->rchild);
    }
}

//3 42 4 32 42 42 32 56 37 0 32
//18 73 10 5 68 99 27 41 51 32 25 0 24
//二叉排序树的主函数
int main() {
    int a[10], n;
    n = 0;
    while (1) {
        cin >> a[n];
        if (!a[n])break;  //输入数据以0结束
        n++;
    }
    BiSortTree<int> bst(a, n);//构造二叉排序树
    BiNode<int> *root;
    root = bst.GetRoot();
    cout << "Inorder:";
    bst.InOrderBST(root);//中序遍历二叉排序树(得到递增有序序列)
    cout << endl;
    int x;
    cin >> x;
    BiNode<int> *s;
    s = bst.SearchBST(root, x);
    if (s == nullptr) cout << "Find " << x << " failure";
    else cout << "Find " << x << " success";
    cout << endl;
    return 0;
}

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值