Code coverage report for spec/dataStructures/binarySearchTree.spec.js

Statements: 100% (37 / 37)      Branches: 100% (0 / 0)      Functions: 100% (8 / 8)      Lines: 100% (37 / 37)      Ignored: none     

All files » spec/dataStructures/ » binarySearchTree.spec.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 581   1 1 1   1 1 1     1 1         1 1 1 1 1   1 3     1 1 1 1   1 1 1     1 1 1 1   1 1 1     1 1 1 1   1 1 1      
var bst = require("../../lib/dataStructures/binarySearchTree.js");
 
describe('When adding an element to a binary search tree', function () {
	var tree;
	var value = 2;
 
	beforeEach(function() {
		tree = new bst();
		tree.add(value);
	});
 
	it('the 1st element of the binary search tree should contain the value.', function () {
		expect(tree.head.data).toBe(value);
	});
});
 
 
describe('When adding three elements to a binary search tree', function () {
	var tree;
	var value2 = 2;
	var value1 = 1;
	var value3 = 3;
 
	beforeEach(function() {
		tree = new bst();
	});
 
	it('if the elements are added in order (middle, least, greatest) the tree will be balanced.', function () {
		tree.add(value2);
		tree.add(value1);
		tree.add(value3);
 
		expect(tree.head.data).toBe(value2);
		expect(tree.head.left.data).toBe(value1);
		expect(tree.head.right.data).toBe(value3);
	});
 
	it('if the elements are added in order (least, middle, greatest) the tree will only have right nodes.', function () {
		tree.add(value1);
		tree.add(value2);
		tree.add(value3);
 
		expect(tree.head.data).toBe(value1);
		expect(tree.head.right.data).toBe(value2);
		expect(tree.head.right.right.data).toBe(value3);
	});
 
	it('if the elements are added in order (greatest, middle, least) the tree will only have left nodes.', function () {
		tree.add(value3);
		tree.add(value2);
		tree.add(value1);
 
		expect(tree.head.data).toBe(value3);
		expect(tree.head.left.data).toBe(value2);
		expect(tree.head.left.left.data).toBe(value1);
	});
});