树的实现(多结点,链表)

这段代码的测试过程中的问题在哪?明明已经给定了preorder,为什么还是显示

img


import copy
# 存储方法:树的孩子兄弟链表
"""
结点
"""
class TreeNode:
    def __init__(self,data,first_child=None,next_sibling=None):
        self.data=data
        self.first_child=first_child
        self.next_sibling=next_sibling
"""
基本操作
"""
class Tree:
    def __init__(self):
        self._root=None
#     构造空树
    def create_empty(self):
        data=None
        sub_root=TreeNode(data)
        if sub_root.first_child and sub_root.next_sibling:
            sub_root.first_child=self.create_empty()
            sub_root.next_sibling=self.create_empty()
        return sub_root
#     后序遍历
    def recursive_postorder(self,sub_root):
        if sub_root:
            self.recursive_postorder(sub_root.first_child)
            print(sub_root.data,end='')
            self.recursive_postorder(sub_root.next_sibling)
#   先序遍历
    def recurive_preorder(self,sub_root):
        if sub_root:
            print(sub_root.data,end='')
            self.recurive_preorder(sub_root.first_child)
            self.recursive_postorder(sub_root.next_sibling)
#     从带#的先序序列创建树
    def recursive_create(self,preorder):
        data=preorder.pop(0)
        if data=="#":
            sub_root=None
        else:
            sub_root=TreeNode(data)
            sub_root.first_child=self.recursive_create(preorder)
            sub_root.next_sibling=self.recursive_create(preorder)
        return sub_root
#     树的层次遍历
    def level_treversal(self):
        if self._root is None:
            return
        lst=[]
        lst.append(self._root)
        while lst!=[]:
            first=lst[0]
            lst.pop(0)
            print(first.data,end='')
            if first.first_child:
                lst.append(first.first_child)
#     树的结点数
    def __len__(self):
        return self.recursive_size(self._root)
    def recursive_size(self,sub_root):
        if not sub_root:
            return 0
        else:
            return 1+self.recursive_size(sub_root.first_child)+self.recursive_size(sub_root.next_sibling)
#     树的叶节点数
    def leaf_number(self,sub_root,count=0):
        if sub_root is None:
             count+=1
        else:
            if sub_root.first_child is None:
                return self.leaf_number(sub_root.first_child)
            if sub_root.next_sibling is None:
                return self.leaf_number(sub_root.next_sibling)
        return count
#     清空树
    def clear(self,sub_root):
        sub_root=TreeNode(None)
        self.clear(sub_root.first_child)
        self.clear(sub_root.next_sibling)
#     求树的高度
    def recursive_height(self,sub_root):
        if not sub_root:
            return 0
        maxHeight=0
        p=sub_root.first_child
        while p:
            h=self.recursive_height(p)
            if h>maxHeight:
                maxHeight=h
            p=p.next_sibling
        return maxHeight+1
#     树的复制
    def __copy__(self,old_Tree):
        new_Tree=Tree()
        new_Tree=copy.copy(old_Tree)
        return new_Tree
#     输出所有叶结点的值
    def leaf_data(self,sub_root):
        if sub_root.first_child is None:
            print(sub_root.data,end="")
        return self.leaf_data(sub_root.next_sibling)
#     输出所有非叶结点的值
    def nonleaf_data(self,sub_root):
        if sub_root:
            print(sub_root.data,end='')
            if sub_root.first_child:
                self.nonleaf_data(sub_root.first_child)
            if sub_root.next_sibling:
                self.nonleaf_data(sub_root.next_sibling)
#     读取根节点
    def getroot(self):
        sub_root=copy.copy(self._root)
        return sub_root

if __name__ == '__main__':
    t=Tree()
    preorder=['A','B','F','G','M','N','C','H','D','I','J','K','E','L']
    t.recursive_create(['A','B','F','G','M','N','C','H','D','I','J','K','E','L'])
    print(t)
    sub_root=t.getroot()
    t.recurive_preorder(sub_root)
    # print(t.recurive_preorder(sub_root))
    t.recursive_postorder(sub_root)
    t.level_treversal()
    t. __len__()

你少给了个 # 号,结果内容 pop 完了,你还在 pop