简体   繁体   English

C中二叉树中内部和外部节点的表示

[英]Representation of internal and external nodes in binary tree in C

I am reading data structures in C and C++ by Tannenbaum. 我正在阅读Tannenbaum的C和C ++数据结构。 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. 一种技术是声明两种不同的节点类型和指针类型,并为内部节点使用union,每个替换包含两种指针类型之一。
  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? (A)作者在内部节点中的意思是什么?

(B) can any one define example structure for two techniques in C to understand the statements? (B)可以用C语言中的任何一个定义示例结构来理解语句吗?

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: 构建这种结构的方法之一是定义C结构(sic!),如下所示:

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. 所以儿子指针笔者指的是这些leftright为指针类型的字段。 They are more often called child pointers though. 它们通常被称为指针。

can any one define example structure for two techniques in C to understand the statements? 任何人都可以在C中定义两种技术的示例结构来理解这些语句吗?

Here are they. 他们是这样的。

(1) (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. 但是,我不打算解释如何事先知道节点的左子节点是内部节点还是外部节点,可能是内部状态应该知道的,或者可能只有一个ExternalNode结构的实例,所有的非叶节点链接和直接比较是可能的,也许有人可以建议。

(2) (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. 使用匿名结构/联合可以使这更方便,但这是C的ms扩展。

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. 只有当您尝试在不包含数据的叶节点上保存一些内存或者如果它们确实需要一些内部节点不需要的额外内存时,这在理论上才有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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