ok, so I was passing in the correct values after all. Let me reillustrate my problem here.
here's the code with the problem line (I think) in red... the function is called with root->data and x->data both == 1.
Code:
208 // This function performs a left rotation on nodes
209
210 void leftrotate(struct node** root, struct node** x)
211 {
212 struct node* y;
213
214 y = (*x)->right;
215 (*x)->right = y->left;
216 if(y->left)
217 {
218 y->left->parent = (*x);
219 }
220 (y->parent) = ((*x)->parent);
221 if(!(*x)->parent)
222 {
223 (*root) = y;
224 }
225 else
226 {
227 if(*x == (*x)->parent->left)
228 {
229 (*x)->parent->left = y;
230 }
231 else
232 {
233 (*x)->parent->right = y;
234 }
235 }
236 y->left = *x;
237 (*x)->parent = y;
238
239 return;
240 }
here's what my tree looks like when the function is called, and what it should look like afterwards...
Code:
when called: what I need after:
1 2
\ / \
2 1 3
\
3
my struct is:
Code:
struct node
{
int data;
char color;
struct node* parent;
struct node* left;
struct node* right;
};
And finally, my gdb output with the suspect line in red again and other lines of importance in teal.
Code:
Breakpoint 1, leftrotate (root=0xbffff6cc, x=0x804a3c8) at main.c:214
214 y = (*x)->right;
(gdb) p x->data
$8 = 1
(gdb) step
215 (*x)->right = y->left;
(gdb) p x->data
$9 = 1
(gdb) p y->data
$10 = 2
(gdb) step
216 if(y->left)
(gdb)
220 (y->parent) = ((*x)->parent);
(gdb) p x->data
$11 = 1
(gdb) p y->data
$12 = 2
(gdb) step
221 if(!(*x)->parent)
(gdb) p x->data
Cannot access memory at address 0x0
(gdb) p y->data
$13 = 2
??? I know from my previous example that x is getting moved one step up the tree, which is to NULL in this case, but why? Obviously, when I run the next line
I get a seg fault since x doesn't point to anything.
Sorry about reposting all of this, I just thought it would be better to state things more clearly and all in one spot. Thanks again
dinjas