Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged

Conversation

Ramy-Badr-Ahmed
Copy link
Contributor

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed commented Oct 13, 2024

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($data);       
foreach ($bTree as $node) {  
  // looping nodes in inOrder manner (default)
  // access $node-key and $node->value  
} 
 
//Alternatively, set another traversal method before looping by:
$bTree->setTraversalType('postOrder');
$bTree->setTraversalType('preOrder');

//or initalize tree with:
$bTree = new BSTree($data, 'postOrder');     
$bTree = new BSTree($data, '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 as well as large trees.

GitHub Actions

All tests and workflows (in my forked repository) have passed.

  • PHPCS

Code style

  • DIRECTORY.md

directory_md

  • build

PHP Composer


Reference

Data Structures and Algorithms in C++, 2nd Edition

Ramy-Badr-Ahmed and others added 30 commits August 24, 2024 21:17
Ramy-Badr-Ahmed and others added 21 commits September 22, 2024 14:08
Copy link
Contributor Author

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @darwinz
Looking forward to your review 🙂

@darwinz darwinz merged commit 57e772a into TheAlgorithms:master Oct 19, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants