Skip to content

Commit 8b8ef90

Browse files
committed
Added solution to ch4-6 and associated tests.
1 parent 9fa4d89 commit 8b8ef90

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/chapter4/ch4-q06.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
/**
4+
* Finding the successor as a few different scenarios:
5+
* 1. Where a right child exists:
6+
* a. If it has no left child then it is the successor.
7+
* b. If it has a left child then keep following left child pointers until
8+
* you've got the left most child, this is the successor.
9+
* 2. Where no right child exists:
10+
* a. If this node is a left child then the successor is its parent.
11+
* b. Otherwise follow parent pointers until we find a node that is a left
12+
* child of its parent, then the parent is the successor.
13+
*
14+
* N = |tree|
15+
* Time: O(lg N)
16+
* Additional space: O(1)
17+
*/
18+
export function findSuccessor(node) {
19+
if (!node) {
20+
throw new Error('node cannot be null');
21+
}
22+
23+
let snode;
24+
if (node.right) {
25+
snode = node.right;
26+
while (snode.left) {
27+
snode = snode.left;
28+
}
29+
return snode.val;
30+
}
31+
else {
32+
// go up until we find left path
33+
snode = node;
34+
while (snode.parent && snode !== snode.parent.left) {
35+
snode = snode.parent;
36+
}
37+
return snode.parent ? snode.parent.val : undefined;
38+
}
39+
}

src/chapter4/ch4-q06.spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { expect } from 'chai';
2+
import { Tree } from './helpers';
3+
import * as funcs from './ch4-q06';
4+
5+
for (let key in funcs) {
6+
let func = funcs[key];
7+
8+
describe('ch4-q06: ' + key, function() {
9+
10+
beforeEach(function() {
11+
this.tree = new Tree();
12+
});
13+
14+
it('throws an error if node is null', function() {
15+
expect(() => func(null)).to.throw('node cannot be null');
16+
});
17+
18+
it('returns undefined for root of single node tree', function() {
19+
this.tree.add(10);
20+
expect(func(this.tree.root)).to.be.undefined;
21+
});
22+
23+
it('returns right value for simple 3 node balanced tree', function() {
24+
[10, 9, 11].forEach(v => this.tree.add(v));
25+
expect(func(this.tree.root.left)).to.equal(10);
26+
expect(func(this.tree.root)).to.equal(11);
27+
expect(func(this.tree.root.right)).to.be.undefined;
28+
});
29+
30+
it('returns correct values for larger balanced tree', function() {
31+
[8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15].forEach(v => this.tree.add(v));
32+
33+
expect(func(this.tree.root.left.left.left)).to.equal(2);
34+
expect(func(this.tree.root.left.left.right)).to.equal(4);
35+
expect(func(this.tree.root.left.right.left)).to.equal(6);
36+
expect(func(this.tree.root.left.right.right)).to.equal(8);
37+
38+
expect(func(this.tree.root.right.left.left)).to.equal(10);
39+
expect(func(this.tree.root.right.left.right)).to.equal(12);
40+
expect(func(this.tree.root.right.right.left)).to.equal(14);
41+
expect(func(this.tree.root.right.right.right)).to.be.undefined;
42+
});
43+
44+
});
45+
46+
}

0 commit comments

Comments
 (0)