AOJ ALDS1_8_A: Binary Search Tree I

本文介绍了两种不同的二叉搜索树插入算法实现:一种是非递归方式,它通过遍历找到合适的位置插入新节点;另一种是递归方式,代码更为简洁易懂。这两种方法均能有效地将新元素插入到二叉搜索树中,并保持其特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两种二叉搜索树的插入算法

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <map>
using namespace std;
const int SIZE = 10005;

struct BSTNode
{
    int key;
    BSTNode *lchild, *rchild, *parent;
    BSTNode(int k = 0, BSTNode* p = nullptr, BSTNode* l = nullptr, BSTNode* r = nullptr)
        :key(k), parent(p), lchild(l), rchild(r){}
};
typedef BSTNode* BSTree;
BSTree insert(BSTree& Root, int key)
{
    BSTree parent = nullptr, child = Root;

    while (child != nullptr)
    {
        parent = child;
        if (key > child->key)
            child = child->rchild;
        else
            child = child->lchild;
    }
    child = new BSTNode(key, parent);
    if (parent == nullptr)
        Root = child;
    else if (key > parent->key)
        parent->rchild = child;
    else
        parent->lchild = child;
    return child;
}

void pre_order(const BSTree T)
{
    if (!T)
        return;
    cout << ' ' << T->key;
    pre_order(T->lchild);
    pre_order(T->rchild);
}

void in_order(const BSTree T)
{
    if (!T)
        return;

    in_order(T->lchild);
    cout << ' ' << T->key;
    in_order(T->rchild);
}
int main(void)
{
    ios::sync_with_stdio(false);
    int n, key;
    char str[10];
    cin >> n;
    BSTree T = nullptr;
    while (n--)
    {
        cin >> str;
        if (str[0] == 'i')
        {
            cin >> key;
            insert(T, key);
        }
        else
        {
            in_order(T);
            cout << endl;
            pre_order(T);
            cout << endl;
        }
    }
    return 0;
}
/*
递归实现的要更简洁也容易理解
*/
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <map>
using namespace std;
const int SIZE = 10005;

struct BSTNode
{
    int key;
    BSTNode *lchild, *rchild;
    BSTNode(int k = 0, BSTNode* l = nullptr, BSTNode* r = nullptr)
        :key(k), lchild(l), rchild(r){}
};
typedef BSTNode* BSTree;
BSTree insert(BSTree& T, int key)
{
    if (!T)
    {
        T = new BSTNode(key);
        return T;
    }
    else if (key > T->key)
        T->rchild = insert(T->rchild, key);
    else
        T->lchild = insert(T->lchild, key);
    return T;
}

void pre_order(const BSTree T)
{
    if (!T)
        return;
    cout << ' ' << T->key;
    pre_order(T->lchild);
    pre_order(T->rchild);
}

void in_order(const BSTree T)
{
    if (!T)
        return;

    in_order(T->lchild);
    cout << ' ' << T->key;
    in_order(T->rchild);
}
int main(void)
{
    ios::sync_with_stdio(false);
    int n, key;
    int Tsize;
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    char str[10];
    cin >> n;
    BSTree T = nullptr;
    while (n--)
    {
        cin >> str;
        if (str[0] == 'i')
        {
            cin >> key;
            insert(T, key);
        }
        else
        {
            in_order(T);
            cout << endl;
            pre_order(T);
            cout << endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值