Skip to main content

A Python package that implements common data structures such as Array, Stack, Queue, Linked List, Binary Tree, and Graph.

Project description

Data Structure Package

This package provides implementations of several common data structures and their basic operations. The package includes:

  • Array
  • Stack
  • Queue
  • Linked List
  • Doubly Linked List
  • Binary Tree
  • Graph

Each data structure is implemented with a set of core methods, providing users with the ability to manipulate and interact with the data structures in a direct way. Below is an overview of each data structure and the operations available.

Table of Contents

  1. Array Class
  2. Stack Class
  3. Queue Class
  4. Linked List Class
  5. Doubly Linked List Class
  6. Binary Tree Class
  7. Graph Class
  8. Installation
  9. Usage

Array Class

The Array class provides basic operations on dynamic arrays, allowing for efficient management and manipulation of array data.

Supported Operations:

  • length() - Returns the number of elements in the array.
  • append(item) - Adds an item to the end of the array.
  • insert(index, item) - Inserts an item at a specific index.
  • remove(item) - Removes the first occurrence of the item.
  • contains(item) - Checks if an item exists in the array.
  • clear() - Clears all elements in the array.
  • is_equal(other_array) - Compares if two arrays are equal.
  • concatenate(other_array) - Concatenates another array to the current one.
  • pop() - Removes the last item from the array by default else you can specify index.
  • delete(index) - Removes an item at a specific index.

Example:

arr = Array()
arr.append(10)
arr.append(20)
arr.insert(1, 15)
print(arr.length())  # Output: 3
arr.remove(15)
print(arr.contains(10))  # Output: True

Stack Class

The Stack class provides a LIFO (Last In First Out) data structure to push and pop items.

Supported Operations:

  • push(item) - Adds an item to the top of the stack.
  • pop() - Removes and returns the top item.
  • top() - Returns the top item without removing it.
  • size() - Returns the number of items in the stack.
  • is_empty() - Checks if the stack is empty.
  • display_stack() - Displays the elements in the stack.

Example:

s = Stack()
s.push(10)
s.push(20)
s.push(30)
s.push(40)
s.display_stack() #[10, 20, 30, 40]
print(s.top())    # Output: 40
s.pop()           # Output: 40
print(s.size())   # Output: 3
s.display_stack() #[10, 20, 30]

Queue Class

The Queue class provides a FIFO (First In First Out) data structure with operations to add, remove, and inspect items in the queue.

