Add binary search tree.

This commit is contained in:
Oleksii Trekhleb
2018-04-02 17:50:56 +03:00
parent 00e40a0eca
commit d6be33842c
8 changed files with 184 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
import BinaryTreeNode from '../BinaryTreeNode';
describe('BinaryTreeNode', () => {
it('should create node', () => {
const node = new BinaryTreeNode();
expect(node).toBeDefined();
expect(node.value).toBeNull();
expect(node.left).toBeNull();
expect(node.right).toBeNull();
expect(node.hasLeft()).toBeFalsy();
expect(node.hasRight()).toBeFalsy();
const leftNode = new BinaryTreeNode(1);
const rightNode = new BinaryTreeNode(3);
const rootNode = new BinaryTreeNode(2);
rootNode
.addLeft(leftNode)
.addRight(rightNode);
expect(rootNode.value).toBe(2);
expect(rootNode.left.value).toBe(1);
expect(rootNode.right.value).toBe(3);
});
it('should traverse node', () => {
const leftNode = new BinaryTreeNode(1);
const rightNode = new BinaryTreeNode(3);
const rootNode = new BinaryTreeNode(2, leftNode, rightNode);
expect(rootNode.traverseInOrder()).toEqual([null, 1, null, 2, null, 3, null]);
expect(rootNode.toString()).toBe('1,2,3');
});
});