The following is deleting a Tree in C (provided by GeeksforGeeks)
#include<stdio.h>
#include<stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
/* This function traverses tree in post order to
to delete each and every node of the tree */
void deleteTree(struct node* node)
{
if (node == NULL) return;
/* first delete both subtrees */
deleteTree(node->left);
deleteTree(node->right);
/* then delete the node */
printf("\n Deleting node: %d", node->data);
free(node);
}
/* Driver program to test deleteTree function*/
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
deleteTree(root);
root = NULL;
printf("\n Tree deleted ");
getchar();
return 0;
}
The above deleteTree() function deletes the tree, but doesn’t change root to NULL which may cause problems if the user of deleteTree() doesn’t change root to NULL and tires to access values using root pointer. We can modify the deleteTree() function to take reference to the root node so that this problem doesn’t occur. See the following code.
#include<stdio.h>
#include<stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
Deleting a node in C can be done by free(node)
. In Python, there's no such method. Do I need a reference to the node's parent to delete the node in Python whereas I don't need one in C?
(i.e., is there a way to do delete(node)
instead of parent.left = None
in Python?
Clarification: I would remove a tree like following (there might be some errors) in Python.
def delete_tree(root, parent):
if not root:
return
if not root.left and not root.right:
if root is parent.left:
parent.left = None
else:
parent.right = None
delete_tree(root.left, root)
delete_tree(root.right, root)
root = None
In the provided C code, a node can be deleted by releasing the allocated memory for a specific node. In Python, however, I need a reference to the node's parent to delete the node. Is there a simpler way than my code to delete a specific node from a tree?
As explained in comments, there's no need for this in Python. But if you want Python code that "acts like" the C code, here you go:
class Node:
# with data members .data, .left, and .right
def __init__(self, data):
self.data = data
self.left = self.right = None
def deleteTree(node):
if node is not None:
deleteTree(node.left)
deleteTree(node.right)
node.left = node.right = None
It is impossible to spell "free this memory" in Python. All you can do - and all you need to do - is to stop referencing objects you no longer care about.
Edit: and, I should add, you'll eventually think this is a good thing: life is soooooo much more pleasant when you know you'll never see a NULL
pointer dereferencing segfault again ;-) And that's really "why" Python has no way to spell "free this memory".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With