PHP
PHP copied to clipboard
Implemented Binary Search Tree as Data Structure. Implemented with Iterator Interface.
Description
The BST allows for efficient data organization, enabling operations like insertion, deletion, and traversal in logarithmic time complexity, making it ideal for scenarios where fast lookups, insertions, and deletions are necessary. It can be utilized in various applications, including:
- Database indexing: Quickly locating and managing records.
- Memory management: Allocating and deallocating memory efficiently.
- Autocompletion: Facilitating rapid searches for suggestions based on user input.
- Sorting algorithms: Implementing efficient sorting methods.
Contents
-
BSTNodeClass: Represents individual nodes in the Binary Search Tree with attributeskey,value,left,rightandparent. Where:key: The value used to organize the nodes in the tree.value: Stores any associated data for the node.left,right: Pointers to the left and right child nodes.parent: Pointer to the parent node.The
BSTNodeclass encapsulates the structure of each node, linking to its child and parent nodes to maintain the tree structure and facilitate traversal. -
BSTreeClass: Implements the core functionalities of a Binary Search Tree.insert(): Adds a new node to the tree and return the tree root. If duplicated, throws aDuplicateKeyException.search(): Locates and returns a node with a specific key or returns null if not exists.remove(): Deletes and returns a node from the tree, updates the tree referencing, restructures the tree to ensure it remains valid. If node not exists, returns null.minNode(): Return the minimum node in the BST, useful in node removal (in-order successor swap).getHeight(): Get the height of the given node relative to the farthest leaf node.getDepth(): Get the depth of the given node relative to the root of the tree.serialize(): Converts the segment tree into a JSON string.deserialize(): Restores it from the serialized format. -
BinaryTreeTraversalClass:-
Provides traversal methods:
inOrder(): In-order traversal (left-root-right).preOrder(): Pre-order traversal (root-left-right).postOrder(): Post-order traversal (left-right-root).breadthFirst(): Breadth-first (level-order) traversal. -
Implements the
Iteratorinterface Methods:current(): Returns the current node in the iteration.key(): Returns the key of the current node.next(): Moves the pointer to the next node.rewind(): Resets the pointer to the first node.valid(): Checks if the current position is valid in the iteration.
The Iterator allows looping through the Binary Search Tree by iterating over the nodes in
inOrder,preOrder, orpostOrdermanner. Example: -
$bTree = new BSTree(); // default: inOrder traversed nodes
foreach ($bTree as $node) {
// access $node-key and $node->value
}
//Alternatively, set another traversal method before looping by:
$bTree->setTraversalType('postOrder');
$bTree->setTraversalType('preOrder');
Unit Tests:
BSTreeTest.php: Contains PHPUnit tests to validate the correct behavior of insertion, deletion, searching, traversing and iterating the BST, ensuring the integrity of the tree structure. It also consider edge cases for these operations.
GitHub Actions
All tests and workflows (in my forked repository) have passed.
- PHPCS
- DIRECTORY.md
- build