mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Add binary search tree.
This commit is contained in:
38
src/data-structures/tree/__test__/BinaryTreeNode.test.js
Normal file
38
src/data-structures/tree/__test__/BinaryTreeNode.test.js
Normal 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');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user