【问题标题】:Binary Tree implementation with Separate class for node and tree具有用于节点和树的单独类的二叉树实现
【发布时间】:2019-01-13 15:28:12
【问题描述】:

我是 Python 和数据结构的新手。在学习二叉树时,我发现所有可用的实现在节点类中都有树的方法。

但我想以不同的方式实现它,除了 Node 类之外,还会有一个二叉树类,其中包含二叉树的方法。

这是我想出的代码,但它引发了我传递给函数 insertleft()insertright() 的参数的错误。

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

class Bt():
    def __init__(self, root):
        self.root = root

    def insertleft(temp.left, data):    
        if (temp.left == None):
            temp.left.data = data
        elif(data<temp.left.data):
            t1 = temp.left
            insertleft(t1.left, data)
        elif(data>temp.left.data):
            t1 = temp.left
            insertright(t1.left, data)

    def insertright(temp.right, data):  
        if (temp.right == None):
            temp.right.data = data
        elif(data<temp.right.data):
            t1 = temp.right
            insertleft(t1.right, data)
        elif(data>temp.right.data):
            t1 = temp.right
            insertright(t1.right, data)


    def insert(self, data):
        temp = self.root
        if(temp.data = None):
            temp.data = data
        elif(data<temp.data):
            insertleft(temp.left, data)
        elif(data>temp.data):
            insertright(temp.right, data)


r = Bt()
r.root = Node(5)
r.insert(4)
r.insert(6)

这是我收到的错误:

def insertleft(temp.left, data):  
                      ^
SyntaxError: invalid syntax

我不确定正确的语法应该是什么。提前感谢您的帮助

【问题讨论】:

  • 你在一个类中定义一个函数。第一个参数应该是一个变量,表示实例化的类对象 itself。以下参数是对传入参数的引用。所以 temp.left 在你的情况下应该只是 tempnode 。你的逻辑还有其他问题......
  • 另外,发现 this SO question 似乎你正在努力完成。

标签: python-3.x oop data-structures binary-tree binary-search-tree


【解决方案1】:

当您定义一个函数时,您还定义您将接受的参数。不过在类内部,只要方法不是静态方法或类方法,第一个参数应该是self 或表示实例化对象本身的变量。

在您的情况下,您在调用函数时已经传递了 temp.lefttemp.right 的值。将函数定义更改为仅使用 temp 应该可以正常工作。或者更易读,node,因为这就是您实际指的。

您的insertleftinsertright 函数与insert 执行相同的操作,并且不需要包含它们。只需修改insert 函数即可完成所有操作。

最终结果应该是这样的:

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

class Bt():
    def __init__(self, root=None):  #Added default value
        self.root = Node(root)

    def insert(self, data):
        if self.root.data is None:
            self.root.data = data   #Set the root data if it doesn't exist.
        else:
            self._insert(self.root, data)   #Prime the helper function with root node

    def _insert(self, node, data):    #Insert helper function to do the insertion
        if data < node.data:    #Check if data needs to be inserted on the left
            if node.left is None:   #Set left node if it doesn't exist
                node.left = Node(data)
            else:                   #Else let the left node decide where it goes as a child
                self._insert(node.left, data)
        else:                   #Else data needs to be inserted on the right
            if node.right is None:  #Set right node if it doesn't exist
                node.right = Node(data)
            else:                   #Else let the right node decide where it goes as a child
                self._insert(node.right, data)


r = Bt(5)
r.insert(4)
r.insert(6)

现在您只是缺少打印树和数据...以及访问数据的功能。

【讨论】:

  • 谢谢,我现在明白了。 :)
  • 这是我的搜索节点实现:
【解决方案2】:

二叉搜索树中的每个节点都被视为另一棵二叉搜索树。因此我们不需要为节点创建一个单独的类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多