简体   繁体   中英

Representation of internal and external nodes in binary tree in C

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:

  1. 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.
  2. 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!

What does author mean a son pointer within an internal node?

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:

struct Node {
    int someData;
    struct Node *left, *right;
};

So the son pointers the author is referring to are these left and right fields having pointer types. They are more often called child pointers though.

can any one define example structure for two techniques in C to understand the statements?

Here are they.

(1)

struct InternalNode;
struct ExternalNode;

union ChildPointer {
    struct InternalNode *asInternal;
    struct ExternalNode *asExternal;
};

struct InternalNode {
    // Some data here

    union ChildPointer left, right;
};

struct ExternalNode {
    // External node data here
};

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)

union Node;
struct InternalData {
    // Internal node data here

    union Node *left, *right;
};

struct ExternalData {
    // External node data here
};

union Node {
    struct InternalData internal;
    struct ExternalData external;
};

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM