-
-
Notifications
You must be signed in to change notification settings - Fork 458
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
darwinz
merged 56 commits into
TheAlgorithms:master
from
Ramy-Badr-Ahmed:features/bstree-implementation
Oct 19, 2024
Merged
Implemented Binary Search Tree as Data Structure. Implemented with Iterator Interface. #174
darwinz
merged 56 commits into
TheAlgorithms:master
from
Ramy-Badr-Ahmed:features/bstree-implementation
Oct 19, 2024
+1,269
−0
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Co-authored-by: Brandon Johnson <[email protected]>
Co-authored-by: Brandon Johnson <[email protected]>
Co-authored-by: Brandon Johnson <[email protected]>
Co-authored-by: Brandon Johnson <[email protected]>
Co-authored-by: Brandon Johnson <[email protected]>
Co-authored-by: Brandon Johnson <[email protected]>
…validArgumentException)
, TheAlgorithms#163, TheAlgorithms#166. Implemented Segment Tree Data Structure.
, TheAlgorithms#163, TheAlgorithms#166. Implemented Segment Tree Data Structure.
Co-authored-by: Brandon Johnson <[email protected]>
Ramy-Badr-Ahmed
commented
Oct 13, 2024
There was a problem hiding this 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
reviewed
Oct 15, 2024
Co-authored-by: Brandon Johnson <[email protected]>
…rrected more comments.
darwinz
approved these changes
Oct 19, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Contents
BSTNode
Class: Represents individual nodes in the Binary Search Tree with attributeskey
,value
,left
,right
andparent
. 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 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.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
, orpostOrder
manner. Example: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.
Reference
Data Structures and Algorithms in C++, 2nd Edition