Skip to content

Using DynamicOneDimensionalArray in Arraystack #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 20 additions & 38 deletions pydatastructs/miscellaneous_data_structures/stack.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydatastructs.linear_data_structures import OneDimensionalArray
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray
from copy import deepcopy as dc

__all__ = [
@@ -19,13 +19,7 @@ class Stack(object):
By default, 'array'
Currently only supports 'array'
implementation.
maxsize : int
The maximum size of the stack.
For array implementation.
top : int
The top element of the stack.
For array implementation.
items : OneDimensionalArray
items : DynamicOneDimensionalArray
Optional, by default, None
The inital items in the stack.
For array implementation.
@@ -39,12 +33,12 @@ class Stack(object):
=======

>>> from pydatastructs import Stack
>>> s = Stack(maxsize=5, top=0)
>>> s = Stack()
>>> s.push(1)
>>> s.push(2)
>>> s.push(3)
>>> str(s)
'[1, 2, 3, None, None]'
'[1, 2, 3]'
>>> s.pop()
3

@@ -57,8 +51,6 @@ class Stack(object):
def __new__(cls, implementation='array', **kwargs):
if implementation == 'array':
return ArrayStack(
kwargs.get('maxsize', None),
kwargs.get('top', 0),
kwargs.get('items', None),
kwargs.get('dtype', int))
raise NotImplementedError(
@@ -82,46 +74,36 @@ def peek(self):

class ArrayStack(Stack):

__slots__ = ['maxsize', 'top', 'items', 'dtype']
__slots__ = ['items', 'dtype']

def __new__(cls, maxsize=None, top=0, items=None, dtype=int):
if not _check_type(maxsize, int):
raise ValueError("maxsize is missing.")
if not _check_type(top, int):
raise TypeError("top is not of type int.")
def __new__(cls, items=None, dtype=int):
if items is None:
items = OneDimensionalArray(dtype, maxsize)
if not _check_type(items, OneDimensionalArray):
raise ValueError("items is not of type, OneDimensionalArray")
if items._size > maxsize:
raise ValueError("Overflow, size of items %s is greater "
"than maxsize, %s"%(items._size, maxsize))
items = DynamicOneDimensionalArray(dtype, 0)
else:
items = DynamicOneDimensionalArray(dtype, items)
obj = object.__new__(cls)
obj.maxsize, obj.top, obj.items, obj.dtype = \
maxsize, top, items, items._dtype
obj.items, obj.dtype = \
items, items._dtype
return obj

def push(self, x):
if self.top == self.maxsize:
raise ValueError("Stack is full.")
self.items[self.top] = self.dtype(x)
self.top += 1
self.items.append(x)

def pop(self):
if self.top == 0:
raise ValueError("Stack is already empty.")
self.top -= 1
r = self.items[self.top]
self.items[self.top] = None
return r
if self.is_empty:
raise ValueError("Stack is empty")

top_element = dc(self.items[self.items._last_pos_filled])
self.items.delete(self.items._last_pos_filled)
return top_element

@property
def is_empty(self):
return self.top == 0
return self.items._last_pos_filled == -1

@property
def peek(self):
return self.items[self.top - 1]
return self.items[self.items._last_pos_filled]

def __str__(self):
"""
19 changes: 7 additions & 12 deletions pydatastructs/miscellaneous_data_structures/tests/test_stack.py
Original file line number Diff line number Diff line change
@@ -4,22 +4,17 @@

def test_Stack():

s = Stack(maxsize=3, top=0)
s = Stack()
s.push(1)
s.push(2)
s.push(3)
assert s.top == 3
assert s.peek == 3
assert str(s) == '[1, 2, 3]'
assert raises(ValueError, lambda: s.push(4))
assert s.pop() == 3
assert s.pop() == 2
assert s.pop() == 1
assert s.top == 0
assert raises(ValueError, lambda: s.pop())
assert raises(ValueError, lambda: Stack())
assert raises(TypeError, lambda: Stack(maxsize=8, top=3.5))
assert raises(ValueError, lambda: Stack(maxsize=5, top=0, items=[1, 2, 3]))
assert raises(ValueError, lambda: Stack(maxsize=5, top=0,
items=OneDimensionalArray(int, 6)))
assert raises(NotImplementedError, lambda: Stack(implementation='',
maxsize=5, top=0))
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=''))
6 changes: 3 additions & 3 deletions pydatastructs/trees/binary_trees.py
Original file line number Diff line number Diff line change
@@ -653,7 +653,7 @@ def _pre_order(self, node):
"""
visit = []
tree, size = self.tree.tree, self.tree.size
s = Stack(maxsize=size)
s = Stack()
s.push(node)
while not s.is_empty:
node = s.pop()
@@ -671,7 +671,7 @@ def _in_order(self, node):
"""
visit = []
tree, size = self.tree.tree, self.tree.size
s = Stack(maxsize=size)
s = Stack()
while not s.is_empty or node is not None:
if node is not None:
s.push(node)
@@ -689,7 +689,7 @@ def _post_order(self, node):
"""
visit = []
tree, size = self.tree.tree, self.tree.size
s = Stack(maxsize=size)
s = Stack()
s.push(node)
last = OneDimensionalArray(int, size)
last.fill(False)