I am reading data structures in C and C++ by Tannenbaum. Following is text snippet from the book
Non-leaf nodes are called internal nodes and leaves are called external
nodes. This terminology is often used when only single type of node is
defined. Of course, a son pointer within an internal node must be
identified as pointing to an internal and external node. This can be
done in two ways:
- one technique is to declare two different node types and pointer
types and to use a union for internal nodes with each alternative
containing one of the two pointer types.- Other technique is to retain a single type of pointer and a single
type of node, where the node is a union that does (if the node is an
internal node) or does not (if an external node) containing left and
right pointer fields.
My questions are
(A) What does author mean a son pointer within an internal node?
(B) can any one define example structure for two techniques in C to understand the statements?
Thanks!
I suppose the text is about a data structure called a tree. One of the approaches to building such a structure is defining a C structure (sic!) as follows:
So the son pointers the author is referring to are these
leftandrightfields having pointer types. They are more often called child pointers though.Here are they.
(1)
However, I’m not going to explain how to know in advance whether node’s left child is an internal or external node, probably it should be known by the internal state, or maybe there’s only one instance of the ExternalNode structure, to which all the non-leaf nodes link and the direct comparison is possible, maybe someone could advise.
(2)
This can be made a lot more convenient using anonymous structs/unions, but this is an ms-extension to C.
This can be theoretically meaningful only if you’re trying to save some bits of memory on leaf nodes that doesn’t contain data or some more memory if they do need some extra memory which is not needed by internal nodes.