Skip to content

The KaririCode DataStructure component offers advanced PHP data structures, including lists, stacks, queues, maps, and sets. It features efficient, strongly-typed, object-oriented implementations like ArrayList, LinkedList, BinaryHeap, and TreeMap.

License

Notifications You must be signed in to change notification settings

KaririCode-Framework/kariricode-data-structure

Repository files navigation

KaririCode Framework: Data Structure Component

en pt-br

PHP Composer Data Structures

The KaririCode Data Structure component provides a collection of advanced data structures implemented in PHP, designed with strong typing and object-oriented principles. It includes implementations for various common structures like dynamic arrays, linked lists, heaps, queues, maps, sets, and stacks.

Features

  • ArrayList: A dynamic array providing fast access and amortized O(1) complexity for adding elements.
  • LinkedList: A doubly linked list with O(1) insertion and removal at both ends, and O(n) for arbitrary index access.
  • BinaryHeap: A binary heap (min-heap or max-heap) with O(log n) for insertions, removals, and polling.
  • HashMap: A hash-based map providing average O(1) complexity for put, get, and remove operations.
  • TreeMap: A self-balancing red-black tree map with O(log n) time complexity for put, get, and remove operations.
  • TreeSet: A set implementation backed by TreeMap, ensuring elements are stored in a sorted order.
  • ArrayDeque: A double-ended queue using a circular array with amortized O(1) operations at both ends.
  • ArrayStack: A stack implemented using a dynamic array, providing O(1) complexity for push, pop, and peek operations.

Installation

To install the KaririCode DataStructure component, use the following command:

composer require kariricode/data-structure

Basic Usage

ArrayList Example

use KaririCode\DataStructure\Collection\ArrayList;

$list = new ArrayList();
$list->add("Item 1");
$list->add("Item 2");
echo $list->get(0); // Outputs: Item 1

LinkedList Example

use KaririCode\DataStructure\Collection\LinkedList;

$linkedList = new LinkedList();
$linkedList->add("First");
$linkedList->add("Second");
$linkedList->remove("First");

BinaryHeap Example

use KaririCode\DataStructure\Heap\BinaryHeap;

$heap = new BinaryHeap();
$heap->add(10);
$heap->add(5);
$heap->add(20);
echo $heap->poll(); // Outputs: 5 (min-heap by default)

HashMap Example

use KaririCode\DataStructure\Map\HashMap;

$map = new HashMap();
$map->put("key1", "value1");
echo $map->get("key1"); // Outputs: value1

TreeSet Example

use KaririCode\DataStructure\Set\TreeSet;

$set = new TreeSet();
$set->add("value1");
$set->add("value2");
echo $set->contains("value1"); // Outputs: true

ArrayStack Example

use KaririCode\DataStructure\Stack\ArrayStack;

$stack = new ArrayStack();
$stack->push("First");
$stack->push("Second");
echo $stack->peek(); // Outputs: Second
$stack->pop();       // Removes "Second"

ArrayDeque Example

use KaririCode\DataStructure\Queue\ArrayDeque;

$deque = new ArrayDeque();
$deque->addFirst("First");
$deque->addLast("Last");
echo $deque->peekLast(); // Outputs: Last
$deque->removeLast();    // Removes "Last"

Testing

To run tests for the KaririCode DataStructure component, execute the following command:

make test

License

This project is licensed under the MIT License. See the LICENSE file for details.

Support and Community


Built with ❤️ by the KaririCode team. Maintained by Walmir Silva - walmir.silva@kariricode.org

About

The KaririCode DataStructure component offers advanced PHP data structures, including lists, stacks, queues, maps, and sets. It features efficient, strongly-typed, object-oriented implementations like ArrayList, LinkedList, BinaryHeap, and TreeMap.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published