【问题标题】:printing out python list from python binary tree从 python 二叉树打印出 python 列表
【发布时间】:2016-05-25 02:42:50
【问题描述】:

我想定义一个返回树节点值列表的函数。该列表按级别顺序(从上到下,从左到右),如果 child 丢失,然后在其位置,插入“None”。

这是二叉树的实现

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()       

【问题讨论】:

    标签: python python-3.x tree binary-tree


    【解决方案1】:

    添加整个 .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()
    

    【讨论】:

      【解决方案2】:

      你应该向这个函数传递一个空列表

      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 将拥有你想要的列表

      【讨论】:

      • 我打算用它来调用 tree =BinaryTree(2) tree.insert_left(31) tree.insert_right(5) right = tree.get_right_subtree() left = tree.get_left_subtree() left。 insert_left(27) right.insert_right(1) right2 = right.get_right_subtree() right2.insert_left(7)
      • 但是 return_list 有两个参数,所以我在调用时将使用什么作为我的第二个参数,例如 return_list(tree,...)
      • 不,这将是类的方法,因此您将其称为 tree.retrun_list(templist)。其中 templist 是一个空列表。
      • 当我调用它时出现此错误 AttributeError: 'BinaryTree' object has no attribute 'return_list'
      • 您应该将此方法添加到您的课程中。这将是成员方法
      【解决方案3】:

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

        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
      

      【讨论】:

      • 它似乎不起作用它没有提供水平顺序遍历
      • 你得到的输出是什么
      • [2, 29, 26, 无, 无, 无, 4, 无, 2, 9, 无, 无, 无]
      • 但应该是这样的:[2, 29, 4, 26, None, None, 2, None, None, None, None, None, None, 9, None]
      • 我试图避免将函数放在类中,而只是让它与类分开?
      【解决方案4】:

      这不是答案,只是想添加我得到的结果和说明

      $ python binarayTree.py
      3---+
      (l) 29---+
      (l)     26---+
      (r) 4---+
      (r)     2---+
      (l)         9---+
      
      [3, 29, 4, 26, None, None, 2, None, None, 9, None, None, None]
      

      这里是结果列表的解释

      first level 3
      second level 29,4
      third level 26, None, None ,2
      fourth level None, None,9, None, None, None
      

      【讨论】:

      • 你通过你提供的 def return_list 函数得到这个?
      • 我不明白为什么我得到不同的输出。我将代码稍作修改以取出self,并添加了一个tree参数
      • 某些东西一定是不同的,因为我复制粘贴了所有内容,我的结果与你的不同:/
      • 不,不是来自 def return_list,而是来自 return_b_list
      • 让我添加py文件
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      相关资源
      最近更新 更多