在Python中以前缀表示法以字符串表达式递归创建二叉树

时间:2018-11-23 23:57:35

标签: python recursion binary-tree prefix

Representation of LinkedBinaryTree that should be outputted
    导入LinkedBinaryTree

def create_expression_tree(prefix_exp_str):

    def create_expression_tree_helper(prefix_exp, start_pos):
        start_pos += 1
        op = ['+', '-', '*', '/']
        elem = prefix_exp[start_pos]
        if elem == ' ':
            elem = prefix_exp[start_pos+1]
            start_pos += 1

        if elem not in op:
            return LinkedBinaryTree.LinkedBinaryTree.Node(int(elem))
        else:
            left = create_expression_tree_helper(prefix_exp, start_pos)
            right = create_expression_tree_helper(prefix_exp, start_pos+2)
            return LinkedBinaryTree.LinkedBinaryTree.Node(elem, left, right)

    tree = LinkedBinaryTree.LinkedBinaryTree(create_expression_tree_helper(prefix_exp_str, -1))

    return tree

tree1 = create_expression_tree('* 2 + - 15 6 4')
for i in tree1.preorder():
    print(i.data, end='')

我实现了自己的二叉树类,如下所示。 Preorder()是我的LinkedBinaryTree的生成器,它以前缀顺序提供树的值。有了这段代码,我正在输出 * 2 + -151 但应该输出 * 2 + -1564(如果正确创建了二进制表达式树)。 我知道数字大于1位数存在问题,但是我不确定如何在不损害运行时的情况下(即使用切片)进行修复。而且我不确定为什么它会省略一些标记。有任何想法吗? (该实现必须在线性时间内运行,并且在我定义的辅助函数中不能使用其他参数)。

import ArrayQueue

class LinkedBinaryTree:

    class Node:
        def __init__(self, data, left=None, right=None):
            self.data = data
            self.parent = None
            self.left = left
            if (self.left is not None):
                self.left.parent = self
            self.right = right
            if (self.right is not None):
                self.right.parent = self

    def __init__(self, root=None):
        self.root = root
        self.size = self.subtree_count(root)

    def __len__(self):
        return self.size

    def is_empty(self):
        return len(self) == 0

    def subtree_count(self, root):
        if (root is None):
            return 0
        else:
            left_count = self.subtree_count(root.left)
            right_count = self.subtree_count(root.right)
            return 1 + left_count + right_count


    def sum(self):
        return self.subtree_sum(self.root)

    def subtree_sum(self, root):
        if (root is None):
            return 0
        else:
            left_sum = self.subtree_sum(root.left)
            right_sum = self.subtree_sum(root.right)
            return root.data + left_sum + right_sum


    def height(self):
        return self.subtree_height(self.root)

    def subtree_height(self, root):
        if (root.left is None and root.right is None):
            return 0
        elif (root.left is  None):
            return 1 + self.subtree_height(root.right)
        elif (root.right is  None):
            return 1 + self.subtree_height(root.left)
        else:
            left_height = self.subtree_height(root.left)
            right_height = self.subtree_height(root.right)
            return 1 + max(left_height, right_height)


    def preorder(self):
        yield from self.subtree_preorder(self.root)

    def subtree_preorder(self, root):
        if(root is None):
            return
        else:
            yield root
            yield from self.subtree_preorder(root.left)
            yield from self.subtree_preorder(root.right)


    def postorder(self):
        yield from self.subtree_postorder(self.root)

    def subtree_postorder(self, root):
        if(root is None):
            return
        else:
            yield from self.subtree_postorder(root.left)
            yield from self.subtree_postorder(root.right)
            yield root


    def inorder(self):
        yield from self.subtree_inorder(self.root)

    def subtree_inorder(self, root):
        if(root is None):
            return
        else:
            yield from self.subtree_inorder(root.left)
            yield root
            yield from self.subtree_inorder(root.right)


    def breadth_first(self):
        if (self.is_empty()):
            return
        line = ArrayQueue.ArrayQueue()
        line.enqueue(self.root)
        while (line.is_empty() == False):
            curr_node = line.dequeue()
            yield curr_node
            if (curr_node.left is not None):
                line.enqueue(curr_node.left)
            if (curr_node.right is not None):
                line.enqueue(curr_node.right)

    def __iter__(self):
        for node in self.breadth_first():
            yield node.data

我在这里为LinkedBinaryTree类添加了代码。广度遍历方法的实现中使用的ArrayQueue类只是使用Python列表作为基础数据结构的基本队列。

1 个答案:

答案 0 :(得分:0)

您的代码有两个问题:

  • 您一一处理了字符,但可能会出现几位数(通过将表达式拆分为列表来固定)
  • 您没有考虑到以下事实:链接的运算符表达式可能比标准增量更长(通过在size上添加Node属性来解决此问题

这是新的Node

class Node:
    def __init__(self, data, left=None, right=None):
        self.data = data
        self.parent = None
        self.left = left
        if (self.left is not None):
            self.left.parent = self
        self.right = right
        if (self.right is not None):
            self.right.parent = self

    @property
    def size(self):
        l = 1
        if self.left is not None:
            l += self.left.size
        if self.right is not None:
            l += self.right.size
        return l

这是新的树创建者

def create_expression_tree(prefix_exp_str):

    expr_lst = prefix_exp_str.split(" ")

    op = {'+': None, '-': None, '*': None, '/': None}

    def create_expression_tree_helper(prefix_exp, start_pos):
        start_pos += 1
        elem = prefix_exp[start_pos]

        node = None

        if elem not in op:
            node = LinkedBinaryTree.Node(int(elem))
        else:
            left = create_expression_tree_helper(prefix_exp, start_pos)
            incr = left.size
            right = create_expression_tree_helper(prefix_exp, start_pos+incr)
            node = LinkedBinaryTree.Node(elem, left, right)

        return node

    tree = LinkedBinaryTree(create_expression_tree_helper(expr_lst, -1))

    return tree

编辑,这是不需要修改Node类的版本

def create_expression_tree(prefix_exp_str):

    expr_lst = prefix_exp_str.split(" ")

    op = {'+': None, '-': None, '*': None, '/': None}

    def create_expression_tree_helper(prefix_exp, start_pos):
        start_pos += 1
        elem = prefix_exp[start_pos]

        node = None
        size = 1

        if elem not in op:
            node = LinkedBinaryTree.Node(int(elem))
        else:
            left, left_size   = create_expression_tree_helper(prefix_exp, start_pos)
            right, right_size = create_expression_tree_helper(prefix_exp, start_pos+left_size)

            node  = LinkedBinaryTree.Node(elem, left, right)
            size += left_size + right_size

        return node, size

    tree = LinkedBinaryTree(create_expression_tree_helper(expr_lst, -1)[0])

    return tree