【问题标题】:Creating a List from a Binary Search Tree从二叉搜索树创建列表
【发布时间】:2013-10-20 04:00:00
【问题描述】:

我正在尝试列出二叉搜索树中的所有项目。我理解递归,但我不知道如何让它返回每个值,然后将其附加到列表中。我想创建一个名为makeList() 的函数,它将返回树中所有项目的列表。除了makeList() 函数之外,我的程序中的所有函数都可以工作,并且包含在内是为了确保每个人都了解我如何设置树的基本结构。

class Node(object):
    def __init__(self, data):
        self.data = data
        self.lChild = None
        self.rChild = None

class Tree(object):
    def __init__(self):
        self.root = None

    def __str__(self):
        current = self.root

    def isEmpty(self):
        if self.root == None:
            return True
        else:
            return False

    def insert (self, item):
        newNode = Node (item)
        current = self.root
        parent = self.root

        if self.root == None:
            self.root = newNode
        else:
            while current != None:
                parent = current
                if item < current.data:
                    current = current.lChild
                else:
                    current = current.rChild

            if item < parent.data:
                parent.lChild = newNode
            else:
                parent.rChild = newNode

    def inOrder(self, aNode):
        if aNode == None:
            pass
        if aNode != None:
            self.inOrder(aNode.lChild)
            print aNode.data
            self.inOrder(aNode.rChild)

    def makeList(self, aNode):
        a = []
        self.inOrder(aNode)
        a += [aNode.data]
        print a

n = Tree()
for i in [4,7,2,9,1]:
    n.insert(i)

n.makeList(n.root)

查看我的makeList() 函数,我知道为什么它不起作用,但我不知道如何使它起作用。

编辑

好的,我明白了!我什至得到了两个答案:

def makeList(self, aNode, a = []):
    if aNode != None:
        self.makeList(aNode.lChild, a)
        a += [aNode.data]
        self.makeList(aNode.rChild, a)
    return a

和

def makeList2(self, aNode):
    if aNode is None:
        return []
    return self.makeList2(aNode.lChild) + [aNode.data] + self.makeList2(aNode.rChild)

回首往事,我发现我对递归不是很了解,所以是时候阅读书籍了!任何人有任何关于递归的好资源?

另一个问题,假设我调用了我的makeList() 函数。当 Python 通过makeList() 时,当它到达self.makeList(aNode.lChild, a) 时,它会在它仍在完成makeList() 函数的同时再次开始运行该函数,还是一切都停止了,它只是以新的aNode 重新开始?

我希望这是有道理的。

【问题讨论】:

  • 如果你想读一本关于树递归的书,我想推荐《编程面试暴露:John Mongan 找到下一份工作的秘密》。
  • 避免在函数签名中使用可变的默认值作为参数。这些可变值在函数定义期间(而不是它的调用)被评估一次,因此它可能是许多奇怪错误的罪魁祸首。基本上很多函数调用都会使用同一个可变对象。

标签: python binary-search-tree


【解决方案1】:

你离得太近了! makeList 可以很简单:

def makeList(self, aNode):
    if aNode is None:
        # Stop recursing here
        return []
    return self.makeList(aNode.lChild) + [aNode.data] + self.makeList(aNode.rChild)

基本上,确保您没有尝试递归过去的空节点。然后返回左树的列表,当前节点,右树的列表。

【讨论】:

  • 哈哈你们让我思考!我会努力解决这个问题,然后再回复你们!
  • 如果左叶或右叶都没有会发生什么?
【解决方案2】:

inOrder 打印内容但不返回任何内容,因此对于构建列表没有用处。您需要一种按顺序返回每个节点的方法。这可能是您的课程尚未涵盖的内容,但请查看yield 命令。

【讨论】:

  • 是的,迭代器可以很容易地打印树并从中生成扁平列表,并且通常对类的其他用户也很有用。迭代器++
【解决方案3】:

基本思路是这样的:

def makeList(self):
    return self.lChild.makeList() + [self.data] + self.rChild.makeList()

看看它和 inOrder 本质上是一样的吗?

你的程序有不同的结构,这使得它更难实现,但基本思想是一样的。

【讨论】:

  • 现在我看到它基本上是一样的。很难想象。
  • 谢谢。此代码适用于组合的节点和树类代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多