Skip to content

Commit

Permalink
Merge pull request #15 from warmachine028/v0.3.1
Browse files Browse the repository at this point in the history
Added Fix and High Quality PNGs in Readme.md
  • Loading branch information
warmachine028 authored Apr 6, 2022
2 parents 55132b4 + 0818ac0 commit 2898b51
Show file tree
Hide file tree
Showing 27 changed files with 111 additions and 254 deletions.
194 changes: 19 additions & 175 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
updated: Thursday, 24th February 2022
updated: Wednesday, 6th April 2022

<div align=center>
<a href="https://github.com/warmachine028/datastax">
Expand All @@ -20,25 +20,10 @@

## What's New?

- Added Sum Segment Tree
- Added Min Segment Tree
- Added Huffman Tree
- data encoder
- data decoder
- Huffman Code
- Huffman Table
- compression ratio
- space saved
- Added Red Black Tree - 🗸 TESTED
- Added Splay Tree - 🗸 TESTED
- Added Delete methods in: 🗸 TESTED
- BinaryTree
- BinarySearchTree
- AVLTree
- Enhanced string representation of all LinkedLists
- Added Custom Comparator for PriorityQueue
- Added name-mangler function for items with multiline string representations
- Added HuffmanTable object for storing and visualizing huffman-table
- Added Mandatory Keyword arguments to avoid confusion in:
- Arrays
- ThreadedBinaryTree
- Added High Quality PNGs in README.md

## Table of Contents

Expand Down Expand Up @@ -148,202 +133,61 @@ pip install datastax

- **Queue**

```py
from datastax.arrays import Queue
![queue](./assets/Usage/queue.png)

# Building a Queue Data Structure with fixed capacity
queue = Queue(capacity=5)
# Enqueueing items inside queue
for item in ('item 1', 'item 2'):
queue.enqueue(item)
# Performing Dequeue Operation
queue.dequeue()
queue.enqueue('item 3')
print(queue)
```
```shell
$ OUTPUT:
┌──────────╥──────────┬──────────┐
FRONT -> │ ╳ ║ item 2 │ item 3 │ <- REAR
└──────────╨──────────┴──────────┘
```
![queue_output](./assets/Usage/queue_output.png)

---------------------------------------------------

- **BinaryTree**

```py
from datastax.trees import BinaryTree
![BinaryTree](./assets/Usage/binaryTree.png)

bt = BinaryTree([1, 2, 3, 4, 5])
print(bt)
```
```shell
$ OUTPUT:
1
┌─────┴─────┐
2 3
┌──┴──┐
4 5
```
![BinaryTree_output](./assets/Usage/binaryTree_output.png)

---------------------------------------------------

- **MinHeapTree**

```py
from datastax.trees import MinHeapTree
![MinHeapTree](./assets/Usage/mht.png)

MiHT = MinHeapTree([1, 2, 4, 2, 6, 5, 9, 18, 3, 2])
print(MiHT)
```
```shell
$ OUTPUT
1
┌───────────┴───────────┐
2 4
┌─────┴─────┐ ┌─────┴─────┐
2 2 5 9
┌──┴──┐ ┌──┘
18 3 6
```
![MinHeapTree_output](./assets/Usage/mht_output.png)

---------------------------------------------------

- **ThreadedBinaryTree**

```py
from datastax.trees import ThreadedBinaryTree as Tbt
logic = str("BinaryTree")
tbt = Tbt(['a', 'b', 'c', 'd', 'e'], insertion_logic=logic)
print(tbt)
```
![ThreadedBinaryTree](./assets/Usage/tbt.png)

```shell
$ OUTPUT
┌───┐
┌───────────────────────────> DUMMY │<──────────────┐
│ ┌───┴───┘ │
│ a │
│ ┌───────────────┴───────────────┐ │
│ b │ │ c │
│ ┌───────┴───────┐ │ └───────┴───────┘
│ d │ │ e │
└───┴───┘ └───┴───┘
```
![ThreadedBinaryTree_output](./assets/Usage/tbt_output.png)

---------------------------------------------------

- **SumSegmentTree**

```py
from datastax.trees import SumSegmentTree
sst = SumSegmentTree([1, 3, 5, 7, 9, 11])
print(sst)
print(sst.preorder_print())
```
![SumSegmentTree](./assets/Usage/sst.png)

```shell
$ OUTPUT
36
[0:5]
┌───────────┴───────────┐
9 27
[0:2] [3:5]
┌─────┴─────┐ ┌─────┴─────┐
4 5 16 11
[0:1] [3:4]
┌──┴──┐ ┌──┴──┐
1 3 7 9
36 [0:5]
├─▶ 9 [0:2]
│ ├─▶ 4 [0:1]
│ │ ├─▶ 1
│ │ └─▶ 3
│ └─▶ 5
└─▶ 27 [3:5]
├─▶ 16 [3:4]
│ ├─▶ 7
│ └─▶ 9
└─▶ 11
```
![SumSegmentTree_Output](./assets/Usage/sst_output.png)

---------------------------------------------------

- **HuffmanTree**

