当前位置: 首页 > 知识库问答 >
问题:

从蟒蛇二叉树打印出蟒蛇列表

东门文斌
2023-03-14

我想定义一个返回树节点值列表的函数。列表按级别顺序排列(从上到下,从左到右),如果缺少孩子,则在其位置插入“无”。

这是二叉树实现

class BinaryTree:

def __init__(self, data, left = None, right = None):

    self.left = left
    self.right  = right

def insert_left(self, data):
    self.left = BinaryTree(data, left=self.left)  

def insert_right(self, data):
    self.right = BinaryTree(data, right=self.right)

def set_value(self, val):
    self.key = val

def get_value(self):
    return self.key

def create_string(self, indent):
    string = str(self.key) + '---+'
    if self.left:
        string += '\n(l)' + indent + self.left.create_string(indent + '    ')
    if self.right:
        string += '\n(r)' + indent + self.right.create_string(indent + '    ')
    return string

def __str__(self):
    return self.create_string('  ')

def return_list(self, templist):
    templist.append(self.key)
    if self.left is None:
        templist.append(None)
    else:
        self.left.return_list(templist)
    if self.right is None:
        templist.append(None)
    else:
        self.right.return_list(templist)

def main():    
    tree = BinaryTree(3) 
    tree.insert_left(29)
    tree.insert_right(4)
    right = tree.get_right_subtree()
    left = tree.get_left_subtree()
    left.insert_left(26)
    right.insert_right(2)
    right2 = right.get_right_subtree()
    right2.insert_left(9)
    templist = []
    tree.return_list(templist)
main()       

共有3个答案

慎建本
2023-03-14

如果您确实在寻找广度优先搜索,那么此方法可能会有所帮助

  def return_b_list(tree,templist,queue):
    if tree is None:
        return;
    queue.append(tree)
    while len(queue) !=0:
        node = queue.popleft()
        if node is None:
            templist.append(None)
        else:
            templist.append(node.key)
            queue.append(node.left)
            queue.append(node.right)

如何打电话?(此方法不必是类的一部分)

def main():    
    tree = BinaryTree(3) 
    tree.insert_left(29)
    tree.insert_right(4)
    right = tree.get_right_subtree()
    left = tree.get_left_subtree()
    left.insert_left(26)
    right.insert_right(2)
    right2 = right.get_right_subtree()
    right2.insert_left(9)
    templist = []
    queue = deque() # you should do a from collections import deque
    return_b_list(tree,templist,queue)
    print templist
匡旭东
2023-03-14

您应该向此函数传递一个空列表

def return_list(self, templist):
    templist.append(self.key)
    if self.left is None:
        templist.append(None)
    else:
        self.left.return_list(templist)
    if self.right is None:
        templist.append(None)
    else:
        self.right.return_list(templist)

调用此方法后,templist 将具有所需的列表

丁沛
2023-03-14

添加整个. py文件如果您运行它应该可以工作

from collections import deque

class BinaryTree:
    def __init__(self, data, left = None, right = None):
        self.key = data
        self.left = left
        self.right  = right

    def insert_left(self, data):
        self.left = BinaryTree(data, left=self.left)  

    def insert_right(self, data):
        self.right = BinaryTree(data, right=self.right)

    def get_left_subtree(self):
        return self.left

    def get_right_subtree(self):
        return self.right

    def set_value(self, val):
        self.key = val

    def get_value(self):
        return self.key

    def create_string(self, indent):
        string = str(self.key) + '---+'
        if self.left:
            string += '\n(l)' + indent + self.left.create_string(indent + '    ')
        if self.right:
            string += '\n(r)' + indent + self.right.create_string(indent + '    ')
        return string

    def __str__(self):
        return self.create_string('  ')

    def return_list(self, templist):
        templist.append(self.key)
        if self.left is None:
            templist.append(None)
        else:
            self.left.return_list(templist)
        if self.right is None:
            templist.append(None)
        else:
            self.right.return_list(templist)


def return_b_list(tree,templist,queue):
    if tree is None:
        return;
    queue.append(tree)
    while len(queue) !=0:
        node = queue.popleft()
        if node is None:
            templist.append(None)
        else:
            templist.append(node.key)
            queue.append(node.left)
            queue.append(node.right)




def main():    
    tree = BinaryTree(3) 
    tree.insert_left(29)
    tree.insert_right(4)
    right = tree.get_right_subtree()
    left = tree.get_left_subtree()
    left.insert_left(26)
    right.insert_right(2)
    right2 = right.get_right_subtree()
    right2.insert_left(9)
    templist = []
    queue = deque() # you should do a from collections import deque
    return_b_list(tree,templist,queue)
    print tree.create_string(' ')
    print templist

main()
 类似资料:
  • 我正在运行Ubuntu 18.04。 我使用mysql连接器-python连接Python到MySQL。 我使用的是Python 3.6.7,并且已经安装了mysql连接器-python。 我已经安装了mysql连接器-python-py3_8.0.13-1ubuntu18.10_all.deb. 在运行Python脚本时,mysql。连接器模块似乎加载正确,但脚本在碰到光标时失败。next()具

  • 假设我有一些资源,我想在用python编写的aws lambda中的不同请求之间共享。我应该如何实现这一点? 是否有“启动后”挂钩,或者我应该在第一次调用时惰性地创建资源?“延迟初始化”的缺点是,它意味着一些请求会随机变慢,因为您选择了一个消费者来承担启动成本。 此外…这些资源会在lambda可执行文件被“冻结”后幸存下来吗? 本页https://docs.aws.amazon.com/lambd

  • 我需要在我的中添加一个新的目录位置,但问题是我使用的是一个全新安装的系统(Linux),其中尚未定义任何。我读过并使用过,我认为我很了解它,但我不知道当没有存在时会发生什么。 我不能附加到不存在的东西上,但我希望当前发现的所有重要库都能正常工作,因此要小心,我在Python中使用了来获取所有标准值。然后我为定义了一个-变量,包括我刚刚找到的所有节点,以及我的新目录。但是哇,很多东西都停止工作了!P

  • 我正在尝试使用本教程从一个使用selenium and beautiful soup的站点提取房地产列表信息:https://medium.com/@ben.sturm/scraping-house-listing-data-using-Selenium-and-Beautiful Soup-1CBB94BA9492 目的是在找到“下一页”按钮之前收集第一页的所有href链接,导航到下一页并收集该

  • 我是一名程序员,但我之前没有使用Python或其任何库的经验,甚至没有OCR/ALPR的整体经验。我有一个脚本,我做的(基本上复制和粘贴其他脚本在整个网络上),我假装用来识别车牌。但事实是我的代码现在非常糟糕。它可以很好地识别图像中的文本,但它很难捕捉车牌。我很少能用它拿到牌照。 因此,我需要一些帮助,说明我应该如何更改代码以使其更好。 在我的代码中,我只需选择一个图像,将其转换为二进制和BW,然

  • 如何在matplotlib中绘制多个条形图,当我多次尝试调用bar函数时,它们重叠,如下图所示,最高值红色只能看到。如何在x轴上绘制带有日期的多个条形图? 到目前为止,我尝试了这个: 我得到了这个: 结果应该是这样的,但是日期在x轴上,横条彼此相邻: