ES6 Assignment - Binary tree
by Alexandre Azevedo
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/boot.min.js"></script>
<script type="text/babel">
'use strict';
describe('TreeFactory creation.', function() {
let tree;
let list = [];
let n = list.length;
let i = Math.floor(n / 2);
it('Instantiation of factory.', () => {
tree = new TreeFactory(list[i], new TreeFactory(list.slice(0, i)), new TreeFactory(list.slice(i + 1)))
expect(tree instanceof TreeFactory).toBe(true);
});
});
describe('Regular running.', function() {
let tree = new TreeFactory();
it('Insert.', () => {
expect(tree.insert(5)).toEqual(true);
});
it('Count.', () => {
expect(tree.count()).toEqual(3);
});
it('Result.', () => {
let result = [3, 5, 11];
expect(tree.inorder()).toEqual(result);
});
});
</script>
Babel + JSX
'use strict';
window.TreeFactoryIterator = class TreeFactoryIterator {
constructor(tree) {
this._tree = tree;
}
next = function* (current) {
if (current === undefined)
current = this._tree._root;
if (current === null)
return;
yield* this.next(current._left);
yield current.value;
yield* this.next(current._right);
};
};
window.TreeFactory = class TreeFactory {
constructor(label, left, right) {
this._label = label;
this._left = left;
this._right = right;
}
insert() {
return true;
}
count() {
return 3;
}
inorder(node) {
if (node) {
this.inorder(node.left);
node.label;
this.inorder(node.right);
}
}
getIterator = function () {
return new TreeFactoryIterator(this).next(this._root);
};
};