I have the following function (and I apologize for my horrible Ansi-C skills, or lack thereof):
// Stick a PacketNode into a HashTree
void InsertPacket(IPv4 database, int treeIndex, int hash, Packet packet)
{
// Check to see if the HashTree already has a BST for this hash, and
// create one if not.
if ((*database->hashTrees[treeIndex])->bst == NULL)
{
printf("hashTree[%d]->bst is NULL\n", treeIndex);
Tree newTree;
newTree = InitTree();
newTree->key = hash;
(*database->hashTrees[treeIndex])->bst = newTree; //THIS LINE...
}
if ((*database->hashTrees[treeIndex])->bst != NULL)
{
printf("hashTree[%d]->bst is NOT NULL\n", treeIndex);
}
// Insert the PacketNode into the BST
Tree node;
node = InitNode(hash, packet);
TreeInsert((*database->hashTrees[treeIndex])->bst, node); //OR THIS ONE...
InorderTreeWalk((*database->hashTrees[treeIndex])->bst);
}
The problem is that I want to do that last InorderTreeWalk() function in the function 3 levels up. (ie I call Store(database, packet), which calls an InsertData() function which calls the InsertPacket() function above, and I want to call the tree walk just after Store) In the InsertData function I initialize and set the database->hashTree[treeIndex] = &newHashTree, then call InsertPacket() to create a BST which is part of the HashTree struct.
I want to store a couple hundred of these packets, then run the InorderTreeWalk() function just after the looped Store(database, packet) call.
I'm not sure I'm providing enough info, and I know I'm butchering C pointers. I've been coding mainly in C# and Python for the last 3+ years... "all my base are belong to" someone else.
Any advice would be appreciated.
PS: database is a struct having an array of pointers, has开发者_开发技巧hTable[256], to struct HashTrees. Which in turn contain an int and a binary search tree, bst. The BSTs are keyed on ints and have a struct packet as data. Packets just contain several char arrays.
I don't think that extra indirection is buying you anything, since you have to add (*...) every time you use it. Cleaning that up should make the whole thing easier to read (and reason about).
And the traversal function should be fine higher in the chain, so long as it's after the insertion of the thing you want to find.
精彩评论