#🔒 Expressions into Tree, somebody help me with the basics of trees, I am stuck ..

6 messages · Page 1 of 1 (latest)

somber violet
#

So, I am working on an exercise using expression Trees. The classes are already given but I cannot seem to find the functionality, can somebody explain to me how I can use these classes to work for example with mathematical expressions?

class TreeNode:
    def __init__(self, item=None, left=None, right=None):
        self._item = item
        self._left = left
        self._right = right

    def preorder(self, action) -> None:
        """
        Performs an action for each node in the tree in preorder
        :param action: function to call for every value in the tree
        """
        action(self._item)
        if self._left is not None:
            self._left.preorder(action)
        if self._right is not None:
            self._right.preorder(action)

    def postorder(self, action) -> None:
        """
        Performs an action for each node in the tree in postorder
        :param action: function to call for every value in the tree
        """
        if self._left is not None:
            self._left.postorder(action)
        if self._right is not None:
            self._right.postorder(action)
        action(self._item)

    def inorder(self, action) -> None:
        """
        Performs an action for each node in the tree in inorder
        :param action: function to call for every value in the tree
        """
        if self._left is not None:
            self._left.inorder(action)
        action(self._item)
        if self._right is not None:
            self._right.inorder(action)
from enum import Enum


class TokenType(Enum):
    """
    Tokens can be of three types:
    numbers (digits), identifiers (characters), and symbols (any)
    """
    NUMBER = 1
    IDENTIFIER = 2
    SYMBOL = 3


class TokenList:
    def __init__(self, value = None):
        self._type = TokenType.NUMBER
        self._value = value
        self._next = None

    def __str__(self):
        return_value = str(self._value)
        if self._next is not None:
            return_value += " " + str(self._next)
        return return_value


def _match_number(input_str: str, position: int) -> tuple[int, int]:
    # Precondition: input_str[position] is a number
    number = ""
    # Maybe accept a "-"?
    while position < len(input_str) and "0" <= input_str[position] <= "9":
        number += input_str[position]
        position += 1
    return int(number), position


def _match_identifier(input_str: str, position: int) -> tuple[str, int]:
    # Precondition: input_str[position] is a letter
    old_position = position
    while position < len(input_str) and input_str[position].isalnum():
        position += 1
    return input_str[old_position:position], position


def _match_symbol(input_str: str, position: int) -> tuple[str, int]:
    return input_str[position], position+1


def _generate_node(input_str: str, position: int) -> tuple[TokenList, int]:
    # Precondition: input_str is not whitespace
    new_node = TokenList()
    if "0" <= input_str[position] <= "9":
        new_node._type = TokenType.NUMBER
        new_node._value, position = _match_number(input_str, position)
    elif input_str[position].isalpha():
        new_node._type = TokenType.IDENTIFIER
        new_node._value, position = _match_identifier(input_str, position)
    else:
        new_node._type = TokenType.SYMBOL
        new_node._value, position = _match_symbol(input_str, position)
    return new_node, position


def generate_token_list(input_str: str) -> TokenList:
    front = None
    back = None
    position = 0
    while position < len(input_str):
        if input_str[position].isspace():
            position += 1
        else:
            new_node, position = _generate_node(input_str, position)
            if front is None:
                front = new_node
                back = new_node
            else:
                back._next = new_node
                back = new_node
    return front
polar valveBOT
#

@somber violet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

somber violet
#

If I need to give more clarity please say so ❤️

somber violet
#

!close

polar valveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.