Skip to main content

Python Data Structures

For all of your data structure needs

About

A Python package that contains common data structures. Data structures within this package also contain associated search and sorting algorithms. The intention of this package is mostly a learning endeavor but, it may also be used for various build purposes. I hope you find it easy to understand and interact with. If you have any questions or comments, please feel free to reach out.

Getting Started

Further information regarding this package can be found on GitHub: python-data-structures.

Installation

To get started, install the package:

pip install pydatastructs

Then, import it into your project:

from pydatastructs import (
    Stack,
    Queue,
    Tree,
    BinarySearchTree,
    LinkedList,
    MaxHeap,
    MinHeap
    )

Data Structures

Stack

A list or array based data structure with last-in-first-out (LIFO) properties.

Methods

  • get()
  • length()
  • is_empty()
  • push()
  • pop()
  • merge_sort()

Usage

from pydatastructs import Stack

my_stack = Stack(collection=[5, 3, 1, 4, 2])
length = my_stack.length()

if length < 6:
    my_stack.push(0)

my_stack.merge_sort() # [0, 1, 2, 3, 4, 5]

Queue

A list or array based data structure with first-in-first-out (FIFO) properties.

Methods

  • get()
  • length()
  • is_empty()
  • enqueue()
  • dequeue()
  • merge_sort()

Usage

from pydatastructs import Queue

my_queue = Queue(collection=[5, 3, 1, 4, 2])
queue_is_empty = my_queue.is_empty

if not queue_is_empty:
    print(my_queue.dequeue())

my_queue.merge_sort() # [1, 3, 4, 5]

Tree

A node based data structure where each node contains a value property and a children property. The children property is a collection of child nodes. Finally, each node itself is a tree or sub-tree.

Methods

  • add()
  • contains()
  • depth_first_traversal()
  • breadth_first_traversal()

Usage

from pydatastructs import Tree

my_tree = Tree(value=1)
my_tree.add(2)
my_tree.add(3)
my_tree.children[0].add(4)
my_tree.children[0].add(5)
my_tree.children[1].add(6)
my_tree.children[1].add(7)

result = []
def add_to_result(node: Tree):
    result.append(node.value)

my_tree.depth_first_traversal(add_to_result)
print(result) # [1, 2, 4, 5, 3, 6, 7]
              # my_tree: 
              #            1
              #        /      \
              #      2         3
              #   /    \     /   \
              #  4      5   6     7

Binary Search Tree

A node based data structure where each node contains a value property and, a left and right property. The left and right properties point to potential child nodes. The left node's value will always be less than the parent node's value. The right node's value will always be greater than the parent node's value. Finally, each node itself is a tree or sub-tree.

Methods

  • insert()
  • contains()
  • depth_first_traversal()
  • breadth_first_traversal()

Usage

from pydatastructs import BinarySearchTree

my_binarysearchtree = BinarySearchTree(value=10)
values = [6, 14, 4, 12, 8, 16]

for val in values:
    my_binarysearchtree.insert(val)

result = []
def add_to_result(node: BinarySearchTree):
    result.append(node.value)

my_binarysearchtree.breadth_first_traversal(add_to_result)
print(result) # [10, 6, 14, 4, 8, 12, 16]
              # my_binarysearchtree: 
              #            10
              #        /      \
              #      6         14
              #   /    \     /   \
              #  4      8   12     16

Linked List

A node based data structure containing a head and tail property. The head points to the root node and, the tail points to the last node in the linked list. Each node has a value property and a next property, which points to the next node in the linked list.

Methods

  • append()
  • remove_head()
  • find_node()

Usage

from pydatastructs import LinkedList

my_linkedlist = LinkedList(value=1)
values = [2, 3, 4, 5]

for val in values:
    my_linkedlist.append(val)

node = my_linkedlist.find_node(3)

print(node.next.value) # 4

Max Heap

A complete binary tree data structure represented as an array where, every parent node's value is greater than or equal to their child node's values.

Methods

  • get()
  • insert()
  • remove_max()

Usage

from pydatastructs import MaxHeap

my_maxheap = MaxHeap()
values = [1, 2, 3, 4, 5, 6, 7]

for val in values:
    my_maxheap.insert(val)

my_maxheap.get() # [7, 4, 6, 1, 3, 2, 5]
                 # my_maxheap: 
                 #            7
                 #        /      \
                 #      4         6
                 #   /    \     /   \
                 #  1      3   2     5

Min Heap

A complete binary tree data structure represented as an array where, every parent node's value is less than or equal to their child node's values.

Methods

  • get()
  • insert()
  • remove_min()

Usage

from pydatastructs import MinHeap

my_minheap = MinHeap()
values = [7, 6, 5, 4, 3, 2, 1]

for val in values:
    my_minheap.insert(val)

my_minheap.get() # [1, 4, 2, 7, 5, 6, 3]
                 # my_minheap: 
                 #            1
                 #        /      \
                 #      4         2
                 #   /    \     /   \
                 #  7      5   6     3

Contact

For support, feedback or, to report a bug, you may contact the maintainer:

License

Distributed under the MIT License.

Release files for pydatastructs 1.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydatastructs 1.2.3
File Size Uploaded
pydatastructs-1.2.3.tar.gz 13.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydatastructs 1.2.3
File Interpreter ABI Platform
pydatastructs-1.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 31.1 kB

Release files / pydatastructs-1.2.3.tar.gz

Download URL pydatastructs-1.2.3.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ca9c1b2f77e5b6fb644cb1bca6cade126f24e37e3e2657f69a7cc17b5e995d83
BLAKE2b-256 checksum
How to use checksums
5f4cf0c6574f85b0f9e35b6d439d7834047a557f599e8f344275a04eeb30e20d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

Release files / pydatastructs-1.2.3-py3-none-any.whl

Download URL pydatastructs-1.2.3-py3-none-any.whl
Size 17.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c3706f7244b3f7ed2484da92e79a1ce691822cfe782ceeae3d3fcfbebcf540a
BLAKE2b-256 checksum
How to use checksums
b32ef3f3808bcef20c33582216ffa90a4fadd61e21bc3523fba63159551560aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

Release history Release notifications | RSS feed

This release

1.2.3 This release

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page