summaryrefslogtreecommitdiff
path: root/1_anno/Programmazione_2/exercises/exam_08_10_14.cc
blob: c9c33bebd0b83f6a546d1a6acbb60826e5dc6cf9 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include<iostream>

using namespace std;

template<class H> class MultiBST {
    virtual MultiBST<H>* ins(H x) = 0;
    virtual int multiplicity(H x) = 0;
    virtual void inorder() = 0;
};

template<class H>
class node {
public:
    node(H val) : _key{val}, _parent{nullptr}, _left{nullptr}, _right{nullptr}, _rk{1} {}
    H& key() { return _key; }  
    const H& key() const { return _key; }  
    node<H>*& parent() { return _parent; }
    const node<H>*& parent() const { return _parent; }
    node<H>*& left() { return _left; }
    const node<H>*& left() const { return _left; }
    node<H>*& right() { return _right; }
    const node<H>*& right() const { return _right; }
    int& rk() { return _rk; }
    const int& rk() const { return _rk; }
private:
    H _key;
    node<H>* _parent;
    node<H>* _left;
    node<H>* _right;
    int _rk;
};

template<class H>
class MyMultiBST : public MultiBST<H> {
public:
    MyMultiBST() : _root{nullptr} {}
    node<H>*& root() { return _root; }
    const node<H>*& root() const { return _root; }
    int multiplicity(H x) {
        auto elem = _search(x);
        if(elem)
            return elem->rk();
        return 0;
    }
    MyMultiBST<H>* ins(H x) {
        auto iter = _search(x);
        if(iter) {
            iter->rk() = iter->rk()+1;
        } else {
            iter = _root;
            node<H>* y{nullptr};

            while(iter) {
                y = iter;
                if(iter->key() > x) 
                    iter = iter->left();
                else
                    iter = iter->right();
            }

            node<H>* nodus = new node<H>{x};
            nodus->parent() = y;
            if(!y) {
                _root = nodus;
            } else if(y->key() > x) {
                y->left() = nodus;
            } else {
                y->right() = nodus;
            }
        }

        return this;
    }
    void inorder() {
       _inorder(_root);
    }
    MyMultiBST<H>* del(H x) {
        node<H>* y;
        node<H>* z = _search(x);

        if(!z) return this;

        if(z->rk() > 1) {
            z->rk() = z->rk()-1;
        } else {
            if(!z->left()) {
                _transplant(z, z->right());
            } else if(!z->right()) {
                _transplant(z, z->left());
            } else {
                y = _min(z->right());
                if(y->parent() != z) {
                    _transplant(y, y->right());
                    y->right() = z->right();
                    y->right()->parent() = y;
                }
                _transplant(z, y);
                y->left() = z->left();
                y->left()->parent() = y;
                delete z;
            }
        }

        return this;
    }
    node<H>* predecessor(H x) {
        auto iter = _search(x);
        if(!iter) return nullptr;
        if(iter->left())
            return _max(iter->left());

        auto y = iter->parent();
        while(y && iter == y->left()) {
            iter = y;
            y = y->parent();
        }

        return y;
    }
    int rank(H x) {
        auto iter = predecessor(x);
        int sum{1};
        while(iter) {
            sum+=iter->rk();
            iter = predecessor(iter->key());
        }
        return sum;
    }
private:
    void _transplant(node<H>* u, node<H>* v) {
        if(!u->parent()) _root = v;
        else if(u == u->parent()->left())
            u->parent()->left() = v;
        else
            u->parent()->right() = v;

        if(v)
            v->parent() = u->parent();
    }
    node<H>* _max(node<H>* x) {
        if(!x) return nullptr;

        auto iter = x;
        while(iter->right()) {
            iter = iter->right();
        }

        return iter;
    }
    node<H>* _min(node<H>* x) {
        if(!x) return nullptr;

        auto iter = x;
        while(iter->left()) {
            iter = iter->left();
        }

        return iter;
    }
    void _inorder(node<H>* p) {
        if(p) {
            _inorder(p->left());
            for(int i = 0; i < p->rk(); ++i) {
                cout << p->key() << ' ';
            }
            _inorder(p->right());
        }
    }
    node<H>* _search(H val) {
        auto iter = _root;
        while(iter && iter->key() != val) {
            if(iter->key() > val)
                iter = iter->left();
            else
                iter = iter->right();
        }

        return iter;
    }
    node<H>* _root;
};

int main() {
    MyMultiBST<int>* t = new MyMultiBST<int>{};

    for(auto const& i : {10, 7, 7, 23, 30, 4, 1, 5, 9, 5, 1, 7, 1, 9})
        t->ins(i);

    t->inorder();
    cout << '\n';
    for(auto const& i : {5, 7, 17})
        cout << i << ": " << t->multiplicity(i) << endl;

    for(auto const& i : {7, 4, 23, 7, 7})
        t->del(i);
    t->inorder();
    cout << '\n';
    for(auto const& i: {5, 9, 30})
        cout << t->rank(i) << ' ';
    delete t;
    return 0;
}