trekhleb / trekhleb/javascript-algorithms

Bug in AVL Tree rorateLeftLeft and rotateRightRight

Open
#325 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
197k
Forks
31k
PR merge metrics
No merged PRs in 30d

Description

run code

  const avl = new AvlTree();
  avl.insert(24);
  avl.insert(12);
  avl.insert(34);
  avl.insert(26);
  avl.insert(25);
  console.log(avl.toString());  // output:  25,26,34,24,34

left child 12 is modified to a duplicate value 34.

  const avl = new AvlTree();
  avl.insert(24);
  avl.insert(12);
  avl.insert(34);
  avl.insert(15);
  avl.insert(18);
  console.log(avl.toString());  // output: 12,24,12,15,18

same question.
fix code:

  rotateLeftLeft(rootNode) {
    // Detach left node from root node.
    const leftNode = rootNode.left;
    rootNode.setLeft(null);

    // Make left node to be a child of rootNode's parent.
    if (rootNode.parent) {
      rootNode.parent.replaceChild(rootNode, leftNode);
    } else if (rootNode === this.root) {
      // If root node is root then make left node to be a new root.
      this.root = leftNode;
    }

    // If left node has a right child then detach it and
    // attach it as a left child for rootNode.
    if (leftNode.right) {
      rootNode.setLeft(leftNode.right);
    }

    // Attach rootNode to the right of leftNode.
    leftNode.setRight(rootNode);
  }
  rotateRightRight(rootNode) {
    // Detach right node from root node.
    const rightNode = rootNode.right;
    rootNode.setRight(null);

    // Make right node to be a child of rootNode's parent.
    if (rootNode.parent) {
      rootNode.parent.replaceChild(rootNode, rightNode);
    } else if (rootNode === this.root) {
      // If root node is root then make right node to be a new root.
      this.root = rightNode;
    }

    // If right node has a left child then detach it and
    // attach it as a right child for rootNode.
    if (rightNode.left) {
      rootNode.setRight(rightNode.left);
    }

    // Attach rootNode to the left of rightNode.
    rightNode.setLeft(rootNode);
  }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate rotateLeftLeft and rotateRightRight in the AVL tree implementation and reproduce the two JavaScript examples from the issue. Compare the resulting toString() output with the expected tree values, then verify that both rotations preserve the nodes without duplicate values.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.