summaryrefslogtreecommitdiff
path: root/src/tools/util/tree.h
blob: 527f718b00fa062d1ae2d48d68010a848e16de0e (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. 
//

#ifndef __GENERIC_TREE_H__
#define __GENERIC_TREE_H__

#include <windows.h>

// Partially balanced binary tree
// it does individual rotations on insertion, but does nto allow deletion.
// thus the worst case depth is not (n), but (n/2)
// Generic paramter is the element type
// Find and Add require a method that compares 2 elements
template <typename _E>
struct tree
{
    _E name;
    tree<_E> *lChild;
    tree<_E> *rChild;
    size_t lDepth;
    size_t rDepth;

    tree(_E e)
    {
        name = e;
        lChild = rChild = NULL;
        lDepth = rDepth = 0;
    }
    ~tree()
    {
        Cleanup();
    }

    bool InOrderWalk( bool (WalkFunc)(_E))
    {
        if (lChild != NULL && !lChild->InOrderWalk(WalkFunc))
            return false;
        if (!WalkFunc(name))
            return false;
        if (rChild != NULL)
            return rChild->InOrderWalk(WalkFunc);
        return true;
    }

    /*
     * return the depths of the tree from here down (minimum of 1)
     */
    size_t MaxDepth()
    {
        return lDepth > rDepth ? lDepth + 1 : rDepth + 1;
    }

    /*
     * Search the binary tree for the given string
     * return a pointer to it was added or NULL if it
     * doesn't exist
     */
    _E * Find(_E SearchVal, int (__cdecl CompFunc)(_E, _E))
    {
        int cmp = CompFunc(name, SearchVal);
        if (cmp < 0)
        {
            if (lChild == NULL)
                return NULL;
            else
                return lChild->Find(SearchVal, CompFunc);
        }
        else if (cmp > 0)
        {
            if (rChild == NULL)
                return NULL;
            else
                return rChild->Find(SearchVal, CompFunc);
        }
        else
            return &name;
    }

    /*
     * Search the binary tree and add the given string
     * return S_OK if it was added or S_FALSE if it already
     * exists (or E_OUTOFMEMORY)
     */
    HRESULT Add(_E add
                    , int (__cdecl CompFunc)(_E, _E))
    {
        int cmp = CompFunc(name, add
                              );
REDO:
        if (cmp == 0)
            return S_FALSE;

        if (cmp < 0)
        {
            if (lChild == NULL)
            {
                lDepth = 1;
                lChild = new tree<_E>(add
                                     );
                if (lChild == NULL)
                    return E_OUTOFMEMORY;
                return S_OK;
            }
            else if (rDepth < lDepth)
            {
                tree<_E> *temp = new tree<_E>(name);
                if (temp == NULL)
                    return E_OUTOFMEMORY;
                temp->rChild = rChild;
                temp->rDepth = rDepth;
                if (lChild != NULL &&
                    (cmp = CompFunc(lChild->name, add
                                       )) > 0)
                    {
                        // push right
                        temp->lChild = NULL;
                        temp->lDepth = 0;
                        name = add
                                   ;
                        rChild = temp;
                        rDepth++;
                        return S_OK;
                    }
                else if (cmp == 0)
                {
                    temp->rChild = NULL;
                    delete temp;
                    return S_FALSE;
                }
                else
                {
                    // Rotate right
                    temp->lChild = lChild->rChild;
                    temp->lDepth = lChild->rDepth;
                    name = lChild->name;
                    lDepth = lChild->lDepth;
                    rDepth = temp->MaxDepth();
                    rChild = temp;
                    temp = lChild->lChild;
                    lChild->lChild = lChild->rChild = NULL;
                    delete lChild;
                    lChild = temp;
                    goto REDO;
                }
            }
            else
            {
                HRESULT hr = lChild->Add(add
                                         , CompFunc);
                lDepth = lChild->MaxDepth();
                return hr;
            }
        }
        else
        {
            if (rChild == NULL)
            {
                rDepth = 1;
                rChild = new tree<_E>(add
                                     );
                if (rChild == NULL)
                    return E_OUTOFMEMORY;
                return S_OK;
            }
            else if (lDepth < rDepth)
            {
                tree<_E> *temp = new tree<_E>(name);
                if (temp == NULL)
                    return E_OUTOFMEMORY;
                temp->lChild = lChild;
                temp->lDepth = lDepth;
                if (rChild != NULL &&
                    (cmp = CompFunc(rChild->name, add
                                       )) < 0)
                    {
                        // push left
                        temp->rChild = NULL;
                        temp->rDepth = 0;
                        name = add
                                   ;
                        lChild = temp;
                        lDepth++;
                        return S_OK;
                    }
                else if (cmp == 0)
                {
                    temp->lChild = NULL;
                    delete temp;
                    return S_FALSE;
                }
                else
                {
                    // Rotate left
                    temp->rChild = rChild->lChild;
                    temp->rDepth = rChild->lDepth;
                    name = rChild->name;
                    rDepth = rChild->rDepth;
                    lDepth = temp->MaxDepth();
                    lChild = temp;
                    temp = rChild->rChild;
                    rChild->rChild = rChild->lChild = NULL;
                    delete rChild;
                    rChild = temp;
                    goto REDO;
                }
            }
            else
            {
                HRESULT hr = rChild->Add(add
                                         , CompFunc);
                rDepth = rChild->MaxDepth();
                return hr;
            }
        }
    }

    /*
     * Free the memory allocated by the tree (recursive)
     */
    void Cleanup()
    {
        if (this == NULL)
            return ;
        if (lChild != NULL)
        {
            lChild->Cleanup();
            delete lChild;
            lChild = NULL;
        }
        if (rChild != NULL)
        {
            rChild->Cleanup();
            delete rChild;
            rChild = NULL;

        }
    }

};

#endif // __GENERIC_TREE_H__