AVL Tree Help

closed account (GybDjE8b)
Whenever i compile it my height output is giving me 0 1 0 1....
this shouldn't be happening.... Can any1 explain why and check if my insert_internal & my rotation if it is correct

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
  class AVLSearch
{
    public:
    AVLSearch(){
        root = NULL;
    }
    void insert(int key, string value);
    string search(int key);
    int getHeight();
    bool delete_node(int key);
    int getbalance(Node *node);

    private:
    Node *root;
    int delete_internal(Node*& root, int key);
    Node* popMinNode(Node*& root);
    void insert_internal(Node* &root, Node* newptr);
    string search_internal(Node* root, int key);
    int getHeight_internal(Node* root);
    Node* rightRotate(Node* y);
    Node* leftRotate(Node* x);
};


bool AVLSearch::delete_node(int key)
{
    int del = delete_internal(root, key);
    if(del == 0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}


int AVLSearch::getHeight()
{
       return getHeight_internal(root);
}

int AVLSearch::getHeight_internal(Node *node)
{
    if(node == NULL)  return -1;
    else
    {
        int h_left = getHeight_internal(node->leftchild);
        int h_right = getHeight_internal(node->rightchild);
        cout << h_left << " " << h_right << endl;
        if(h_left >= h_right)
        {
            return h_left + 1;
        }
        else
        {
            return h_right + 1;
        }
    }
}


string AVLSearch::search(int key)
{
    return search_internal(root, key);
}

string AVLSearch::search_internal(Node *root, int key)
{
    if(root == NULL)
    {
       return "Not found";
    }
    else if(root->key == key)
    {
       return root->value;
    }
    else if(root->key > key)
    {
        return search_internal(root->leftchild, key);
    }
    else if(root->key < key)
    {
        return search_internal(root->rightchild, key);
    }
}

int AVLSearch::delete_internal(Node*& root, int key)
{
    if(root == NULL)
    {
        return 0;
    }
    else if(root->key > key)
    {
        return delete_internal(root->leftchild, key);
    }
    else if(root->key < key)
    {
        return delete_internal(root->rightchild, key);
    }
    else if(root->key == key)
    {
    if(root->leftchild == NULL && root->rightchild == NULL)
    {
            delete root;
                root = NULL;
    }
    else if(root->leftchild == NULL && root->rightchild != NULL)
    {
        Node * temp = root->rightchild;
        delete root;
        root = temp;
    }
    else if(root->rightchild == NULL && root->leftchild != NULL)
    {
        Node * temp = root->leftchild;
                delete root;
                root = temp;
    }
    else
    {
        Node* min = popMinNode(root->rightchild);
        cout << "Min key : " << min->key << endl;
        root->key = min->key;
                root->value = min->value;
    }
    return 1;
    }
}

Node* AVLSearch::popMinNode(Node*& root)
{
    if(root->leftchild == NULL)
    {
        Node *temp = root;
        root = root->rightchild;
        return temp;
    }
    else
    {
        return popMinNode(root->leftchild);
    }
}


void AVLSearch::insert(int key, string value)
{
    Node *newptr = new Node(key, value);
    insert_internal(root, newptr);
}


void AVLSearch::insert_internal(Node* &root, Node* newptr)
{
    if(root == NULL)
    {
           root = newptr;
    }
    else if(root->key > newptr->key)
    {
        insert_internal(root->leftchild, newptr);
    }
    else if(root->key < newptr->key)
    {
        insert_internal(root->rightchild, newptr);
    }
    
    int balance = getbalance(root);

    if (balance > 1 && newptr->key < root->leftchild->key)
        rightRotate(root);

    // Right Right Case
    else if (balance < -1 && newptr->key > root->rightchild->key)
        leftRotate(root);

    // Left Right Case
    else if (balance > 1 && newptr->key > root->leftchild->key)
    {
        root->leftchild =  leftRotate(root->leftchild);
        rightRotate(root);
    }
    // Right Left Case
    else if (balance < -1 && newptr->key < root->rightchild->key)
    {
        root->rightchild = rightRotate(root->rightchild);
        leftRotate(root);
    }

}
Node* AVLSearch::rightRotate(Node *y)
{
    Node *x = y->leftchild;
    Node *T2 = x->rightchild;
    x->rightchild= y;
    y->leftchild = T2;
    return x;
}
Node* AVLSearch::leftRotate(Node *x)
{
    Node *y = x->rightchild;
    Node *T2 = y->leftchild;
    y->leftchild = x;
    x->rightchild = T2;
    return y;
}

int AVLSearch::getbalance(Node *node)
{
    int h_left = getHeight_internal(node->leftchild);
    int h_right = getHeight_internal(node->rightchild);
    return (h_left - h_right);
}




int main()
{
    cout << "Hello World" << endl;
    AVLSearch bst;
    bst.insert(1001,"Hello");
    cout << "Height : " << bst.getHeight() << endl;
    bst.insert(1002,"World");
    cout << "Height : " << bst.getHeight() << endl;
    bst.insert(1003,"What");
    cout << "Height : " << bst.getHeight() << endl;
    bst.insert(1004,"Where");
    cout << "Height : " << bst.getHeight() << endl;
    bst.insert(1005,"Which");
    cout << "Height : " << bst.getHeight() << endl;
    cout << bst.search(1001) << endl;
    bst.delete_node(1001);
    cout << bst.search(1001) << endl;
    cout << "New Height : " << bst.getHeight() << endl;
    return 0;
}
Last edited on
Hi,

The easiest thing to do, is to use a debugger, this will save you days of staring at code.

Hopefully there will be a GUI one in your IDE somewhere.

Set some breakpoints before where you think there might be a problem.

Make a watch list of variables, of whose values you want to keep an eye on.

When the break point is hit, step through 1LOC at a time, look at the values, and deduce where things went wrong.
Just wanted to make a point:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int AVLSearch::getHeight_internal(Node *node)
{
    if(node == NULL)  return -1;
    else
    {
        int h_left = getHeight_internal(node->leftchild);
        int h_right = getHeight_internal(node->rightchild);
        cout << h_left << " " << h_right << endl;
        if(h_left >= h_right)
        {
            return h_left + 1;
        }
        else
        {
            return h_right + 1;
        }
    }
}


This is not how height information is stored in an AVL tree. Doing this will make the performance worse than that of a normal balanced tree. Height information is stored at each node i.e. each node knows what level it is located on the tree
closed account (GybDjE8b)
I've tried to set up breakpoints to check when i do the getheight it shows -1 for leftchild all the time i dont get why
and whenever i do h_left - h_right it obviously gonna be more of a negative number
Last edited on
Topic archived. No new replies allowed.