繁体   English   中英

在python中使用递归插入二叉搜索树

[英]insert in binary search tree using recursion in python

我正在尝试使用递归在二叉搜索树中插入值,但是当我使用中序遍历运行它时,我得到了 None 的输出。 我曾尝试查看实现此功能的其他语言,我只是尝试复制它,但它不起作用。 我将树的根传递给插入函数,如果它不为空,我希望它向左或向右遍历。 有人可以告诉我这有什么问题吗。 我尝试将 bst.root 改为 bst.get_root() ,但它仍然产生相同的结果。

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

class BinaryTree:
    def __init__(self):
        self.root = None
        self.size = 0

    def get_size(self):
        return self.size

    def get_root(self):
        return self.root

    def insert(self, root, value):
        if root is None:
            root = Node(value)
        else:
            if value < root.value:
                root.left = self.insert(root.left, value)
            else:
                root.right = self.insert(root.right, value)
        return root

    def inorder(self, root):
        if root == None:
            return
        else:
            self.inorder(root.left)
            print(root.value, end=" -> ")
            self.inorder(root.right)

bst = BinaryTree()
bst.insert(bst.root, 2)
bst.insert(bst.root, 4)
bst.insert(bst.root, 3)
bst.insert(bst.root, 1)

print(bst.inorder(bst.root))

问题是一个node可能是None并且您正在使用它调用一个函数,然后将一个节点分配给None并且没有保存任何内容。

让我们来看看:

def insert(self, root, value):  # say root is None
    if root is None:
        root = Node(value)  # here we're doing: `None = Node(value)`
    else:
        if value < root.value:
            root.left = self.insert(root.left, value)
        else:
            root.right = self.insert(root.right, value)
    return root 

为了修复它,我们需要“向上一级”,如果node.leftNone然后分配给node.left (顺便说一下,root 是一个坏名字,所以我使用“node”代替)。

一种方法是:

def insert(self, value):  # this is the function that gets called, pay attention that we're not sending `root`
    if self.root is None:
        self.root = Node(value)
    else:
        self._insert(self.root, value) # here's the call to a "private" function to which we are passing nodes down, starting from root

def _insert(self, node, value):
    if value < node.value:  # we know that `node` cannot be None - so it's safe to check its value! 
        if node.left:
            self._insert(node.left, value) # the recursive call is done only when `node.left` is not None
        else:
            node.left = Node(value)  # direct assignment
    else:
        if node.right:
            self._insert(node.right, value)
        else:
            node.right = Node(value)  # direct assignment

两条评论:

  1. 现在有一个更干净的界面,看看现在插入调用的样子:
    bst = BinaryTree()
    bst.insert(2)
    bst.insert(4)
    bst.insert(3)
    bst.insert(1)
  1. insert 是一个“设置”一个值的动作,因此 - 它不必返回任何东西(它可以,如果你真的想要的话)。

问题是您从未将插入的结果分配给根节点。 看看insert方法以及self.root是如何赋值_insert的返回值的。

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None


class BinaryTree:
    def __init__(self):
        self.root = None
        self.size = 0

    def get_size(self):
        return self.size

    def get_root(self):
        return self.root

    def insert(self, value):
        self.root = self._insert(self.root, value)

    def _insert(self, root, value):
        if root is None:
            root = Node(value)
        elif value < root.value:
            root.left = self._insert(root.left, value)
        elif value >= root.value:
            root.right = self._insert(root.right, value)
        return root

    def inorder(self, root):
        if root == None:
            return
        else:
            self.inorder(root.left)
            # print(root.value, end=" -> ")
            print(root.value)
            self.inorder(root.right)

bst = BinaryTree()
bst.insert(2)
bst.insert(4)
bst.insert(3)
bst.insert(1)

print(bst.inorder(bst.root))

编辑主要问题是插入调用的结果没有保存到根目录中。 如果您按如下方式更改原始代码,它也将起作用:

bst = BinaryTree()
bst.root = bst.insert(bst.root, 2)
bst.root = bst.insert(bst.root, 4)
bst.root = bst.insert(bst.root, 3)
bst.root = bst.insert(bst.root, 1)

暂无
暂无

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

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