Hello, i'm working on a project dealing with creating a Binary Tree and I've come to a point where i'm stuck on inserting new nodes into the tree. The following is the definitions for the structs and functions that must be used:
Now, when I look over the function insertTree I see that i'll be passing it a pointer to a Tree (which contains a pointer to a TreeNode) and also a pointer to some information (TreeInfoT is a pointer to a struct containing some information).Code:/* * Defines one node in the Binary Tree */ typedef struct TreeNode { struct TreeNode *L; TreeInfoT info; struct TreeNode *R; } *TreeNodeT; /* * Tree type is just a pointer to the root of the tree */ typedef struct Tree { TreeNodeT root; } *TreeT; /* * Creates tree node with the provided info node * Inserts the tree node into the correct position in the tree */ void insertTree(TreeT, TreeInfoT);
my current code looks like this:
edit: I've inserted my new code, now, but i'm still stuck when trying to figure this out recursively. the compareToTreeInfo function takes two things of type TreeInfoT (which are pointers to some information, we arn't allowed to know what type of information it contains until we show off the project which is why we have to do this with the pointers) and compares them, returning values as if it were strcmp. My issue is, the first time we check this it has to be done via a->root because we're being passed a pointer to the tree and not a pointer to the node. That means I have no clue how to do this recursively since any further calls won't have a root to go through....it would just be a->info.Code:void insertTree(TreeT a, TreeInfoT b) { if (a->root==NULL) //if the root of the tree is pointing to nothing { //create the node, store the data and set the root equal tot he node TreeNodeT temp=(TreeNodeT)malloc(sizeof(struct TreeNode)); if (temp==NULL) //if malloc fails, display error and exit { printf("Memory Allocation Failed! \n"); exit(1); } temp->info=b; //store the pointer to the info in the nodes info field temp->L=NULL; //set the L and R pointers to Null temp->R=NULL; a->=temp; //Point the root to the node return //exit the function } else if (compareToTreeInfo(a->root->info,b)<= 0) insertTree(
Any help that can set me in the right direction would be SO helpful,
Anthony Vitello



LinkBack URL
About LinkBacks


