Exemplo n.º 1
0
    def test_operations(self):
        stack = Stack()

        stack.push(1)
        stack.push(2)
        stack.push(3)

        self.assertEqual(3, stack.pop())
        self.assertEqual(2, stack.pop())
        self.assertEqual(1, stack.pop())
 def test_head(self):
     stack = Stack()
     stack.push('element 1')
     stack.push('element 2')
     output = stack.pop()
     self.assertEqual(output, 'element 2')
     self.assertEqual(stack.peek(), 'element 1')
Exemplo n.º 3
0
class MinStack():
    def __init__(self):
        self.items = Stack()
        self.mins = Stack()

    def push(self, elem: Node):
        self.items.push(elem)
        if self.mins.head is None or elem.value < self.mins.head.value:
            self.mins.push(Node(elem.value))
        else:
            self.mins.push(Node(self.mins.head.value))

    def pop(self):
        self.mins.pop()
        return self.items.pop()

    def min(self):
        return self.mins.head.value
Exemplo n.º 4
0
class NodeInorderIterator(object):
    def __init__(self, node):
        self._node = node
        self._accumulator = Stack()

    def __iter__(self):
        return self

    def __next__(self):
        if self._accumulator.isEmpty() and not self._node:
            raise StopIteration()

        if self._node:
            current = self._node
            while current:
                self._accumulator.push(current)
                current = current.left

        node = self._accumulator.pop()
        self._node = node.right

        return node
    def _balance(self, root):

        # balancer will always produce a new balanced set
        balancedSet = PersistentBalancedSet(iterator=self._iterator)
        rightStack = Stack()

        # last default values for first iteration
        lastBalanceFactor = 0
        lastNode = None
        lastParent = None

        # current default values for first iteration
        parent = None  # for root parent will be None
        node = root.__root
        isUnbalanced = False

        while node:
            balanceFactor = getBalanceFactor(node)
            if balanceFactor < -1 or balanceFactor > 1:
                isUnbalanced = True
            if isUnbalanced and balanceFactor in (1, 0, -1):
                if lastBalanceFactor < -1:
                    self._rightRotation(lastNode, lastParent, balancedSet)
                else:
                    self._leftRotation(lastNode, lastParent, balancedSet)
                return balancedSet

            if not balancedSet.__root:
                node = copy(node)
                object.__setattr__(balancedSet, '_PersistentBalancedSet__root', node)

            # WARNING: change order of execution of below section on your own risk.
            if node.right:
                rnode = copy(node.right)
                node.right = rnode
                rightStack.push((rnode, node,))  # a tuple of right node and its parent, preserving right elements

            if node.left:
                lnode = copy(node.left)
                node.left = lnode

            # useful for next iteration
            lastNode = node
            lastParent = parent
            lastBalanceFactor = balanceFactor

            # this is to set next node in iteration as per preorder traversal rules
            if node.left:
                parent = node
                node = node.left
            elif not rightStack.isEmpty():
                element = rightStack.pop()
                node = element[0]
                parent = element[1]
            else:
                node = None

        return balancedSet
Exemplo n.º 6
0
def is_balanced(word):
    s = Stack()
    for c in word:
        if c == '(':
            s.push(Node(c))
        elif c == ')':
            if s.head is not None:
                s.pop()
            else:
                return False
    return s.head is None
Exemplo n.º 7
0
class BinaryTreePreorderIterator(object):
    def __init__(self, node):
        self._node = node
        self._accumulator = Stack()

    def __iter__(self):
        return self

    def __next__(self):
        if self._node:
            if self._node.right:
                self._accumulator.push(self._node.right)
            node = self._node
            self._node = self._node.left
        else:
            if self._accumulator.isEmpty() and not self._node:
                raise StopIteration()
            node = self._accumulator.pop()
            if node.right:
                self._accumulator.push(node.right)
            self._node = node.left

        return node.value
Exemplo n.º 8
0
 def __init__(self):
     self.items = Stack()
     self.mins = Stack()
Exemplo n.º 9
0
 def test_constructor(self):
     stack = Stack()
     self.assertIsNotNone(stack)
Exemplo n.º 10
0
 def test_tail(self):
     stack = Stack()
     stack.push('element 1')
     stack.push('element 2')
     self.assertEqual(stack.peek(), 'element 2')
Exemplo n.º 11
0
 def __init__(self, node):
     self._node = node
     self._accumulator = Stack()