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
12,443 changes: 9,740 additions & 2,703 deletions package-lock.json

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions src/algorithms/math/power-of-two-greater-than-number/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Power of two greater than a given Number

We want to calculate the power of two which is greater than a given
number

**Naive solution**

In this case, we need to use a loop.

Let's set `number, power` which are two variables that represente respectively
the number given to the function and the power of two obtained after each iteration.
The variable `power` is initialize to 1 before the loop.
As condition of our loop, we must check out if power is less than number.
If it verified, we continue to run the loop and `power <- power*2` else we break it
and return the value of the variable power.

For exemple: consider number = 5

```
1. number = 5 and power = 1 (power < number) we continue
2. number = 5 and power = 2 (power < number) we continue
3. number = 5 and power = 4 (power < number) we continue
4. number = 5 and power = 8 (power > number) we do not continue and we return 8
```
This method resolve the probleme but the complexity is great.

**Optimal solution**

The best solution for this problem is to use the logarithme function in basis 2.

Note: log2(number) = ln(number) / ln(2)

This expression return a real number and, if we consider the entire part of this
and adding 1 of it we obtain a number. Assuming that this number is n, then if
we calculate 2 power n we obtain the power of two which is directly greater than
number.

For exemple: consider number = 5

```
n = E(log2(number = 5)) + 1
n = E(ln(5) / ln(2)) + 1
n = E(2.3219...) + 1
n = 2 + 1
n = 3
then 2 power n=3 equals 8
```

#reference
"Logarithme binaire - Wikipédia" https://fr.m.wikipedia.org/wiki/Logarithme_binaire


Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import powerOfTwoGreaterThanNumber from '../powerOfTwoGreaterThanNumber';

describe('powerOfTwoGreaterThanNumber', () => {
it('should calculate the power of two greater than the given number', () => {
expect(powerOfTwoGreaterThanNumber(-1)).toBe(-1);
expect(powerOfTwoGreaterThanNumber(0)).toBe(-1);
expect(powerOfTwoGreaterThanNumber(1)).toBe(2);
expect(powerOfTwoGreaterThanNumber(2)).toBe(4);
expect(powerOfTwoGreaterThanNumber(3)).toBe(4);
expect(powerOfTwoGreaterThanNumber(14)).toBe(16);
expect(powerOfTwoGreaterThanNumber(27)).toBe(32);
expect(powerOfTwoGreaterThanNumber(63)).toBe(64);
expect(powerOfTwoGreaterThanNumber(500)).toBe(512);
expect(powerOfTwoGreaterThanNumber(1000)).toBe(1024);
expect(powerOfTwoGreaterThanNumber(2000)).toBe(2048);
expect(powerOfTwoGreaterThanNumber(4097)).toBe(8192);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @param {number} number
* @return {number}
*/
export default function powerOfTwoGreaterThanNumber(number) {
// if number is negative or equal to 0, we return -1
if (number <= 0) {
return -1;
}

const power = Math.floor(Math.log(number) / Math.log(2)) + 1;

return 2 ** power;
}
Original file line number Diff line number Diff line change
@@ -148,4 +148,26 @@ 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 (!this.nodeComparator.equal(parent, null)
&& parent.right
&& this.nodeComparator.equal(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();
});
});