Dec 9, 2014 at 11:35pm UTC
I am writing an avl tree and when I insert numbers 0-50 and 0- (-50) it works perfectly. however, when i insert numbers -50 - 0 and 50-0 it crashes. has anybody dealt with this before? i'll post my code if someone replies! thanks!
Dec 9, 2014 at 11:38pm UTC
I'm driving a car and when I turn left then right then right then left, I get to my destination. However, when I turn right then left then left then right, it crashes. Has anyone dealt with this before?
We need to look at your car code.
Dec 10, 2014 at 1:38am UTC
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
Node* AVL::rotate_left(Node* current)
{
cout<<"rotating left" <<endl;
if (root->balance < 1)
{
// cout<<"returned"<<endl;
// return NULL;
}
Node* temp = current->right;
current->right = temp->left;
temp->left = current;
current = temp;
//if(current->right->right != NULL)
// cout<<current->left->data<<" "<<current->data<<" "<<current->right->data<<" "<< current->right->right->data<<endl;
return current;
}
Node* AVL::rotate_right(Node* current)
{
cout<<"rotating right" <<endl;
if (root->balance < 1)
{
// cout<<"returned"<<endl;
// return NULL;
}
Node* temp = current->left;
current->left = temp->right;
temp->right = current;
current = temp;
return current;
}
Node* AVL::rebalance_right(Node* current)
{
//cout<<"root balance: "<<root->balance<<endl;
cout<<"rebalance right" <<endl;
if (current->right->balance < 0) //
{
if (current->right->left->balance > 0)
{
current->right->balance = 0;
current->right->left->balance = 0;
current->balance = -1;
}
else if (current->right->left->balance == 0) //figure this one out!
{
current->right->balance = 0;
current->right->left->balance = 0;
current->balance = 0;
}
else
{
current->right->balance = 1;
current->right->left->balance = 0;
current->balance = 0;
}
current->right = rotate_right(current->right);
decrease = true ;
}
else
{
current->balance = 0;
current->right->balance = 0;
}
decrease = true ;
current = rotate_left(current);
}
Node* AVL::rebalance_left(Node* current)
{
cout<<"rebalance left" <<endl;
if (current->balance > 0)
{
if (current->left->right->balance < 0)
{
current->left->balance = 0;
current->left->left->balance = 0;
current->balance = 1;
}
else if (current->left->right > 0)
{
current->left->balance = -1;
current->left->left->balance = 0;
current->balance =0;
}
else
{
current->left->balance = 0;
current->left->left->balance = 0;
current->balance = 0;
}
current->left = rotate_left(current->left);
decrease = true ;
}
else
{
current->left->balance = 0;
current->balance = 0;
}
decrease = true ;
current = rotate_right(current);
return current;
}
bool AVL::add(int data)
{
if (containsR(data, root) == false )
{
root = addR(root, data);
cout<<"$$$$$$$$$$$$$$$$$$$$ added: " <<data<<endl;
return true ;
}
else
{
return false ;
}
}
Node* AVL::addR(Node* current, int data)
{
if (current == NULL)
{
increase = false ;
return new Node(data, 0, 0, NULL, NULL);
}
else if (data == current->data)
{
return NULL;
}
else if (data < current->data)
{
current->left = addR(current->left, data);
if (increase)
{
increase = false ;
current->balance = current->balance - 1;
}
current = balanceTree(current);
return current;
}
else if (data > current->data)
{
current->right = addR(current->right, data);
if (increase)
{
current->balance = current->balance + 1;
increase = false ;
}
current = balanceTree(current);
return current;
}
}
int AVL::height(Node* current)
{
if (current == NULL)
return 0;
else
return 1 + max(height(current->left), height(current->right));
}
int AVL::max(int a, int b)
{
return (a >= b)? a: b;
}
int AVL::get_balance(Node* current)
{
if (current == NULL)
{
return 0;
}
else
{
return height(current->left) - height(current->right);
}
}
Node* AVL::balanceTree(Node* current)
{
int heightDiff = get_balance(current);
cout<<"1" <<endl;
if (heightDiff > 1)
{
cout<<"2" <<endl;
if (get_balance(current->left) > 0)
{
cout<<"3" <<endl;
//current = rotate_LL(current);
current = rebalance_left(current);
}
else
{
cout<<"4" <<endl;
//current = rotate_LR(current);
current = rotate_right(current);
}
}
else if (heightDiff < -1)
{
cout<<"5" <<endl;
if (get_balance(current->right) < 0)
{
cout<<"6" <<endl;
//current = rotate_RR(current);
current = rebalance_right(current);
}
else
{
cout<<"7" <<endl;
//current = rotate_RL(current);
//current = rotate_right(current);
current = rotate_left(current);
}
}
return current;
}
Node is a class that has pointers to its left and right children. also has a data member of it, and a balance member.
Last edited on Dec 10, 2014 at 3:03pm UTC
Dec 10, 2014 at 7:52am UTC
Please edit your post and make sure that your code is [co de]between code tags[/code] so that there is syntax highlighting and line numbers.