I am getting a Segmentation Fault when im trying to pass the tree and its root so that the tree can be traversed. The code for an In-Order traversal was given to me and it works. Here is what how i print the tree with an In-Order Traversal:
I call from my main program. In a separate file i have the functions:
Code:
/***********************************************************************/
/* FUNCTION: RBTreePrint */
/**/
/* INPUTS: tree is the tree to print */
/**/
/* OUTPUT: none */
/**/
/* EFFECT: This function recursively prints the nodes of the tree */
/* inorder using the PrintKey and PrintInfo functions. */
/**/
/* Modifies Input: none */
/**/
/***********************************************************************/
void RBTreePrint(rb_red_blk_tree* tree) {
InorderTreePrint(tree,tree->root->left);
}
/***********************************************************************/
/* FUNCTION: InorderTreePrint */
/**/
/* INPUTS: tree is the tree to print and x is the current inorder node */
/**/
/* OUTPUT: none */
/**/
/* EFFECTS: This function recursively prints the nodes of the tree */
/* inorder using the PrintKey and PrintInfo functions. */
/**/
/* Modifies Input: none */
/**/
/* Note: This function should only be called from RBTreePrint */
/***********************************************************************/
void InorderTreePrint(rb_red_blk_tree* tree, rb_red_blk_node* x) {
rb_red_blk_node* nil=tree->nil;
rb_red_blk_node* root=tree->root;
if (x != tree->nil) {
InorderTreePrint(tree,x->left);
printf("info=");
tree->PrintInfo(x->info);
printf(" key=");
tree->PrintKey(x->key);
printf(" l->key=");
if( x->left == nil) printf("NULL"); else tree->PrintKey(x->left->key);
printf(" r->key=");
if( x->right == nil) printf("NULL"); else tree->PrintKey(x->right->key);
printf(" p->key=");
if( x->parent == root) printf("NULL"); else tree->PrintKey(x->parent->key);
printf(" red=%i\n",x->red);
InorderTreePrint(tree,x->right);
}
}
With that working, I tried to write a Pre-Order traversal function. My function call is:
Code:
preOrder(tree, root);
And this is where i get the Segmentation Fualt.
Here is the preOrder function
Code:
void preOrder(rb_red_blk_tree* tree, rb_red_blk_node* x)
{
if(x != tree->nil)
{
printNode(tree, x);
preOrder(tree, x->left);
preOrder(tree, x->right);
}
}
Here are the structs for the tree and nodes of the tree.
Code:
typedef struct rb_red_blk_node {
void* key;
void* info;
int red; /* if red=0 then the node is black */
struct rb_red_blk_node* left;
struct rb_red_blk_node* right;
struct rb_red_blk_node* parent;
} rb_red_blk_node;
/* Compare(a,b) should return 1 if *a > *b, -1 if *a < *b, and 0 otherwise */
/* Destroy(a) takes a pointer to whatever key might be and frees it accordingly */
typedef struct rb_red_blk_tree {
int (*Compare)(const void* a, const void* b);
void (*DestroyKey)(void* a);
void (*DestroyInfo)(void* a);
void (*PrintKey)(const void* a);
void (*PrintInfo)(void* a);
/* A sentinel is used for root and for nil. These sentinels are */
/* created when RBTreeCreate is caled. root->left should always */
/* point to the node which is the root of the tree. nil points to a */
/* node which should always be black but has aribtrary children and */
/* parent and no key or info. The point of using these sentinels is so */
/* that the root and nil nodes do not require special cases in the code */
rb_red_blk_node* root;
rb_red_blk_node* nil;
} rb_red_blk_tree;
Any help would be appreciated.