簡體   English   中英

在Python中刪除樹(數據結構)

[英]Deleting a tree(data structure) in Python

以下是刪除C中的樹(由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);
}

刪除C中的節點可以通過free(node) 在Python中,沒有這樣的方法。 我是否需要引用節點的父節點來刪除Python中的節點,而我在C中不需要一個節點?

(也就是說,在Python中有沒有辦法delete(node)而不是parent.left = None

澄清:我會在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

在提供的C代碼中,可以通過釋放特定節點的分配內存來刪除節點。 但是,在Python中,我需要引用節點的父節點來刪除節點。 是否有比我的代碼更簡單的方法從樹中刪除特定節點?

正如評論中所解釋的那樣,在Python中沒有必要這樣做。 但是,如果您希望Python代碼“像C代碼一樣”,那么您可以:

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

在Python中拼寫“釋放這個內存”是不可能的。 所有你能做的 - 你需要做的就是 - 停止引用你不再關心的對象。

編輯:和,我要補充,你最終會覺得這是一件好事 :生活是soooooo更加舒適,當你知道你永遠不會看到一個NULL再次指針引用段錯誤;-)這就是真正的“為什么” Python有沒辦法拼出“釋放這段記憶”。

我認為你對C和Python內存管理的差異有一點誤解。 在C語言中,您需要刪除之前從堆中獲取的所有未使用的內存。 您可以通過調用free函數來完成。
Python使用垃圾收集進行自動內存管理。 所以,通過做parent.left = None你告訴:對這個對象的引用將是None ,沒有其他人知道對這個對象的引用,我們可以收集它。 垃圾收集器處理其他所有內容,因此您無需從堆中顯式刪除此對象。

是否有比我的代碼更簡單的方法從樹中刪除特定節點?

您沒有從樹中刪除特定節點,而是刪除樹本身。 您可以更輕松地在Python中執行此操作 - 只需將其根目錄的引用設置為None ,垃圾收集器將關注樹。

我想你要找的是:

node ##class '__main__.node'
del(node)
node
Traceback (most recent call last):
NameError: name 'node' is not defined

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM