Skip to content

Fix binary tree code#9

Merged
AvraamMavridis merged 1 commit intoAvraamMavridis:masterfrom
9inpachi:master
Nov 10, 2020
Merged

Fix binary tree code#9
AvraamMavridis merged 1 commit intoAvraamMavridis:masterfrom
9inpachi:master

Conversation

@9inpachi
Copy link
Copy Markdown
Contributor

Fixed the traverse function which was affecting findParentOfToBeDeletedNode function by returning the root as parent every time.

Also fixed the deleteNode function which was not working for the deletion of root node and some other cases.

I have simpler version for the deleteNode function if you want to use that in the guide - let me know - I will add replace it in this PR.

const deleteNode = (root: MyNode, value: number): MyNode => {
  if (value < root.value) {
    root.left = deleteNode(root.left, value);
  } else if (value > root.value) {
    root.right = deleteNode(root.right, value);
  } else {
    if (root.left && !root.right) {
      root = root.left;
    } else if (root.right && !root.left) {
      root = root.right;
    } else if (root.left && root.right) {
      const node = mostLeftNode(root.right);
      const temp = node.value;
      root.value = temp;
      root.right = deleteNode(root.right, temp);
    } else {
      root = undefined;
    }
  }
  return root;
}

Thanks.

@AvraamMavridis AvraamMavridis merged commit efaa486 into AvraamMavridis:master Nov 10, 2020
@AvraamMavridis
Copy link
Copy Markdown
Owner

thx @9inpachi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants