码迷,mamicode.com
首页 > 其他好文 > 详细

二叉搜索树(BST)

时间:2018-02-02 18:36:33      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:stc   creat   false   parent   bst   amp   oid   name   include   

(第一段日常扯蛋,大家不要看)这几天就要回家了,osgearth暂时也不想弄了,毕竟不是几天就能弄出来的,所以打算过完年回来再弄。这几天闲着也是闲着,就掏出了之前买的算法导论看了看,把二叉搜索树实现了下。

#include <iostream>
#include <memory>
#include <vector>

using namespace std;

//节点结构
struct Node {
    int key;
    int num;
    shared_ptr<Node> parent;
    shared_ptr<Node> left;
    shared_ptr<Node> right;
    Node(int _key) : key(_key), num(1),parent(NULL), left(NULL), right(NULL) {}
};

//循环插入
bool Insert(shared_ptr<Node>& root, int _key) {
    shared_ptr<Node> node(new Node(_key));
    if (root == NULL) {
        root = node;
        return true;
    }
    shared_ptr<Node> x = root;
    shared_ptr<Node> y;
    while(x != NULL) {
        y = x;
        if (node->key == x->key) {
            x->num++;
            return true;
        }
        if (node->key < x->key) x = x->left;
        else x = x->right;
    }
    if (node->key < y->key) {
        y->left = node;
        node->parent = y;
    }
    else {
        y->right = node;
        node->parent = y;
    }
    return true;
}

//迭代插入
bool reInsert(shared_ptr<Node>& root, shared_ptr<Node> node) {
    if (root == NULL) {
        root = node;
        return true;
    }
    if (node->key == root->key) {
        root->num++;
        return true;
    }
    if (node->key < root->key) return reInsert(root->left, node);
    else return reInsert(root->right, node);
}

//创建二叉树
void BSTCreat(shared_ptr<Node>& root, vector<int> keys) {
    for (int key : keys) {
        Insert(root, key);
    }
}

//遍历
void showBST(shared_ptr<Node>& root) {
    if (root != NULL) {
     //cout << root->key << " ";//先序遍历
        showBST(root->left);
        cout << root->key << " "; //中序遍历
        showBST(root->right);
     //cout << root->key << " ";//后序遍历
    }
}

//删除节点
bool Delete(shared_ptr<Node>& root, int _key) {
    if(root == NULL) return false;
    if(root->key < _key) return Delete(root->right, _key);
    else if(root->key > _key)return Delete(root->left, _key);
    else {
        if (root->right==NULL) {
            root = root->left;
            return true;
        }
        if (root->left==NULL) {
            root = root->right;
            return true;
        }
        if (root->right->left==NULL) {
            root->right->left = root->left;
            root = root->right;
            return true;
        }
        shared_ptr<Node> y = root->right;
        while(y->left!=NULL) {
            y = y->left;
        }
        y->parent->left = y->right;
        y->right = root->right;
        y->left = root->left;
        root = y;
        return true;
    }
}

int main() {
    vector<int> keys{10,6,14,4,8,12,16,3,5,7,9,11,13,15,17};

    shared_ptr<Node> root;
    BSTCreat(root, keys);
    showBST(root);
    cout << endl;

    Delete(root, 14);
    showBST(root);

    return 0;

}

 

二叉搜索树(BST)

标签:stc   creat   false   parent   bst   amp   oid   name   include   

原文地址:https://www.cnblogs.com/narjaja/p/8405972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!