class TestInOrderTreeTraverser(unittest.TestCase): def setUp(self): self.t = ListOfListsTree([5, [3, [1, [0], [2]], [4]], [7, [6], [8]]]) def test_InOrderTreeTraverser_onITree_enumeratesNodes(self): for i, v in enumerate(InorderTreeTraverser().traverse(self.t)): self.assertEqual(i, v) def test_InOrderTreeTraverser_onITree_appliesVisitor(self): for i, child in enumerate(InorderTreeTraverser().traverse( self.t, visitor=lambda t: t)): self.assertEqual(self.t.out_degree(i), child.out_degree(child.root))
def parse(self, expression: "str") -> "ITree<str>": S = self.createLifo() tree = [] op = {'+': 0, '-': 0, '*': 1, '/': 1} for token in self.tokenize(expression): if type(token) == int: tree.append([token]) elif token in op: while len(S) > 0 and S.top( ) in op and op[token] <= op[S.top()]: tree[-2:] = [[S.pop(), tree[-2], tree[-1]]] S.push(token) elif token == '(': S.push('(') elif token == ')': while S.top() != '(': tree[-2:] = [[S.pop(), tree[-2], tree[-1]]] S.pop() while len(S) > 0: tree[-2:] = [[S.pop(), tree[-2], tree[-1]]] return ListOfListsTree(tree[0]) #]pre
def setUp(self): self.t = ListOfListsTree( [0, [1, [4]], [2, [5], [6]], [3, [7, [9], [10]], [8]]])
def setUp(self): self.t = ListOfListsTree([5, [3, [1, [0], [2]], [4]], [7, [6], [8]]])
def setUp(self): self.t = ListOfListsTree( [10, [9, [6, [4, [0], [3, [1], [2]]], [5]], [8, [7]]]])
def setUp(self): self.t = ListOfListsTree( [0, [1, [2, [3, [4], [5, [6], [7]]], [8]], [9, [10]]]])