Skip to content

Commit 8a74774

Browse files
committed
Added solution to ch4-3 and associated tests.
1 parent 270d28b commit 8a74774

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/chapter4/ch4-q03.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
import { LinkedList } from './helpers';
3+
4+
/**
5+
* Travels through tree and adds values into a list of linked lists. Each level
6+
* of tree is represented in a linked list.
7+
*
8+
* N = |tree|
9+
* Time: O(N)
10+
* Additional space: O(N)
11+
*/
12+
export function listTreeByDepthOrder(tree) {
13+
let lists = [];
14+
addToList(lists, tree.root, 0);
15+
return lists;
16+
}
17+
18+
function addToList(lists, node, depth) {
19+
if (node) {
20+
if (!lists[depth]) {
21+
lists[depth] = new LinkedList();
22+
}
23+
24+
lists[depth].append(node.val);
25+
26+
addToList(lists, node.left, depth + 1);
27+
addToList(lists, node.right, depth + 1);
28+
}
29+
}

src/chapter4/ch4-q03.spec.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { expect } from 'chai';
2+
import * as helpers from './helpers';
3+
import * as funcs from './ch4-q03';
4+
5+
function toArrayOfArrays(lists) {
6+
return lists.map(l => l.toArray());
7+
}
8+
9+
for (let key in funcs) {
10+
let func = funcs[key];
11+
12+
describe('ch4-q03: ' + key, function() {
13+
14+
beforeEach(function() {
15+
this.tree = new helpers.Tree();
16+
});
17+
18+
it('returns empty list for empty tree', function() {
19+
expect(toArrayOfArrays(func(this.tree))).to.eql([]);
20+
});
21+
22+
it('returns true for single node tree', function() {
23+
this.tree.add(10);
24+
expect(toArrayOfArrays(func(this.tree))).to.eql([[10]]);
25+
});
26+
27+
it('returns single value lists for left heavy tree', function() {
28+
[10, 9, 8].forEach(v => this.tree.add(v));
29+
expect(toArrayOfArrays(func(this.tree))).to.eql([
30+
[10],
31+
[9],
32+
[8]
33+
]);
34+
});
35+
36+
it('returns 2 value lists for upside down V shaped tree', function() {
37+
[10, 11, 12, 13, 9, 8, 7].forEach(v => this.tree.add(v));
38+
expect(toArrayOfArrays(func(this.tree))).to.eql([
39+
[10],
40+
[9, 11],
41+
[8, 12],
42+
[7, 13]
43+
]);
44+
});
45+
46+
it('returns true for larger balanced tree', function() {
47+
let expected = [];
48+
this.tree.add(8);
49+
expected.push([8]);
50+
expect(toArrayOfArrays(func(this.tree)), 'root').to.be.eql(expected);
51+
this.tree.add(4);
52+
this.tree.add(12);
53+
expected.push([4, 12]);
54+
expect(toArrayOfArrays(func(this.tree)), 'depth 1').to.eql(expected);
55+
this.tree.add(2);
56+
this.tree.add(6);
57+
this.tree.add(10);
58+
this.tree.add(14);
59+
expected.push([2, 6, 10, 14]);
60+
expect(toArrayOfArrays(func(this.tree)), 'depth 2').to.eql(expected);
61+
this.tree.add(1);
62+
this.tree.add(3);
63+
this.tree.add(5);
64+
this.tree.add(7);
65+
expected.push([1, 3, 5, 7]);
66+
expect(toArrayOfArrays(func(this.tree)), '1/2 depth 3').to.eql(expected);
67+
this.tree.add(9);
68+
this.tree.add(11);
69+
this.tree.add(13);
70+
this.tree.add(15);
71+
expected[expected.length - 1].push(9, 11, 13, 15);
72+
expect(toArrayOfArrays(func(this.tree)), 'depth 3').to.eql(expected);
73+
this.tree.add(16);
74+
expected.push([16]);
75+
expect(toArrayOfArrays(func(this.tree)), '1 depth 4').to.eql(expected);
76+
});
77+
78+
});
79+
80+
}

0 commit comments

Comments
 (0)