PHP icon indicating copy to clipboard operation
PHP copied to clipboard

Implemented Binary Search Tree as Data Structure. Implemented with Iterator Interface.

Open Ramy-Badr-Ahmed opened this issue 1 year ago • 0 comments

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

  • BSTNode Class: Represents individual nodes in the Binary Search Tree with attributes key, value, left, right and parent. 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 BSTNode class encapsulates the structure of each node, linking to its child and parent nodes to maintain the tree structure and facilitate traversal.

  • BSTree Class: 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 a DuplicateKeyException. 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.

  • BinaryTreeTraversal Class:

    • 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 Iterator interface 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, or postOrder manner. 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

Code style

  • DIRECTORY.md

directory_md

  • build

PHP Composer

Ramy-Badr-Ahmed avatar Oct 13 '24 13:10 Ramy-Badr-Ahmed