在python

时间:2017-10-07 19:22:55

标签: python list tree

  

这是我的代码:

def load_game(self,universe_list):

    root = universe_list[0]
    self.temp_stack = stack.Stack()
    self.temp_stack.push(root)
    self.__tree = btree.BTree(root, universe_list[1], None) 

    self.__curr_node = self.__tree
    #print(self.__curr_node.data)
    for item in universe_list[1:]:
        if item[-1] == "?":
            self.temp_stack.push(item)
            self.__tree = btree.BTree(item, None, None)
            self.__curr_node = self.__tree
        else:
            if self.__curr_node.left is None:
                self.__curr_node.attachItemLeft(item)
            elif self.__curr_node.right is None:
                self.__curr_node.attachItemRight(item)
                self.temp_stack.pop()
            else:
                stackValue = self.temp_stack.pop()
                itemNode = btree.BTree(stackValue,self.__curr_node, Node(item, None, None))
                self.__curr_node = itemNode

        self.__tree = self.__curr_node                    


def save_game(self):

    return_list=[]

    if self.__tree is not None:
        self.__tree.set_traverse(btree.BTree.PRE)

        for i in self.__tree:                
            return_list.append(i)
    else:
        raise Exception("List is empty.")
    return
    class BTree(object):

PRE="PRE"
POST="POST"
IN="IN"

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

    self.left=left
    self.right=right
    self.data=data
    self.set_traverse(BTree.IN)


def set_traverse(self,traversal):

    if(traversal != BTree.PRE and traversal != BTree.POST and traversal != BTree.IN):
        raise RuntimeError(str(traversal)+" is not a valid value for an iterator type")
    self.__traversal=traversal


def __iter__(self):
    if(self.__traversal==BTree.IN):
        return self.__inorder(self)
    elif(self.__traversal==BTree.PRE):
        return self.__preorder(self)
    elif(self.__traversal==BTree.POST):
        return self.__postorder(self)
    else:
        raise RuntimeError("Somehow we are traversing in no direction")

def __inorder(self, node):
    if node is None:
        raise StopInteration
    if node.left is not None:
        for x in self.__inorder(node.left) :
            yield x
    yield node.data
    if node.right is not None:
        for x in self.__inorder(node.right) :
            yield x

def __preorder(self, node):
    if node is None:
        raise StopInteration
    yield node.data
    if node.left is not None:
        for x in self.__preorder(node.left) :
            yield x
    if node.right is not None:
        for x in self.__preorder(node.right) :
            yield x           

def __postorder(self, node):
    if node is None:
        raise StopInteration
    if node.left is not None:
        for x in self.__postorder(node.left) :
            yield x
    if node.right is not None:
        for x in self.__postorder(node.right) :
            yield x  
    yield node.data

def attachItemLeft(self,data):
    self.left=BTree(data=data)

def attachItemRight(self,data):
    self.right=BTree(data=data)
  

输入:

     

宇宙= [ “腿?”, “猫”, “地下?”, “蚯蚓”, “蛇”, “测试?”, “是”, “否”]

     

测试=游戏(宇宙)

     

test.load_game(宇宙)

     

test.save_game()

     

输出:['测试?','是','否']

     

查询:我的代码只打印最后一棵树(root,left,right)。我想打印树的所有节点

0 个答案:

没有答案