Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
04fa77e
Done with binheap
Oct 31, 2017
3f009ca
reworked push and working on pop, also wrote some tests
markreynoso Nov 2, 2017
1d1137a
completed readme, added setup, added tox, fixed push and pop, finishe…
markreynoso Nov 2, 2017
d36c245
initial setup
Nov 3, 2017
d98d517
Merge branch 'priorityq' of https://github.com/chaitanyanarukulla/dat…
markreynoso Nov 3, 2017
9380413
untested priority heap written. works great in theory.
markreynoso Nov 3, 2017
0881f6c
fixed insert and pop methods and wrote a few tests, all passing
markreynoso Nov 3, 2017
f094873
Test written and passing all test
Nov 5, 2017
ac42137
created graph
markreynoso Nov 5, 2017
d75d601
git issue
Nov 5, 2017
03b0444
Writing test
Nov 6, 2017
2c6e481
test are passing
Nov 6, 2017
bc22f46
tests passing and cleaned up code
markreynoso Nov 6, 2017
b244da2
depth first tested and finished
markreynoso Nov 7, 2017
69d06da
implemented breadth_first_traversal and depth_first_traversal functio…
Nov 7, 2017
9b64dc2
tested for cyclic graphs
Nov 7, 2017
a73a4b1
converted graph to dictionaries and added weight to edges
markreynoso Nov 8, 2017
12f72b6
refactored tests for python 2
markreynoso Nov 8, 2017
67c69e5
added testing for weights of edges
markreynoso Nov 9, 2017
0700ebc
updated readme
markreynoso Nov 9, 2017
b9c8b6f
added if name=main and adjusted for new dictionary setup
markreynoso Nov 9, 2017
efa6675
updated graph
markreynoso Dec 6, 2017
e169335
fixed tests
markreynoso Dec 6, 2017
82a87c2
cleaned up sloppy spots
markreynoso Dec 8, 2017
4afd744
updating errors
markreynoso Dec 10, 2017
fb86f54
fixed adjacent
markreynoso Dec 13, 2017
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
226 changes: 224 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,224 @@
# data-structures
Data-Structures
# Data-Structures

Where a variety of data-structures found in python are being explored, such as:
* A simple class LinkedList for building linked lists as well as a subclass Stack to implement a stack.

## Authors

ChaiChaitanya Narukulla and Mark Reynoso created this code base as a project for Python 401 at Code Fellows Seattle.

### Requirements for testing and development

In order to create a development and testing environment, you will need the following:

```
python2, python3, ipython, pytest, pytest-watch, pytest-cov, tox
```

```
To install environment:

pip install -e .[testing][development]
```

### Linkded List Class Usage

```
To create an instance of a the LinkedList() class contained in package, from python:

new = LinkedList() *you may choose and optional parameter of a single value or an iterable of one of the following: a tuple, a list, or a string.*

LinkedList() contains the following methods:
* _push(val) (O1)_ - will insert the value ‘val’ at the head of the list.
* _pop() (O1)_ - will pop the first value off the head of the list and return it. Raises an exception with an appropriate message if there are no values to return.
* _size() (01)_ - will return the length of the list.
* _search(val) (On)_ - will return the node containing ‘val’ in the list, if present, else None.
* _remove(node) (On)_ - will remove the given node from the list, wherever it might be (node must be an item in the list). If the node is not in the list, it will raise an exception with an appropriate message.
* _display() (O1)_ - will return a unicode string representing the list as if it were a Python tuple literal: “(12, ‘sam’, 37, ‘tango’)”

To access any contained methods:
new.push(val)
new.pop()
new.size()
new.search(val)
new.remove(node)
new.display()

Additionally, LinkedList() will interact with these built-in Python functions:

* _len(new)_ - returns the size of the list.
* _print(new)_ - returns what the display() method returns.

```

### Stack Class Usage

```
To create an instance of a Stack() class contained in package, from python:

new = Stack() *you may choose and optional parameter of a single value or an iterable of one of the following: a tuple, a list, or a string.*

Stack() contains the following methods:
* _push(val) (O1)_ - will insert the value ‘val’ at the head of the stack.
* _pop() (O1)_ - will pop the first value off the head of the stack and return it. Raises an exception with an appropriate message if there are no values to return.

To access any contained methods:
new.push(val)
new.pop()

Additionally, Stack() will interact with these built-in Python functions:

* _len(new)_ - returns the size of the list.

```

