简体   繁体   English

AVL树样本说明

[英]AVL tree sample explanation

I am reading some sample source code on AVL trees and part of the implementation is this following function: 我正在阅读AVL树上的一些示例源代码,部分实现是以下功能:

AvlTree MakeEmpty( AvlTree T )
{
    if( T != NULL )
    {
        MakeEmpty( T->Left );
        MakeEmpty( T->Right );
        free( T );
    }
    return NULL;
}

In the main function, it is used as follows: 在主要功能中,其用法如下:

int main()
{
    AvlTree T;

    T = MakeEmpty( NULL );

The main function then moves onto the inserting numbers into the AVL tree. 然后,主要功能移至AVL树中的插入编号。 My main questions are 我的主要问题是

a) What is the purpose of the MakeEmpty function? a)MakeEmpty函数的目的是什么? I understand that it is a recursive function, but I do not understand its purpose. 我知道它是递归函数,但我不了解它的目的。

b) Why is a NULL value passed into this function? b)为什么将NULL值传递给此函数?

Many thanks! 非常感谢!

Also, AVLTree is a pointer to this struct: 同样,AVLTree是指向此结构的指针:

struct AvlNode
{
    ElementType Element;
    AvlTree  Left;
    AvlTree  Right;
    int      Height;
};

This looks like a c-style ctor and dtor for AvlNode . 这看起来像是dtor的c样式ctorAvlNode

In C++, you could do something like 在C ++中,您可以执行以下操作

struct AvlNode
{
    ElementType Element;
    AvlTree  Left;
    AvlTree  Right;
    int      Height;

    AvlNode()
      : Left(NULL), Right(NULL), Height(0)
    {}

    ~AvlNode()
    { 
       // free node
    } 
};

But you could not do this in C. So basically, the MakeEmpty just to make sure that both pointers does not point to random address, but instead, point to NULL . 但是您不能在C中执行此操作。因此,基本上, MakeEmpty仅用于确保两个指针都不指向随机地址,而是指向NULL

a) What is the purpose of the MakeEmpty function? a)MakeEmpty函数的目的是什么? I understand that it is a recursive function, but I do not understand its purpose. 我知道它是递归函数,但我不了解它的目的。

makeEmpty() function free memory for all nodes while travels in postorder as its name suggest. makeEmpty()函数为所有节点释放空闲内存,正如其名称所暗示的那样,它makeEmpty()旅行。 Note in post order traverser once a node is processed it doesn't referenced again so to free() all nodes post order is correct one because freed nodes shouldn't be reference again. 请注意,在后顺序遍历器中,一旦处理了一个节点,就不会再对其进行引用,因此对free()的所有节点而言,后顺序是正确的,因为释放的节点不应再次被引用。

b) Why is a NULL value passed into this function? b)为什么将NULL值传递给此函数?

Its just a symmetric way to initialized T to NULL. 它只是 T 初始化为NULL的对称方式。 notice MakeEmpty( NULL ); 通知MakeEmpty( NULL ); returns null because null is send. 返回null,因为发送了null。

        T = MakeEmpty( NULL );
==      T = NULL

Edit 编辑

How MakeEmpty() works? MakeEmpty()如何工作? (read comments) (阅读评论)

AvlTree MakeEmpty( AvlTree T )
{
    if( T != NULL )
    {
        MakeEmpty( T->Left );    // but in stack 
        MakeEmpty( T->Right );   // but in stack 
        free( T );               // free node
    }
    return NULL;    // if T is passed NULL then it returns NULL
}

for (B) answer T = MakeEmpty( NULL ); 对于(B)答案T = MakeEmpty( NULL ); returns NULL. 返回NULL。

Here is my understanding of the code: 这是我对代码的理解:

"What is the purpose of the MakeEmpty function? I understand that it is a recursive function, but I do not understand its purpose." “ MakeEmpty函数的目的是什么?我知道它是递归函数,但我不了解它的目的。”

inside int main() basically you are creating a AvlTree object. 在int main()内部,基本上是在创建AvlTree对象。 The reason MakeEmpty is passed Null is because you want an empty tree node, with the right and left children as undefined. 将MakeEmpty传递为Null的原因是因为您需要一个空的树节点,左右子节点为未定义。

b. Why is it being passed null? 为什么将其传递为null? Null terminates the recursion and since the idea is to just create the empty tree with the child nodes not defined passing in Null will create the "emtpy object". Null终止递归,并且由于想法是只创建带有未定义子节点的空树,因此传入Null会创建“空对象”。

Essentially this whole piece of code is creating a null AvlTree as far as i can tell. 基本上,据我所知,整段代码都在创建一个空的AvlTree。 HTH. HTH。 Thanks. 谢谢。

a) It is emptying the tree - ie freeing all of the nodes a)它正在清空树-即释放所有节点

b) Because it is a recursive function it will pass null when it reaches a leaf although it could just as easily check for null before the next recursive call b)因为它是一个递归函数,所以它在到达叶子时将传递null,尽管它可以在下一个递归调用之前轻松地检查null

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

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