コード例 #1
0
def test_LinkedListStack():
    s = Stack(implementation='linked_list')
    s.push(1)
    s.push(2)
    s.push(3)
    assert s.peek.key == 3
    assert str(s) == ("['(1, None)', '(2, None)', '(3, None)']")
    assert s.pop().key == 3
    assert s.pop().key == 2
    assert s.pop().key == 1
    assert s.is_empty is True
    assert raises(IndexError, lambda: s.pop())
    assert str(s) == '[]'
    _s = Stack(implementation='linked_list', items=[1, 2, 3])
    assert str(_s) == "['(1, None)', '(2, None)', '(3, None)']"
    assert len(_s) == 3

    s = Stack(implementation='linked_list', items=['a', None, type, {}])
    assert len(s) == 4
    assert s.size == 4

    peek = s.peek
    assert peek.key == s.pop().key
    assert raises(TypeError,
                  lambda: Stack(implementation='linked_list', items={0, 1}))
コード例 #2
0
 def _post_order(self, node):
     """
     Utility method for computing post-order
     of a binary tree using iterative algorithm.
     """
     visit = []
     tree, size = self.tree.tree, self.tree.size
     s = Stack()
     s.push(node)
     last = OneDimensionalArray(int, size)
     last.fill(False)
     while not s.is_empty:
         node = s.peek
         l, r = tree[node].left, tree[node].right
         cl, cr = l is None or last[l], r is None or last[r]
         if cl and cr:
             s.pop()
             visit.append(tree[node])
             last[node] = True
             continue
         if not cr:
             s.push(r)
         if not cl:
             s.push(l)
     return visit
コード例 #3
0
def test_ArrayStack():
    s = Stack(implementation='array')
    s.push(1)
    s.push(2)
    s.push(3)
    assert s.peek == 3
    assert str(s) == '[1, 2, 3]'
    assert s.pop() == 3
    assert s.pop() == 2
    assert s.pop() == 1
    assert s.is_empty is True
    assert raises(IndexError, lambda: s.pop())
    _s = Stack(items=[1, 2, 3])
    assert str(_s) == '[1, 2, 3]'
    assert len(_s) == 3
コード例 #4
0
def test_Stack():

    s = Stack()
    s.push(1)
    s.push(2)
    s.push(3)
    assert s.peek == 3
    assert str(s) == '[1, 2, 3]'
    assert s.pop() == 3
    assert s.pop() == 2
    assert s.pop() == 1
    assert s.is_empty is True
    assert raises(ValueError, lambda : s.pop())
    _s = Stack(items=[1, 2, 3])
    assert str(_s) == '[1, 2, 3]'
    assert raises(NotImplementedError, lambda: Stack(implementation=''))
コード例 #5
0
def test_Stack():

    s = Stack(maxsize=3, top=0)
    s.push(1)
    s.push(2)
    s.push(3)
    assert s.top == 3
    assert str(s) == '[1, 2, 3]'
    raises(ValueError, lambda: s.push(4))
    assert s.pop() == 3
    assert s.pop() == 2
    assert s.pop() == 1
    assert s.top == 0
    raises(ValueError, lambda: s.pop())
    raises(ValueError, lambda: Stack())
    raises(TypeError, lambda: Stack(maxsize=8, top=3.5))
    raises(ValueError, lambda: Stack(maxsize=5, top=0, items=[1, 2, 3]))
    raises(ValueError,
           lambda: Stack(maxsize=5, top=0, items=OneDimensionalArray(int, 6)))
    raises(NotImplementedError,
           lambda: Stack(implementation='', maxsize=5, top=0))
コード例 #6
0
 def _in_order(self, node):
     """
     Utility method for computing in-order
     of a binary tree using iterative algorithm.
     """
     visit = []
     tree, size = self.tree.tree, self.tree.size
     s = Stack()
     while not s.is_empty or node is not None:
         if node is not None:
             s.push(node)
             node = tree[node].left
         else:
             node = s.pop()
             visit.append(tree[node])
             node = tree[node].right
     return visit
コード例 #7
0
 def _pre_order(self, node):
     """
     Utility method for computing pre-order
     of a binary tree using iterative algorithm.
     """
     visit = []
     tree, size = self.tree.tree, self.tree.size
     s = Stack()
     s.push(node)
     while not s.is_empty:
         node = s.pop()
         visit.append(tree[node])
         if tree[node].right is not None:
             s.push(tree[node].right)
         if tree[node].left is not None:
             s.push(tree[node].left)
     return visit
コード例 #8
0
def test_Stack():

    s = Stack()
    s.push(1)
    s.push(2)
    s.push(3)
    assert s.peek == 3
    assert str(s) == '[1, 2, 3]'
    assert s.pop() == 3
    assert s.pop() == 2
    assert s.pop() == 1
    assert s.is_empty is True
    assert raises(IndexError, lambda : s.pop())
    _s = Stack(items=[1, 2, 3])
    assert str(_s) == '[1, 2, 3]'
    assert len(_s) == 3
    assert raises(NotImplementedError, lambda: Stack(implementation=''))

    s = Stack(implementation='linked_list')
    s.push(1)
    s.push(2)
    s.push(3)
    assert s.peek == 3
    assert str(s) == '[3, 2, 1]'
    assert s.pop().data == 3
    assert s.pop().data == 2
    assert s.pop().data == 1
    assert s.is_empty is True
    assert raises(IndexError, lambda : s.pop())
    _s = Stack(implementation='linked_list',items=[1, 2, 3])
    assert str(_s) == '[3, 2, 1]'
    assert len(_s) == 3

    s = Stack(implementation='linked_list',items=['a',None,type,{}])
    assert len(s) == 4
    assert s.size == 4

    top = s.top
    assert top.data == s.pop().data

    peek = s.peek
    assert peek == s.pop().data
    assert raises(TypeError, lambda: Stack(implementation='linked_list', items={0, 1}))
コード例 #9
0
    def _simple_path(self, key, root):
        """
        Utility funtion to find the simple path between root and node.

        Parameter
        =========

        key: Node.key
            Key of the node to be searched

        Returns
        =======

        path: list
        """

        stack = Stack()
        stack.push(root)
        path = []
        node_idx = -1

        while not stack.is_empty:
            node = stack.pop()
            if self.tree[node].key == key:
                node_idx = node
                break
            if self.tree[node].left:
                stack.push(self.tree[node].left)
            if self.tree[node].right:
                stack.push(self.tree[node].right)

        if node_idx == -1:
            return path

        while node_idx != 0:
            path.append(node_idx)
            node_idx = self.tree[node_idx].parent
        path.append(0)
        path.reverse()

        return path