ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Linked-structure binary search tree(BST)
    자료구조 2023. 7. 5. 17:33
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    #include <iostream>
    #include <string>
    using namespace std;
     
    class Node
    {
    private:    
        Node *parent, *left, *right;
        int elem;
     
    public:
        Node() = delete;
        Node(int elem): elem{elem}, parent{nullptr}, left{nullptr}, right{nullptr} {}
        ~Node() = default;
        friend class BST;
    };
     
    class BST
    {
    private:
        Node* root;
        int n; // size
        void release(Node* node);
        bool is_external(Node* node) const { return node->left == nullptr && node->right == nullptr; }
        void pre_order(Node* node) const;
        int erase(Node* erase);
        void link(Node* u, Node* v); // u의 parent랑 v랑 연결(u는 항상 노드이고, v는 nullptr일 수 있음)
        Node* get_minimum(Node* node);
     
    public:
        BST() : root{nullptr}, n{0} {}
        ~BST() { if(root != nullptr) release(root); } 
        bool empty() const { return n == 0; }
        int size() const { return n; }
        void insert(int x);
        Node* find(int x) const;
        void pre_order() const;
        int erase(int x);
    };
     
    int main()
    {
        int T; cin >> T;
        while(T--){
            BST bst{};
            int N; cin >> N;
            for(int i = 0 ; i < N; i++){
                int tmp; cin >> tmp;
                bst.insert(tmp);
            }
     
            int M; cin >> M;
            for(int i = 0; i < M; i++){
                int tmp; cin >> tmp;
                bst.erase(tmp);
            }
     
            bst.pre_order();
        }
     
     
        return 0;
    }
     
    Node* BST::get_minimum(Node* node)
    {
        while(node->left != nullptr){
            node = node->left;
        }
        return node;
    }
     
    void BST::link(Node* u, Node* v)
    {
        // parent
        if(u->parent == nullptr){
            root = v;
        }
        else if(u->parent->left == u) u->parent->left = v;
        else u->parent->right = v;
        
        // child
        if(v != nullptr) v->parent = u->parent;
    }
     
    Node* BST::find(int x) const
    {
        if(empty()) throw "error from BST::find";
     
        Node* cur = root;
        while(cur != nullptr){
            if(cur->elem == x) return cur;
            else if(cur->elem < x) cur = cur->right;
            else cur = cur->left;
        }
     
        return cur; // doesn't exist
    }
     
    void BST::release(Node* node)
    {
        if(node->left != nullptr) release(node->left);
        if(node->right != nullptr) release(node->right);
        delete node; node = nullptr;    
    }
     
    void BST::insert(int x)
    {
        Node* node = new Node{x};
     
        if(empty()){
            root = node;
        }
        else{
            Node *cur = root, *prev = nullptr;
            while(cur != nullptr){
                prev = cur;
                cur = ((cur->elem <= x)? cur->right: cur->left);
            }
     
            // parent
            if(prev->elem <= x) prev->right = node;
            else prev->left = node;
     
            // child
            node->parent = prev;
        }
     
        n += 1;
    }
     
    void BST::pre_order(Node* node) const
    {
        cout << node->elem << ' ';
        if(node->left != nullptr) pre_order(node->left);
        if(node->right != nullptr) pre_order(node->right);
    }
     
    void BST::pre_order() const
    {
        if(empty()) cout << 0 << endl;
        else {
            pre_order(root);
            cout << endl;
        }
    }
     
    int BST::erase(Node* z)
    {
        int ret = z->elem;
        if(z->left == nullptr) link(z, z->right);
        else if(z->right == nullptr) link(z, z->left);
        else{
            Node* y = get_minimum(z->right);
            if(z != y->parent){
                link(y, y->right);
                y->right = z->right;
                z->right->parent = y;            
            }
            link(z, y);
            y->left = z->left;
            z->left->parent = y;
        }
        
        delete z; z = nullptr;
        n -= 1;
        return ret;
    }
     
    int BST::erase(int x)
    {
        if(empty()) throw "error from BST:erase";
        Node* tar = find(x);
        if(tar == nullptr) throw "error from BST::erase";
        return erase(tar);
     

    '자료구조' 카테고리의 다른 글

    대학교에서 배운 자료구조  (0) 2023.07.14
    Priority Queue using array based Heap  (0) 2023.07.14
    AVL tree  (0) 2023.07.14
    Priority Queue using Linked Heap  (0) 2023.07.04

    댓글

Designed by Tistory.