### Doubly Linked List Class Usage

```
To create an instance of a Dll() class contained in package, from python:

new = Dll() *you may not call Dll() with any parameters.*

Dll() contains the following methods:
* _push(val) (O1)_ - will insert the value ‘val’ at the head of the list.
* _append(val) (O1)_ - will insert the value ‘val’ at the tail of the list.
* _pop() (O1)_ - will pop the first value off the head of the stack and return it. Raises an exception with an appropriate message if there are no values to return.
* _shift() (O1)_ - will remove the last value off the tail of the list and return it. Raises an exception with an appropriate message if there are no values to return.
* _remove(node) (On)_ - will remove the given node from the list, wherever it might be (node must be an item in the list). If the node is not in the list, it will raise an exception with an appropriate message.

To access any contained methods:
new.push(val)
new.append(val)
new.pop()
new.shift()
new.remove()

Additionally, Dll() will interact with these built-in Python functions:

* _len(new)_ - returns the size of the list.

```

### Queue Class Usage

```
To create an instance of a Queue() class contained in package, from python:

new = Queue() *you may not call Queue() with any parameters.*

Queue() contains the following methods:
* _enqueue(val) (O1)_ - will insert the value ‘val’ at the end of the queue.
* _dequeue() (O1)_ - will remove the last value off the front of the queue and return it. Raises an exception with an appropriate message if there are no values to return.
* _peek() (O1)_ - shows the value of the node at the end of the queue.
* _size() (O1)_ - displays the number of node in the queue.

To access any contained methods:
new.enqueue(val)
new.dequeue()
new.peek()
new.size()

Additionally, Queue will interact with these built-in Python functions:

* _len(new)_ - returns the size of the list.

```

### Deque Class Usage

```
To create an instance of a Deque() class contained in package, from python:

new = Deque() *you may not call Deque() with any parameters.*

Deque() contains the following methods:
* _append(val) (O1)_ - will insert the value ‘val’ at the end of the deque.
* _appendleft() (O1)_ - will insert a value to the front of the deque.
*_pop() (01)_ - removes the first val from the end of the deque.
*_popleft() (01)_ - removes the first val from the front of the deque.
* _peek() (O1)_ - shows the value of the item at the end of the deque.
* _peekleft() (O1)_ - shows the value of the item at the font of the deque.
* _size() (O1)_ - displays the number items in the deque.

To access any contained methods:
new.append(val)
new.appendleft(val)
new.pop()
new.popleft()
new.peek()
new.peekleft()
new.size()

```

### Binaary Heap (Binheap) Class Usage

```
To create an instance of a Binheap() class contained in package, from python:

new = Binheap() *you may call Binheap() with any uniqe number or iterable of unique numbers.*

Binheap() contains the following methods:
* _push(val) (Olog(n))_ - will insert the value ‘val’ at the end of the heap.
*_pop() (0log(n))_ - removes the first val from the end of the heap.

To access any contained methods:
new.push(val)
new.pop()

```

### Priorityq (Priorityq) Class Usage

```
To create an instance of a Priorityq() class contained in package, from python:

new = Priorityq() *you may call Priorityq() with any uniqe number or iterable of unique numbers.*

Priorityq() contains the following methods:
*_insert(val,priority) (O1)_ - will insert the value ‘val’ at the end of the heap.
*_pop() (01)_ - removes the higest priority and returns the higest priority.
*_peek() (01)_ - returns the higest priority.

To access any contained methods:
new.insert(val)
new.pop()
new.peek()

```

### Graph Class (weighted) Usage

