"""An expression tree implementation for Boolean expressions."""

from ..operators import OPERATOR_MAPPING, TT_NOT_OP
from .tree_node import (BinaryOperatorExpressionTreeNode,
                        OperandExpressionTreeNode,
                        UnaryOperatorExpressionTreeNode)


class BooleanExpressionTree(object):

    """An expression tree for Boolean expressions.

    Notes:
        This class expects any input it receives to be well-formed; any
        tokenized lists you pass it directly (instead of through the
        ``BooleanExpression`` class) will not be checked.

    Attributes:
        postfix_tokens (List[str]): The tokens in the expression from which to
            build the tree.
        root (tt.trees.ExpressionTreeNode): The root of the tree;
            ``None`` for an empty tree.

    """

    def __init__(self, postfix_tokens):
        self.postfix_tokens = postfix_tokens

        self.root = None
        self._build_tree()

    def _build_tree(self):
        """Iterate over the ``postfix_tokens``, constructing the tree.

        Notes:
            This method will populate this class's ``root`` attribute.

        """
        if not self.postfix_tokens:
            self.root = None
            return

        stack = []
        operators = OPERATOR_MAPPING.keys()

        for token in self.postfix_tokens:
            if token in operators:
                if OPERATOR_MAPPING[token] == TT_NOT_OP:
                    node = UnaryOperatorExpressionTreeNode(
                        token, stack.pop())
                else:
                    node = BinaryOperatorExpressionTreeNode(
                        token, stack.pop(), stack.pop())
            else:
                node = OperandExpressionTreeNode(token)

            stack.append(node)

        self.root = stack.pop()

    def __str__(self):
        return 'Empty!\n' if self.root is None else str(self.root)

@@ -1857,26 +1857,8 @@
     
- return 'Empty!%5Cn'
  if 
@@ -1874,21 +1874,78 @@
  is None
- else
+:%0A            return 'Empty!'%0A        else:%0A            return
  str(sel
@@ -1951,9 +1951,14 @@
 lf.root)
+%5B:-1%5D
 %0A

Remove trailing newline from ExpressionTree __str__