Ejemplo n.º 1
0
 def build_parse_tree(self, expression):
     stack = StackLL()
     root_tree = BinaryTree('')
     current_tree = root_tree
     expression = expression.split()
     for token in expression:
         if token == '(':
             stack.push(current_tree)
             current_tree.insert_left_child(BinaryTree(''))
             current_tree = current_tree.get_left_child()
         elif token == ')':
             current_tree = stack.pop()
         elif token in ['+', '-', '/', '*']:
             current_tree.set_root_key(token)
             stack.push(current_tree)
             current_tree.insert_right_child((BinaryTree('')))
             current_tree = current_tree.get_right_child()
         elif token not in ['+', '-', '/', '*']:
             current_tree.set_root_key(int(token))
             current_tree = stack.pop()
         else:
             raise "INVALID EXPRESSION"
     return root_tree
Ejemplo n.º 2
0
    def testLLStack(self):
        stack_ll = StackLL()
        self.assertIsNone(stack_ll.peek())
        self.assertEqual(stack_ll.size(), 0)
        self.assertTrue(stack_ll.is_empty())

        stack_ll.push("Marcus")
        stack_ll.push(5)
        stack_ll.push(False)
        stack_ll.push(67.128)
        stack_ll.push("Carlos")
        stack_ll.push(StackLLNode)
        stack_ll.push(8)

        self.assertFalse(stack_ll.is_empty())
        self.assertEqual(stack_ll.peek(), 8)
        self.assertEqual(stack_ll.size(), 7)

        stack_ll.pop()
        stack_ll.pop()
        stack_ll.pop()

        self.assertFalse(stack_ll.is_empty())
        self.assertEqual(stack_ll.peek(), 67.128)
        self.assertEqual(stack_ll.size(), 4)

        self.assertEqual(str(stack_ll), "['Marcus', 5, False, 67.128]")

        stack_ll.pop()
        stack_ll.pop()
        stack_ll.pop()
        stack_ll.pop()
        stack_ll.pop()
        stack_ll.pop()

        self.assertIsNone(stack_ll.peek())
        self.assertEqual(stack_ll.size(), 0)
        self.assertTrue(stack_ll.is_empty())
    def testLLStack(self):
        stack_ll = StackLL()
        self.assertIsNone(stack_ll.peek())
        self.assertEqual(stack_ll.size(),0)
        self.assertTrue(stack_ll.is_empty())

        stack_ll.push("Marcus")
        stack_ll.push(5)
        stack_ll.push(False)
        stack_ll.push(67.128)
        stack_ll.push("Carlos")
        stack_ll.push(StackLLNode)
        stack_ll.push(8)

        self.assertFalse(stack_ll.is_empty())
        self.assertEqual(stack_ll.peek(), 8)
        self.assertEqual(stack_ll.size(), 7)

        stack_ll.pop()
        stack_ll.pop()
        stack_ll.pop()

        self.assertFalse(stack_ll.is_empty())
        self.assertEqual(stack_ll.peek(), 67.128)
        self.assertEqual(stack_ll.size(), 4)

        self.assertEqual(str(stack_ll), "['Marcus', 5, False, 67.128]")

        stack_ll.pop()
        stack_ll.pop()
        stack_ll.pop()
        stack_ll.pop()
        stack_ll.pop()
        stack_ll.pop()

        self.assertIsNone(stack_ll.peek())
        self.assertEqual(stack_ll.size(),0)
        self.assertTrue(stack_ll.is_empty())