Need help with C algorithm

Bluga

Banned
Nov 28, 2000
4,315
0
0
I'm trying to build a simple m-way search tree (no need to balance, m is given), does the following function look correct? How can i move to next child node if the curent is full?

===================================================
typedef struct TreeNode {

int num_of_keys;
int *keys;
struct TreeNode **children;

} TREENODE;

/* Insert(...) inserts key into the N-way search tree pointed to by root. It returns NULL if key already exists*/

struct TreeNode* Insert(struct TreeNode *root, int key)
{


if ( root == NULL )
{
root = (TreeNode **)malloc(sizeof(TreeNode *) * num_of_keys);
root->keys[0] = key;
root->num_of_keys = root->num_of_keys - 1;
}
else
{
for ( s=0; (s<root->num_of_keys); s++ );
{
if ( key == root->keys )
return NULL;

else
{
if (key<root->keys)
keys[s+1] = keys; /***shift****/
else{
key[s+1] = key;
s = s+1;
}

}

}

}

}

 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
root->num_of_keys = root->num_of_keys - 1;


what are you trying to do here?

root->num_of_keys - 1; // (some malloced)->num_of_keys is some garbage value
 

Bluga

Banned
Nov 28, 2000
4,315
0
0


<< root->num_of_keys = root->num_of_keys - 1; >>



i'm trying to keep track of the number of keys