Created
April 28, 2014 07:19
-
-
Save gabhi/11364028 to your computer and use it in GitHub Desktop.
left sibling right child tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sets the nextRight of root and calls connectRecur() for other nodes | |
void connect (struct node *p) | |
{ | |
// Set the nextRight for root | |
p->nextRight = NULL; | |
// Set the next right for rest of the nodes (other than root) | |
connectRecur(p); | |
} | |
/* Set next right of all descendents of p. | |
Assumption: p is a compete binary tree */ | |
void connectRecur(struct node* p) | |
{ | |
// Base case | |
if (!p) | |
return; | |
// Set the nextRight pointer for p's left child | |
if (p->left) | |
p->left->nextRight = p->right; | |
// Set the nextRight pointer for p's right child | |
// p->nextRight will be NULL if p is the right most child at its level | |
if (p->right) | |
p->right->nextRight = (p->nextRight)? p->nextRight->left: NULL; | |
// Set nextRight for other nodes in pre order fashion | |
connectRecur(p->left); | |
connectRecur(p->right); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment