Skip to content

Commit 9fa4d89

Browse files
committed
added solution to ch4-4 and associated tests.
1 parent a8ae4cc commit 9fa4d89

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/chapter4/ch4-q04.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
/**
4+
* This function attempts to check if the tree is completely balanced by finding
5+
* the shortest and longest paths from root to leaf. If the difference between
6+
* these two paths is greater than 1 then the tree is not balanced.
7+
*
8+
* N = |tree|
9+
* Time: O(N)
10+
* Additional space: O(lg N) - space cost is due to call stack size while using
11+
* recursion, this may be O(N) in the worst case.
12+
*/
13+
export function isBalanced(tree) {
14+
if (!tree || !tree.root) {
15+
return true;
16+
}
17+
18+
let node = tree.root,
19+
cache = {
20+
min: Number.MAX_SAFE_INTEGER,
21+
max: Number.MIN_SAFE_INTEGER
22+
};
23+
24+
findDepth(cache, node, 0);
25+
return cache.max - cache.min <= 1;
26+
}
27+
28+
function findDepth(cache, node, depth) {
29+
if (!node) {
30+
if (depth < cache.min) {
31+
cache.min = depth;
32+
}
33+
if (depth > cache.max) {
34+
cache.max = depth;
35+
}
36+
} else {
37+
findDepth(cache, node.left, depth + 1);
38+
findDepth(cache, node.right, depth + 1);
39+
}
40+
}

src/chapter4/ch4-q04.spec.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { expect } from 'chai';
2+
import { Tree } from './helpers';
3+
import * as funcs from './ch4-q04';
4+
5+
for (let key in funcs) {
6+
let func = funcs[key];
7+
8+
describe('ch4-q04: ' + key, function() {
9+
10+
beforeEach(function() {
11+
this.tree = new Tree();
12+
});
13+
14+
it('returns true for null tree or root', function() {
15+
expect(func(null)).to.be.true;
16+
expect(func(this.tree)).to.be.true;
17+
});
18+
19+
it('returns true for single node tree', function() {
20+
this.tree.add(10);
21+
expect(func(this.tree)).to.be.true;
22+
});
23+
24+
it('returns false for left heavy tree', function() {
25+
this.tree.add(10);
26+
this.tree.add(9);
27+
this.tree.add(8);
28+
expect(func(this.tree)).to.be.false;
29+
});
30+
31+
it('returns false with equal max height tree but uneven', function() {
32+
this.tree.add(10);
33+
this.tree.add(11);
34+
this.tree.add(12);
35+
this.tree.add(13);
36+
this.tree.add(9);
37+
this.tree.add(8);
38+
this.tree.add(7);
39+
expect(func(this.tree)).to.be.false;
40+
});
41+
42+
it('returns true for larger balanced tree', function() {
43+
this.tree.add(8);
44+
expect(func(this.tree), 'root').to.be.true;
45+
this.tree.add(4);
46+
this.tree.add(12);
47+
expect(func(this.tree), 'depth 1').to.be.true;
48+
this.tree.add(2);
49+
this.tree.add(6);
50+
this.tree.add(10);
51+
this.tree.add(14);
52+
expect(func(this.tree), 'depth 2').to.be.true;
53+
this.tree.add(1);
54+
this.tree.add(3);
55+
this.tree.add(5);
56+
this.tree.add(7);
57+
expect(func(this.tree), '1/2 depth 3').to.be.true;
58+
this.tree.add(9);
59+
this.tree.add(11);
60+
this.tree.add(13);
61+
this.tree.add(15);
62+
expect(func(this.tree), 'depth 3').to.be.true;
63+
this.tree.add(16);
64+
expect(func(this.tree), '1 depth 4').to.be.true;
65+
});
66+
67+
});
68+
69+
}

0 commit comments

Comments
 (0)