二叉排序树的创建查找与插入(C++)

一、创建(插入):

  1. 递归
struct node{
    int val;
    node* left, *right;
    node() { ; }
    node(int x) :val(x), left(NULL), right(NULL) {}
};
node* insert(int x,node* pnode) {
    if (pnode == NULL) {
        pnode = new node(x);
        return pnode;
    }
    else {
        if (x < pnode->val) { //等于根节点不能插入!
            pnode->left = insert(x,pnode->left);
        }
        else {
            pnode->right = insert(x, pnode->right);
        }
        return pnode;
    }
}
void inorder(node*& root) {
    if (root == NULL) {
        return;
    }
    inorder(root->left);
    cout << root->val << ' '; //访问根节点
    inorder(root->right);
}
int main(){
	int n = 7;
    vector<int> a = {9,5,3,6,14,10,15};
    node* pnode = new node(a[0]);  //以第一个元素为根节点
    // 每次插入都从根节点开始判断
    for (int i = 1; i < n; ++i) {
        insert(a[i], pnode);
    }
    inorder(pnode);
}

//在递归插入时,要实现每插入一个节点就输出该节点的双亲节点,可在insert()函数中添加一个参数,用于记录输出双亲节点的值,
//在递归结束时(即找到插入位置进行插入时)输出。 另外,若要实现从空树开始插入,即不需单独先创建一个根节点,insert()函数
//的node*参数可设为引用,初始时传入一个为NULL的根节点指针。
node* insert(int x, node*& pnode,int parent) {
    if (pnode == NULL) {
        pnode = new node(x);
        cout << parent << endl;
        return pnode;
    }
    else {
        if (x <= pnode->val) {
            pnode->left = insert(x, pnode->left, pnode->val);
        }
        else {
            pnode->right = insert(x, pnode->right, pnode->val);
        }
    }
    return pnode;
}
int main() {
    int n; cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    //node* pnode = new node(a[0]);
    node* pnode = NULL;
    for (int i = 0; i < n; ++i) {
        insert(a[i], pnode, -1);
    }
    inorder(pnode);  //pnode为根节点
}

  1. 非递归
struct node{
    int val;
    node* left, *right;
    node() { ; }
    node(int x) :val(x), left(NULL), right(NULL) {}
};
int main()
{
    int n; cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    node* pnode = new node(a[0]);
    //cout << -1 << endl;
    for (int i = 1; i < n; ++i) {
        node* parent = pnode;
        node* cur = new node(a[i]);
        while (true) {
            if (a[i] < parent->val) {
                if (parent->left == NULL) {
                    parent->left = cur;
                    //cout << parent->val << endl;
                    break;
                }
                else {
                    parent = parent->left;
                }
            }
            else {
                if (parent->right == NULL) {
                    parent->right = cur;
                    //cout << parent->val << endl;
                    break;
                }
                else {
                    parent = parent->right;
                }
            }
        }
    }
}

二、查找

//递归  仅实现查找
node* search(int x,node* root) {
    if (root == NULL || root->val == x) {
        return root;
    }
    if (root->val > x) {
        return search(x, root->left);
    }
    else {
        return search(x, root->right);
    }
}
//非递归
node* search(int x,node* root) {
    while (root!=NULL) {
        if (root->val == x) {
            return root;
        }
        if (x < root->val && root->left!=NULL) {
            root = root->left;
        }
        else if (x > root->val && root->right != NULL) {
            root = root->right;
        }
    }
    return NULL;
}

三、在查找不成功时插入

//查找成功则返回查找节点,不成功则返回查找路径上的最后一个节点f,以便进行插入.f初始调用值为NULL
node* search(node* root,int x,node* f,bool& found) {
    if (root == NULL) {
        return f;
    }
    else if (x == root->val) {
        return root;
    }
    else if (root->val > x) {
        f = root;
        return search(root->left, x, f, found);
    }
    else {
        f = root;
        return search(root->right, x, root, found);
    }
}
void insert(node* f, node* root, int x) {
    node* s = new node(x);
    if (root == NULL) { //若树空,则插入节点为根节点
        root = s;
    }
    else if (x < f->val) {
        f->left = s;
    }
    else {
        f->right = s;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值