Reputation: 313
I am reading some sample source code on AVL trees and part of the implementation is this following function:
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. My main questions are
a) What is the purpose of the MakeEmpty function? 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?
Many thanks!
Also, AVLTree is a pointer to this struct:
struct AvlNode
{
ElementType Element;
AvlTree Left;
AvlTree Right;
int Height;
};
Upvotes: 2
Views: 617
Reputation: 58271
a) What is the purpose of the MakeEmpty function? 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. 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.
b) Why is a NULL value passed into this function?
Its just a symmetric way to initialized T to NULL. notice MakeEmpty( NULL );
returns null because null is send.
T = MakeEmpty( NULL );
== T = NULL
Edit
How MakeEmpty() works? (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 );
returns NULL.
Upvotes: 1
Reputation: 189
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."
inside int main() basically you are creating a AvlTree object. The reason MakeEmpty is passed Null is because you want an empty tree node, with the right and left children as undefined.
b. Why is it being passed 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".
Essentially this whole piece of code is creating a null AvlTree as far as i can tell. HTH. Thanks.
Upvotes: 1
Reputation: 6682
This looks like a c-style ctor
and dtor
for AvlNode
.
In C++, you could do something like
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
.
Upvotes: 1
Reputation: 15464
a) It is emptying the tree - i.e. freeing all of the nodes
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
Upvotes: 1