Supported Operations:

  • enqueue(item) - Adds an item to the rear/end of the queue.
  • dequeue() - Removes and returns the front item.
  • peek() / front()` - Returns the front item without removing it.
  • rear() - Returns the rear/end item.
  • is_full() - Checks if the queue is full.
  • is_empty() - Checks if the queue is empty.
  • display_queue() - Displays the elements in the queue.

Example:

q = Queue()
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)
q.enqueue(40) 
q.display_queue() #[10, 20, 30, 40]
print(q.rear())  # Output: 40
q.dequeue()
q.display_queue() #[20, 30, 40]
print(q.front())  # Output: 20

Linked List Class

The LinkedList class implements a single linked list with methods to add, remove, and access elements in the list.

Supported Operations:

  • insert_start(item) - Inserts an item at the beginning of the list.
  • insert_after(item, index) - Inserts an item after a specific node.
  • insert_end(item) - Inserts an item at the end of the list.
  • delete_item(index) - Deletes the item at specific index.
  • display() / traverse() - Displays the entire list.
  • search(item) - Searches for an item in the list and returns True if it exist else False.
  • get_length() - Returns the number of nodes in the list.
  • access(index) - Accesses the node at a specific index.
  • update(index, item) - Updates the node at a specific index with a new item.

Example:

ll = LinkedList()
ll.insert_start(10)
ll.insert_end(20)
ll.insert_after(15,0)
ll.display()  # Output: 10 -> 15 -> 20 -> None

Doubly Linked List Class

The DoublyLinkedList class implements a doubly linked list with methods to add, remove, and access elements in the list. Each node here has references to both the previous and next nodes.

Supported Operations:

  • insert_at_beginning(item) - Inserts an item at the beginning of the list.
  • insert_at(index, item) - Inserts an item at a specific index.
  • insert_at_end(item) - Inserts an item at the end of the list.
  • delete_item(index) - Deletes the item at a specific index.
  • display() - Displays the entire list from start to end.
  • search(item) - Searches for an item in the list and returns True if it exists, False otherwise.
  • get_length() - Returns the number of nodes in the list.
  • access(index) - Accesses the node at a specific index and returns its data.
  • update(index, item) - Updates the node at a specific index with a new item.

Example:

dll = DoublyLinkedList()
dll.insert_at_beginning(10)
dll.insert_at_end(20)
dll.insert_at(1, 15)
dll.display()  # Output: 10 <--> 15 <--> 20 <--> None

Binary Tree Class

The BinaryTree class implements a binary tree, offering methods for traversal and modification of the tree.

Supported Operations:

  • insert(item) - Inserts an item into the tree.
  • search(item) - Searches for an item in the tree returns True if exists else False.
  • delete(item) - Deletes an item from the tree.
  • in_order() - Traverses the tree in-order (left, root, right).
  • pre_order() - Traverses the tree pre-order (root, left, right).
  • post_order() - Traverses the tree post-order (left, right, root).

Example:

r"""
      3
    /   \
   1     4
    \
     2
     """
bt = BinaryTree()
bt.insert("3")
bt.insert("1")
bt.insert("2")
bt.insert("4")
bt.in_order()  # Output: 1 -> 2 -> 3 -> 4 (left root right)
bt.pre_order()  # Output: 3 -> 1 -> 2 -> 4 (root left right)
bt.post_order()  # Output: 2 -> 1 -> 4 -> 3 (left right root)

Graph Class

The Graph class implements an undirected graph using an adjacency list. This class includes various methods for adding and removing vertices and edges, as well as performing DFS and BFS traversals.

Supported Operations:

  • add_vertex(vertex) - Adds a vertex to the graph.
  • add_edge(vertex1, vertex2) - Adds an undirected edge between two vertices.
  • remove_vertex(vertex) - Removes a vertex and all edges associated with it.
  • remove_edge(vertex1, vertex2) - Removes the edge between two vertices.
  • has_edge(vertex1, vertex2) - Checks if an edge exists between two vertices.
  • dfs(start_vertex, visited) - Performs Depth-First Search from a starting vertex.
  • bfs(start_vertex) - Performs Breadth-First Search starting from a given vertex.
  • find_path(start, end, path) - Finds a path from start to end using DFS.
  • search(vertex) - Searches for a vertex in the graph. Returns True if found, else False.
  • display_graph() - Displays the adjacency list of the graph.

Example:

g = Graph()
g.add_vertex("A")
g.add_vertex("B")
g.add_vertex("C")
g.add_edge("A", "B")
g.add_edge("B", "C")
g.display_graph()
# Output:
# A: ['B']
# B: ['A', 'C']
# C: ['B']

g.dfs("A", visited=set())  # Output: A B C
g.bfs("A")  # Output: A B C
g.find_path("A", "C", path=[])  # Output: A -> B -> C

Installation

To install the package directly from PyPI using:

pip install ek-data-structures

Usage

After installation, you can import the data structures into your Python code:

from ek_data_structures import Array, LinkedList, Queue, Stack, BinaryTree, DoublyLinkedList

# Example usage
arr = Array()
arr.append(5)
print(arr.length())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ek_data_structures-0.0.3.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ek_data_structures-0.0.3-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file ek_data_structures-0.0.3.tar.gz.

File metadata

  • Download URL: ek_data_structures-0.0.3.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.9

File hashes

Hashes for ek_data_structures-0.0.3.tar.gz
Algorithm Hash digest
SHA256 40fd8d1c1605799875ddf381cc5d684918e56e91d30ee03c1be826ffa9c1f28c
MD5 ab99a271c9aefa9e1be217c2f5d298f8
BLAKE2b-256 0d5b39705416beb6b54a6afeb6e54d0167995cdc549c333d7b735ad03fa2c9db

See more details on using hashes here.

File details

Details for the file ek_data_structures-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for ek_data_structures-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0664dd5c99f04848bff0a8849a9153fb19f1c0f2de866f03a97affa4a06b44c5
MD5 a0f6c6beba61db8e10b95a1e2395e5f0
BLAKE2b-256 6c216927685ddb33c19c6e585e8b58fa7b3331fc437d16a0d0491cd6638a8053

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page