```
To create an instance of a Graph() class contained in package, from python:

new = Graph() *you may not call Graph() with any parameters.*

Graph() contains the following methods:
* _nodes() (O1)_ - will return a list of all nodes in the graph.
* _edges() (On2)_ - will return a dictionary of edges in graph with weights.
*_add_node(val) (01)_ - adds a new node to the graph.
*_add_edge(val1, val2, weight) (0n)_ - add a new edge to nodes in the graph.
* _del_node(val) (On2)_ - delete node from graph.
* _del_edge(val1, val2) (On)_ - delete edge between two nodes.
* _has_node(val) (O1)_ - returns true if node exists and false if not.
* _neighbors(val) (On2)_ - return a list of nodes with edges to input node.
* _adjacent(val1, val2) (On)_ - return true if edge exists between two inputs,
otherwise false.
* _depth_first_traversal(start_val) (Ologn)_ - will return full visited path of traversal completed.
* _breadth_first_traversal(start_val)(Ologn)_ - will return full visited path of traversal completed.

To access any contained methods:
new.nodes()
new.edges()
new.add_node(val)
new.add_edge(val1, val2, weight)
new.del_node(val)
new.del_edge(val1, val2)
new.has_node(val)
new.neighbors(val)
new.adjacent(val1, val2)
new.depth_first_traversal(start_val)
new.breadth_first_traversal(start_val)

```
16 changes: 16 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from setuptools import setup

setup(
name='data-structures',
description=('A package for building and '
'running the data-structures module'),
package_dir={'': 'src'},
author='Mark and chai',
author_email='chaitanyanarukulla@gmail.com mreynoso@spu.edu',
py_modules=['binheap'],
install_requires=[],
extras_require={
'testing': ['pytest', 'pytest-cov', 'pytest-watch', 'tox'],
'development': ['ipython']
},
)
80 changes: 80 additions & 0 deletions src/binheap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""Binary heap class."""


class Binheap(object):
"""Iniitialize the class node."""

def __init__(self, iterable=None):
"""Create a new min heap."""
self.heaplist = []
self._size = 0
if isinstance(iterable, (list, tuple)):
for item in iterable:
self.push(item)

def push(self, val):
"""Push a value to the end of heap and sort up."""
if type(val) == int or type(val) == float:
self.heaplist.append(val)
self._size += 1
sort_up = True
idx = self._size - 1
while sort_up:
if idx > 0:
if idx % 2 == 0:
parent = (idx - 2) // 2
else:
parent = (idx - 1) // 2
if val < self.heaplist[parent]:
self.heaplist[idx] = self.heaplist[parent]
self.heaplist[parent] = val
idx = parent
else:
sort_up = False
else:
sort_up = False
else:
raise ValueError('You must input numbers only.')

def pop(self):
"""Remove first value in heap and sort down."""
if self._size == 0:
raise IndexError('There are no values to pop.')
elif self._size == 1:
self._size -= 1
return self.heaplist.pop()
elif self._size > 1:
pop_val = self.heaplist[0]
self.heaplist[0] = self.heaplist.pop()
self._size -= 1
idx = 0
sort_down = True
while sort_down:
l_child = idx * 2 + 1
r_child = idx * 2 + 2
if r_child <= self._size - 1 and l_child <= self._size - 1:
if self.heaplist[l_child] > self.heaplist[r_child]:
if self.heaplist[r_child] < self.heaplist[idx]:
self.heaplist[r_child], self.heaplist[idx] =\
self.heaplist[idx], self.heaplist[r_child]
idx = r_child
else:
sort_down = False
else:
if self.heaplist[l_child] < self.heaplist[r_child]:
if self.heaplist[l_child] < self.heaplist[idx]:
self.heaplist[idx], self.heaplist[l_child] =\
self.heaplist[l_child], self.heaplist[idx]
idx = l_child
else:
sort_down = False
elif l_child == self._size - 1:
if self.heaplist[l_child] < self.heaplist[idx]:
self.heaplist[idx], self.heaplist[l_child] =\
self.heaplist[l_child], self.heaplist[idx]
sort_down = False
else:
sort_down = False
else:
sort_down = False
return pop_val
23 changes: 23 additions & 0 deletions src/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Fixutres for test_binheap.py."""
import pytest


@pytest.fixture
def ebh():
"""Initialize an empty binary heap."""
from binheap import Binheap
return Binheap()


@pytest.fixture
def pq():
"""Initialize a empty pq."""
from priorityq import Priorityq
return Priorityq()


@pytest.fixture
def g():
"""Initialize a empty pq."""
from graph import Graph
return Graph()
Loading