Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于使用顺序遍历的一些内容将打印二叉树中的所有项_Python_Pycharm_Computer Science - Fatal编程技术网

Python 关于使用顺序遍历的一些内容将打印二叉树中的所有项

Python 关于使用顺序遍历的一些内容将打印二叉树中的所有项,python,pycharm,computer-science,Python,Pycharm,Computer Science,我们不知道如何按遍历顺序打印,也不知道如果输入二进制字符,如何打印结果,有人能帮我们吗 class TreeNode: def __init__(self , item , left , right): self.item = item self.right = right self.left = left class BinaryTree: def __init__(self): self.root = None

我们不知道如何按遍历顺序打印,也不知道如果输入二进制字符,如何打印结果,有人能帮我们吗

class TreeNode:
    def __init__(self , item , left , right):
        self.item = item
        self.right = right
        self.left = left

class BinaryTree:
    def __init__(self):
        self.root = None
    def add(self , item , binary_str):
        binary_str_itr = iter(binary_str)
        self.root = self.add_aux(self.root , item , binary_str_itr)

    def add_aux(self , current , item , binary_str_itr):
        if current is None:
            current = TreeNode(None , None , None)
        try:
            bit = next(binary_str_itr)
            if bit == '0':
                current.left = self.add_aux(current.left , item , binary_str_itr)
            elif bit == '1':
                current.right = self.add_aux(current.right , item , binary_str_itr)
        except StopIteration:
            current.item = item
        return current


    def print_inorder(self):
        self.print_inorder_aux(self.root)

    def print_inorder_aux(self,current):
        if current is not None:           #if not a base case
            self.print_inorder_aux(current.left)
            print(current)
            self.print_inorder_aux(current.right)

对于Inoder遍历,拇指规则是

leftChild - Root - rightChild 

继续下面的链接

@山姆大叔,对于你的情况,金拇指法则是self.left-self.item-self.right