Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

python - How to remove a binary tree recursively

Hi I have problem with my task in Python. I must delet all tree without T.root = None, because our teacher says it can sometimes leak memory. He wants to do it in order as in the postorder method. My code removes most of the tree but leaves the leftmost nodes from the root.If you run the code you will see the result and understand what exactly the problem is. Anyone have any ideas?

import random
class Tree:

def __init__(self):
    self.root = None

class Node:
    def __init__(self):
        self.key = None
        self.parent = None
        self.left = None
        self.rigth = None

def make_tree(self, n):
    for i in range(n):
        new_node = self.Node()
        new_node.key = random.randint(1, 100)
        if self.root == None:
           self.root = new_node
        else:
            x = self.root
            y = None
            while x != None:
                r = random.randint(0,1)
                if r == 0:
                    y = x
                    x = x.left
                else:
                    y = x
                    x = x.rigth
            if r == 0:
                y.left = new_node
                new_node.parent = y
            else:
                y.rigth = new_node
                new_node.parent = y

def print_tree(self):
    self.walk_tree(self.root, 0)

def walk_tree(self, x, y = 0):
    if x != None:
        self.walk_tree(x.left, y + 1)
        print("    " * y, x.key)
        self.walk_tree(x.rigth, y+1)

def in_order(self, x, y = 0):
    if x != None:
        self.in_order(x.left, y + 1)
        print(x.key)
        self.in_order(x.rigth, y+1)

def pre_order(self, x, y = 0):
    if x != None:
        print(x.key)
        self.pre_order(x.left, y + 1)
        self.pre_order(x.rigth, y+1)

def post_order(self, x, y = 0):
    if x != None:
        self.post_order(x.left, y + 1)
        self.post_order(x.rigth, y+1)
        print(x.key)

def clear_tree(self, x, y = 0):
    if x != None:
        self.clear_tree(x.left, y + 1)
        self.clear_tree(x.rigth, y+1)
        if x != T.root:
            if x.parent.rigth == x.key:
                y = x.parent
                x.parent = None
                y.rigth = None
            else:
                y = x.parent
                x.parent = None   
                y.left = None
        

T = Tree()
T.make_tree(10)
T.print_tree()
#T.in_order(T.root, 0)
print()
#T.pre_order(T.root, 0)
print()
T.post_order(T.root, 0)
print()
T.clear_tree(T.root)
print()
T.print_tree()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...