Closed hackerwins closed 4 days ago
The changes involve enhancements to the SplayNode
and SplayTree
classes by introducing new properties and methods for managing node heights and balancing the tree. A height
property is added to SplayNode
, along with methods to retrieve and set this height. The SplayTree
class now includes a nodeCount
property and methods for balancing the tree and updating node weights and heights. These modifications aim to improve the overall functionality of the splay tree.
File Path | Change Summary |
---|---|
packages/sdk/src/util/splay_tree.ts | - Added methods for height management in SplayNode : getHeight , getLeftHeight , getRightHeight , setHeight . - Introduced nodeCount property in SplayTree . - Added balance , splayMaxHeight , and updateWeightAndHeight methods in SplayTree . - Added updateHeight method in SplayTree . |
sequenceDiagram
participant User
participant SplayTree
participant SplayNode
User->>SplayTree: insert(node)
SplayTree->>SplayNode: create new node
SplayTree->>SplayNode: setHeight()
SplayTree->>SplayTree: updateWeightAndHeight(node)
SplayTree->>SplayTree: balance()
SplayTree->>User: confirm insertion
In the forest where trees sway,
A splay tree found a new way.
With heights now tall and nodes so bright,
It dances in the moonlight.
Hopping with joy, the rabbit sings,
For balance and heights are wondrous things! 🐇✨
[!TIP]
OpenAI O1 model for chat
- We have deployed OpenAI's latest O1 model for chat. - OpenAI claims that this model has superior reasoning capabilities than their GPT-4o model. - Please share any feedback with us in the [discussions post](https://discord.com/channels/1134356397673414807/1283929536186155099).
packages/sdk/src/util/splay_tree.ts (14)
`29-29`: **LGTM!** The new `height` property is correctly initialized to 1 in the constructor. --- `95-100`: **LGTM!** The `getHeight` method correctly returns the height of the node. --- `174-179`: **LGTM!** The `getLeftHeight` method correctly returns the height of the left child node or 0 if it doesn't exist. --- `181-186`: **LGTM!** The `getRightHeight` method correctly returns the height of the right child node or 0 if it doesn't exist. --- `188-193`: **LGTM!** The `setHeight` method correctly updates the height of the node. --- `203-203`: **LGTM!** The new `nodeCount` property is correctly initialized to 0 in the constructor. --- `249-268`: **LGTM!** The `balance` method correctly balances the tree based on the height of the root node and the total node count. It calls the `splayMaxHeight` method if the height of the root node exceeds a calculated threshold. --- `270-285`: **LGTM!** The `splayMaxHeight` method correctly splays the node with the maximum height by traversing the tree and selecting the child node with the maximum height at each level. --- `357-363`: **LGTM!** The `updateWeightAndHeight` method correctly updates both the weight and height of a node by calling the `updateWeight` and `updateHeight` methods. --- `365-379`: **LGTM!** The `updateHeight` method correctly recalculates the height of a node by comparing its current height with the height of its left and right child nodes plus 1. --- `351-352`: **LGTM!** The `updateWeightAndHeight` method is correctly called on both the target node and the new node after inserting a node. --- `476-476`: **LGTM!** The `updateWeightAndHeight` method is correctly called on the root node after deleting a node. --- `601-602`: **LGTM!** The `updateWeightAndHeight` method is correctly called on both the root node and the pivot node after rotating the tree. Also applies to: 626-627 --- `340-340`: **Verify the impact of calling `balance` after inserting a node.** Calling `balance` after inserting a node ensures that the tree remains balanced. However, it's important to verify that this doesn't have any unintended side effects on the tree structure or the order of the nodes. Run the following script to verify the tree structure and node order after inserting a node and calling `balance`:Verification successful
--- **Verification successful: The `balance` method maintains tree balance as intended.** The `balance` method, through its calls to `splayMaxHeight` and `splayNode`, performs standard splay tree operations to maintain balance without unintended side effects on the tree structure or node order.Scripts executed
The following scripts were executed for the analysis: Script: ```shell #!/bin/bash # Description: Verify the tree structure and node order after inserting a node and calling `balance`. # Test: Search for the `insertAfter` method and check if `balance` is called after inserting a node. # Expect: `balance` is called after inserting a node. rg --type typescript -A 10 $'insertAfter' # Test: Search for the `balance` method and check if it modifies the tree structure or node order. # Expect: The tree structure and node order are not modified by `balance`. rg --type typescript -A 20 $'balance\(\)' ``` Length of output: 153 --- Script: ```shell #!/bin/bash # Description: Verify the tree structure and node order after inserting a node and calling `balance`. # Test: Search for the `insertAfter` method and check if `balance` is called after inserting a node. # Expect: `balance` is called after inserting a node. rg --glob '*.ts' -A 10 'insertAfter' # Test: Search for the `balance` method and check if it modifies the tree structure or node order. # Expect: The tree structure and node order are not modified by `balance`. rg --glob '*.ts' -A 20 'balance\(\)' ``` Length of output: 35288 --- Script: ```shell #!/bin/bash # Description: Verify the impact of the `splayMaxHeight` method on the tree structure and node order. # Test: Search for the `splayMaxHeight` method to understand its implementation. rg --glob '*.ts' -A 20 'splayMaxHeight' # Test: Search for any other methods called within `balance` that might affect the tree structure. rg --glob '*.ts' -A 20 'splayNode' ``` Length of output: 15728
Adding both weight and height to each node increases memory usage, and the cost of re-computation becomes higher. Additionally, when splaying based on height, it seems that more splay operations are performed than anticipated. Therefore, @m4ushold and I have decided to close this PR for now.
What this PR does / why we need it?
Apply max height to splay
Related to https://github.com/yorkie-team/yorkie/pull/989
Any background context you want to provide?
What are the relevant tickets?
Address https://github.com/yorkie-team/yorkie/issues/941
Checklist
Summary by CodeRabbit