-
-
Notifications
You must be signed in to change notification settings - Fork 472
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 Splay Tree Data Structure #168
Implemented Splay Tree Data Structure #168
Conversation
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]>
…ray updates reflections
…validArgumentException)
, TheAlgorithms#163, TheAlgorithms#166. Implemented Segment Tree Data Structure.
, TheAlgorithms#163, TheAlgorithms#166. Implemented Segment Tree Data Structure.
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 🙂
Co-authored-by: Brandon Johnson <[email protected]>
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.
Awesome work, as always. Thanks for adding this @Ramy-Badr-Ahmed !
Thanks, Brandon (@darwinz) 🙂 |
EDIT: PR is complemented with #171 |
Description
A Splay Tree is a self-adjusting binary search tree that performs splaying, moving a specified element to the root of the tree through rotations. This allows frequently accessed elements to be accessed quickly, improving the performance of subsequent operations.
Applications
Contents
SplayTreeNode
Class: Represents individual nodes in the Splay 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
SplayTreeNode
class encapsulates the structure of each node, linking to its child and parent nodes to maintain the tree structure and facilitate traversal.SplayTree
Class: Implements the core functionalities of a Splay Tree.insert
: Adds a new node to the tree, performing necessary rotations to maintain balance through thesplay
method.search
: Locates a node with a specific key and splay it to the root. If not found, the last visited node is splayed to the root.update
: Update a node with a specific key and splay it to the root. If not found, the last visited node is splayed to the root.delete
: Deletes a node from the tree and restructures the tree to ensure it remains a valid Splay Tree. It follows the top-down splayig approach, where deletion is performed after splaying the node to the root. Once the element is deleted, the two remaining subtrees are merged. If not found, throws aLogicException
splay
: Repositions the accessed node to the root of the tree using tree rotations. It is divided intosplayLeft()
andsplayRight()
depending on the location of the key in the left or right subtree.The
SplayTree
class employs splaying to ensure that frequently accessed nodes are quicker to reach, enhancing access times for repeated operations.SplayTreeRotations
Abstract Class: Contains rotation methods for all cases of the Splay Tree.zig
: Performs a right rotation on the given node when it is aleft
child of itsparent
.zag
: Performs a left rotation on the given node when it is aright
child of itsparent
.zigZig
: Performs two consecutive right rotations on the node and itsparent
.zagZag
: Performs two consecutive left rotations on the node and itsparent
.zigZag
: Performs a left rotation on theleft
child followed by aright
rotation on the node.zagZig
: Performs a right rotation on theright
child followed by aleft
rotation on the node.rotateLeft
: Rotates the node to the left, promoting itsright
child.rotateRight
: Rotates the node to the right, promoting itsleft
child.The
SplayTreeRotations
class facilitates the rotation operations needed to maintain the balance of the Splay Tree during node access and restructuring.Unit Tests:
SplayTreeTest.php
: Contains PHPUnit tests to validate the correct behavior of insertion, deletion, searching, updating, and ensuring the integrity of the Splay Tree structure. It also consider edge cases for these operations.Time Complexity
The insert, delete, and search operations have an amortized time complexity of
O(log n)
in terms of tree height. The splay operation is alsoO(log n)
on average.GitHub Actions
All tests and workflows (in my forked repository) have passed.
Reference
Data Structures and Algorithms in C++, 2nd Edition