Skip to content
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

Adding of a method that get the successor of a node inside the birany search tree #589

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
adding a method to get the successor of a node inside the birany sear…
…ch tree data structure
dilane3 committed Jan 24, 2022
commit d94d2267515c1482309ae9b548f5931306baf89c
12,443 changes: 9,740 additions & 2,703 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -148,4 +148,23 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {

return this.left.findMin();
}

/**
* @return {BinarySearchTreeNode}
*/
successor() {
if (this.right) {
return this.right.findMin();
}

let currentNode = this;
let { parent } = currentNode;

while (parent !== null && parent.right && parent.right.value === currentNode.value) {
currentNode = parent;
parent = currentNode.parent;
}

return parent;
}
}
Original file line number Diff line number Diff line change
@@ -252,4 +252,21 @@ describe('BinarySearchTreeNode', () => {

expect(childNode.parent).toBeNull();
});

it('should get the successor of a node', () => {
const rootNode = new BinarySearchTreeNode(5);
rootNode.insert(2);
rootNode.insert(3);
rootNode.insert(1);
rootNode.insert(7);
rootNode.insert(6);

const node = rootNode.find(1);
const node2 = rootNode.find(6);
const node3 = rootNode.find(7);

expect(node.successor().value).toBe(2);
expect(node2.successor().value).toBe(7);
expect(node3.successor()).toBeNull();
});
});