yorkie-team / yorkie-js-sdk

Yorkie JavaScript SDK
https://yorkie.dev/docs/js-sdk
Apache License 2.0
140 stars 92 forks source link

Apply max height to SplayTree #900

Closed hackerwins closed 4 days ago

hackerwins commented 3 weeks ago

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?

Before After
image image

What are the relevant tickets?

Address https://github.com/yorkie-team/yorkie/issues/941

Checklist

Summary by CodeRabbit

coderabbitai[bot] commented 3 weeks ago

Walkthrough

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.

Changes

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.

Sequence Diagram(s)

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

Poem

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).

Recent review details **Configuration used: CodeRabbit UI** **Review profile: CHILL**
Commits Files that changed from the base of the PR and between 4d5416f255336e120d19458ba095406c158bb9e5 and 14501baf168d4533077f4c60ccc63a022891c269.
Files selected for processing (1) * packages/sdk/src/util/splay_tree.ts (11 hunks)
Additional comments not posted (14)
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
--- Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (Invoked using PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. ### Other keywords and placeholders - Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. - Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description. - Add `@coderabbitai` anywhere in the PR title to generate the title automatically. ### CodeRabbit Configuration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
hackerwins commented 4 days ago

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.