/structures

Queue, Stack, List, BinaryTree, BST & Wrappers

Primary LanguagePHP

Structures

Latest Stable Version Packagist PHP Version Support License

This package intoduces multiple new data structures such as Trees, Lists and Wrappers.

Data structures

  • Linear
    • Queue
    • Deque
    • Stack
    • List
  • Tree
    • BinaryTree
    • BinarySearchTree
  • Wrapper
    • StringWrapper
    • IntWrapper
    • FloatWrapper

Get started

composer require adrianschubek/structures

Examples

Check /tests directory for more examples.

Queue
use adrianschubek\Structures\Linear\Queue;

$queue = new Queue();
$queue->enqueue("Berlin");
$queue->enqueue("Munich");
$queue->enqueue("Cologne");

$queue->dequeue();

$arr = $queue->toArray();
// ==> ["Munich", "Cologne"]
List
use adrianschubek\Structures\Linear\DynamicList;

$list = new DynamicList();

$list->append("Berlin");
$list->append("Munich");
$list->append("Cologne");

$list2 = new DynamicList();
$list2->append("Düsseldorf");
$list2->append("Essen");

$list->concat($list2);
// ==> ["Berlin", "Munich", "Cologne", "Düsseldorf", "Essen"]
use adrianschubek\Structures\Linear\DynamicList;

$list = new DynamicList([
    "Berlin", "Munich", "Cologne"
]);

$list2 = new DynamicList([
    "Munich", "Cologne"
]);

$diff = $list->diff($list2); 
// ==> ["Berlin"]
Deque
use adrianschubek\Structures\Linear\Deque;

$deque = new Deque([
    "Berlin",
    "Munich",
    "Cologne"
]);

$deque->pop();

$ele = $deque->last();
// ==> "Munich"
BinaryTree
use adrianschubek\Structures\Tree\BinaryTree;

$tree = new BinaryTree("Berlin");
$tree->setLeftTree(new BinaryTree("Hamburg"));
$tree->setRightTree(new BinaryTree("Munich"));

$ele = $tree->getLeftTree()->getContent();
// ==> "Hamburg"
BinarySearchTree
use adrianschubek\Structures\Tree\BinarySearchTree;
use adrianschubek\Structures\Wrapper\StringWrapper;

$tree = new BinarySearchTree("Berlin");
$tree->insert(new StringWrapper("Hamburg"));
$tree->insert(new StringWrapper("Munich"));
$tree->insert(new StringWrapper("Frankfurt"));
// OR
$tree = BinarySearchTree::fromArray(["Berlin", "Hamburg", "Munich", "Frankfurt"]);
// OR
$tree = BinarySearchTree::fromString("Berlin,Munich,Frankfurt,Hamburg");

$tree->search(new StringWrapper("Frankfurt"));
// ==> "Frankfurt"

$tree->search(new StringWrapper("Mainz"));
// ==> null