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 help me understand how to use trees in code in general?
I understand the concept, not the code. It is too abstract for me.
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