Hi everyone. I have question to B tree structure node in C language.
I have three elements in this structure:
struct treeNode is array of children,does is?
but what is the purpose of val[MAX + 1] and count?
1 2 3 4 5 6 7 8 9
#define MAX 4
#define MIN 2
struct treeNode {
int val[MAX + 1], count;
struct treeNode *child[MAX + 1];
};
struct treeNode *root;
the line 6 statement creates a variable in your outer struct that is named child and is an array of treenode pointers. Its size is max+1, which is often done when a user wants to index 1-max instead of 0 to max-1. C and C++ index arrays from 0 and some people used to other languages want to start from 1, is the usual suspect here.
count is an integer. Its purpose is unclear at this time, presumably it counts something, but what? # of children seems plausible.
val is unlcear as well. it is probably the 'data' held in the tree, but this here is a great example of why using good variable names with good comments is good practice.
if its in a book, reading a few more pages may answer your questions with ongoing code snips and explains. If its just a random data structure you got somewhere, you should build your own for what your purpose is, rather than use one that is not well commented.
do you understand binary trees or linked lists, and how a node has a pointer to its own type to make the 'chain' that these data structures are built from (using the classical approaches)? If not, I highly recommend you get a handle on how linked list works first, this tree is going to be complex.