```py
from datastax.trees import HuffmanTree
string = str("Espresso Express")
hft = HuffmanTree(string)
print(hft)
```
![HuffmanTree](./assets/Usage/hft.png)

```shell
$ OUTPUT
16
0 │ 1
┌───────────────────────┴───────────────────────┐
7 9
0 │ 1 0 │ 1
┌───────────┴───────────┐ ┌───────────┴───────────┐
3 4 4 s
0 │ 1 0 │ 1 0 │ 1 5
┌─────┴─────┐ ┌─────┴─────┐ ┌─────┴─────┐
x 2 e r E p
1 0 │ 1 2 2 2 2
┌──┴──┐
o
1 1
```
![HuffmanTree_Output](./assets/Usage/hft_output.png)

---------------------------------------------------

- **RedBlackTree**

```py
from datastax.trees import RedBlackTree
![RedBlackTree](./assets/Usage/rbt.png)

rbt = RedBlackTree([500, 236, 565, 105, 842, 497, 312, 612, 80])
rbt.delete(236)
print(rbt)
```
```shell
$ OUTPUT
500
┌─────────────────┴─────────────────┐
105 612
┌────────┴────────┐ ┌────────┴────────┐
80 497 565 842
┌───┘
312
```
![RedBlackTreeOutput](./assets/Usage/rbt_output.png)

## What's Next

- Enhanced Documentation
- Better TestCases for Huffman Tree
- Better TestCases for Segment Trees
- Test Cases for Fibonacci Tree
- Adding of images of trees instead of trees themselves in README
Binary file added assets/Usage/binaryTree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/binaryTree_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/hft.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/hft_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/mht.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/mht_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/queue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/queue_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/rbt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/rbt_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/sst.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/sst_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/tbt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Usage/tbt_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion datastax/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# __init__.py

# Versioning of the datastax package
__version__ = "0.3.0"
__version__ = "0.3.1"
__all__ = ['trees', 'linkedlists', 'arrays']
4 changes: 2 additions & 2 deletions datastax/arrays/priority_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@


class PriorityQueue(Queue):
def __init__(self, capacity: int = None,
def __init__(self, *, capacity: int = None,
custom_comparator: Callable = None):
super().__init__(capacity)
super().__init__(capacity=capacity)
self.comparator = custom_comparator or max

def swap(self, index1: int, index2: int) -> None:
Expand Down
2 changes: 1 addition & 1 deletion datastax/arrays/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class Queue:
def __init__(self, capacity: int = None):
def __init__(self, *, capacity: int = None):
self._capacity = capacity if capacity is not None else math.inf
self._array: list[Any] = []
self._front = self._rear = 0
Expand Down
2 changes: 1 addition & 1 deletion datastax/arrays/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class Stack:
def __init__(self, capacity: int = None):
def __init__(self, *, capacity: int = None):
self.capacity = capacity if capacity is not None else math.inf
self._array: list[Any] = []

Expand Down
4 changes: 2 additions & 2 deletions datastax/trees/expression_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _construct(self, infix_expression: Union[list, str] = None
infix_expression = [*filter(lambda x: x is not None, infix_expression)]
self.infix_expression = ''.join(map(str, infix_expression))
self.postfix_expression = self.infix_to_postfix()
stack = Stack(len(infix_expression))
stack = Stack(capacity=len(infix_expression))
for item in self.postfix_expression.split():
if self.is_operator(item):
try:
Expand Down Expand Up @@ -63,7 +63,7 @@ def infix_to_postfix(self, infix_expression=''):
raise UnmatchedBracketPairError(self, infix_expression)

postfix_expression: str = ''
stack = Stack(len(infix_expression))
stack = Stack(capacity=len(infix_expression))
infix_expression += ')'
stack.push('(')
current_count = 0
Expand Down
6 changes: 5 additions & 1 deletion datastax/trees/huffman_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def comparator(n1: HuffmanNode, n2: HuffmanNode) -> HuffmanNode:
_data, None, None, frequency
) for _data, frequency in Counter(data).items()
)
p_queue = PriorityQueue(None, comparator)
p_queue = PriorityQueue(capacity=None, custom_comparator=comparator)
for node in nodes:
p_queue.enqueue(node)

Expand Down Expand Up @@ -82,6 +82,10 @@ def _calculate_huffman_code(self):
pass

def size_calculator(self) -> Optional[tuple[int, int]]:
"""
Calculates the actual encoding size and total
huffman encoding size with table included
"""
if not self.root or not self.huffman_table:
return None
fixed_encoding = huffman_encoding = 0
Expand Down
5 changes: 3 additions & 2 deletions datastax/trees/private_trees/threaded_binary_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ def preorder_print(self) -> str:


class ThreadedBinaryTree(binary_tree.BinaryTree):
def __init__(self, array=None, insertion_logic: str = None,
root: ThreadedNode = None):
def __init__(self, array=None, root: ThreadedNode = None, *,
insertion_logic: str = None
):
self._root: Optional[ThreadedNode] = root
self.head = self.tail = self.root
self.dummy_node = ThreadedNode(None, self.root)
Expand Down
Loading

0 comments on commit 2898b51

Please sign in to comment.