Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c2c3bbc

Browse files
committed
add binary search trees, & unit tests
1 parent 99f807f commit c2c3bbc

File tree

7 files changed

+205
-3
lines changed

7 files changed

+205
-3
lines changed

‎dist/datastructures/BinarySearchTree.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
class BinaryTreeNode {
4+
constructor(value) {
5+
this.left = null;
6+
this.right = null;
7+
this.value = value;
8+
}
9+
}
10+
exports.BinaryTreeNode = BinaryTreeNode;
11+
class BinarySearchTree {
12+
constructor() {
13+
this.root = null;
14+
}
15+
insert(value) {
16+
// could refactor as recursive function rather than using while loop
17+
const node = new BinaryTreeNode(value);
18+
if (!this.root) {
19+
this.root = node;
20+
return node;
21+
}
22+
let targetNode = this.root;
23+
while (targetNode) {
24+
if (node.value < targetNode.value) {
25+
if (!targetNode.left) {
26+
targetNode.left = node;
27+
return node;
28+
}
29+
targetNode = targetNode.left;
30+
}
31+
else if (node.value >= targetNode.value) {
32+
if (!targetNode.right) {
33+
targetNode.right = node;
34+
return node;
35+
}
36+
targetNode = targetNode.right;
37+
}
38+
}
39+
return node;
40+
}
41+
}
42+
exports.default = BinarySearchTree;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
var __importStar = (this && this.__importStar) || function (mod) {
3+
if (mod && mod.__esModule) return mod;
4+
var result = {};
5+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6+
result["default"] = mod;
7+
return result;
8+
};
9+
Object.defineProperty(exports, "__esModule", { value: true });
10+
const BinarySearchTree_1 = __importStar(require("./BinarySearchTree"));
11+
let BST = new BinarySearchTree_1.default();
12+
beforeEach(() => BST = new BinarySearchTree_1.default());
13+
describe('basic binary search tree functionality', () => {
14+
test('Binary search tree exists', () => {
15+
expect(BST).toBeTruthy();
16+
});
17+
test('Tree root initiates as null', () => {
18+
expect(BST.root).toBeNull();
19+
});
20+
test('Can add new root node to tree', () => {
21+
var _a;
22+
const node = new BinarySearchTree_1.BinaryTreeNode(5);
23+
BST.root = node;
24+
expect((_a = BST.root) === null || _a === void 0 ? void 0 : _a.value).toBe(5);
25+
});
26+
test('Can add left and right nodes to tree root manually', () => {
27+
var _a, _b, _c, _d;
28+
BST.root = new BinarySearchTree_1.BinaryTreeNode(5);
29+
BST.root.left = new BinarySearchTree_1.BinaryTreeNode(1);
30+
BST.root.right = new BinarySearchTree_1.BinaryTreeNode(10);
31+
expect((_b = (_a = BST.root) === null || _a === void 0 ? void 0 : _a.left) === null || _b === void 0 ? void 0 : _b.value).toBe(1);
32+
expect((_d = (_c = BST.root) === null || _c === void 0 ? void 0 : _c.right) === null || _d === void 0 ? void 0 : _d.value).toBe(10);
33+
});
34+
test('Insert method exists', () => {
35+
expect(typeof BST.insert).toBe('function');
36+
});
37+
test('insert method works by inserting in order (left to right ascending)', () => {
38+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
39+
BST.insert(5);
40+
expect((_a = BST.root) === null || _a === void 0 ? void 0 : _a.value).toBe(5);
41+
BST.insert(3);
42+
expect((_c = (_b = BST.root) === null || _b === void 0 ? void 0 : _b.left) === null || _c === void 0 ? void 0 : _c.value).toBe(3);
43+
BST.insert(7);
44+
expect((_e = (_d = BST.root) === null || _d === void 0 ? void 0 : _d.right) === null || _e === void 0 ? void 0 : _e.value).toBe(7);
45+
BST.insert(4);
46+
expect((_h = (_g = (_f = BST.root) === null || _f === void 0 ? void 0 : _f.left) === null || _g === void 0 ? void 0 : _g.right) === null || _h === void 0 ? void 0 : _h.value).toBe(4);
47+
BST.insert(6);
48+
expect((_l = (_k = (_j = BST.root) === null || _j === void 0 ? void 0 : _j.right) === null || _k === void 0 ? void 0 : _k.left) === null || _l === void 0 ? void 0 : _l.value).toBe(6);
49+
BST.insert(1);
50+
expect((_p = (_o = (_m = BST.root) === null || _m === void 0 ? void 0 : _m.left) === null || _o === void 0 ? void 0 : _o.left) === null || _p === void 0 ? void 0 : _p.value).toBe(1);
51+
BST.insert(8);
52+
expect((_s = (_r = (_q = BST.root) === null || _q === void 0 ? void 0 : _q.right) === null || _r === void 0 ? void 0 : _r.right) === null || _s === void 0 ? void 0 : _s.value).toBe(8);
53+
});
54+
});

‎dist/datastructures/Queue.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const IndexError_1 = __importDefault(require("../utils/IndexError"));
88
class Queue {
99
constructor() {
1010
this.enqueue = (value) => {
11+
// doubly linked list's push
1112
const oldBack = this.back;
1213
const node = new Node_1.default(value);
1314
// add node to end of list
@@ -24,6 +25,7 @@ class Queue {
2425
return node;
2526
};
2627
this.dequeue = () => {
28+
// doubly linked list's shift
2729
if (this.front && this.front.next) {
2830
this.front.next.prev = null;
2931
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import BinarySearchTree, { BinaryTreeNode } from './BinarySearchTree';
2+
3+
let BST = new BinarySearchTree<number>();
4+
beforeEach(() => BST = new BinarySearchTree<number>());
5+
6+
describe('basic binary search tree functionality', () => {
7+
test('Binary search tree exists', () => {
8+
expect(BST).toBeTruthy();
9+
})
10+
test('Tree root initiates as null', () => {
11+
expect(BST.root).toBeNull();
12+
})
13+
test('Can add new root node to tree', () => {
14+
const node = new BinaryTreeNode(5);
15+
BST.root = node;
16+
expect(BST.root?.value).toBe(5);
17+
})
18+
test('Can add left and right nodes to tree root manually', () => {
19+
BST.root = new BinaryTreeNode(5);
20+
BST.root.left = new BinaryTreeNode(1);
21+
BST.root.right = new BinaryTreeNode(10);
22+
expect(BST.root?.left?.value).toBe(1);
23+
expect(BST.root?.right?.value).toBe(10);
24+
})
25+
test('Insert method exists', () => {
26+
expect(typeof BST.insert).toBe('function');
27+
})
28+
test('insert method works by inserting in order (left to right ascending)', () => {
29+
BST.insert(5);
30+
expect(BST.root?.value).toBe(5);
31+
BST.insert(3);
32+
expect(BST.root?.left?.value).toBe(3);
33+
BST.insert(7);
34+
expect(BST.root?.right?.value).toBe(7);
35+
BST.insert(4);
36+
expect(BST.root?.left?.right?.value).toBe(4);
37+
BST.insert(6);
38+
expect(BST.root?.right?.left?.value).toBe(6);
39+
BST.insert(1);
40+
expect(BST.root?.left?.left?.value).toBe(1);
41+
BST.insert(8);
42+
expect(BST.root?.right?.right?.value).toBe(8);
43+
})
44+
})

‎src/datastructures/BinarySearchTree.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
interface BinaryTreeNode<T> {
2+
left: BinaryTreeNode<T> | null;
3+
right: BinaryTreeNode<T> | null;
4+
value: T;
5+
}
6+
7+
class BinaryTreeNode<T> {
8+
constructor(value: T) {
9+
this.left = null;
10+
this.right = null;
11+
this.value = value;
12+
}
13+
}
14+
15+
export { BinaryTreeNode };
16+
17+
interface BinarySearchTree<T> {
18+
root: BinaryTreeNode<T> | null;
19+
}
20+
21+
class BinarySearchTree<T> {
22+
constructor() {
23+
this.root = null;
24+
}
25+
26+
insert(value: T): BinaryTreeNode<T> {
27+
// could refactor as recursive function rather than using while loop
28+
const node = new BinaryTreeNode<T>(value);
29+
if (!this.root) {
30+
this.root = node;
31+
return node;
32+
}
33+
let targetNode: BinaryTreeNode<T> = this.root;
34+
while (targetNode) {
35+
if (node.value < targetNode.value) {
36+
if (!targetNode.left) {
37+
targetNode.left = node;
38+
return node;
39+
}
40+
targetNode = targetNode.left;
41+
}
42+
else if (node.value >= targetNode.value) {
43+
if (!targetNode.right) {
44+
targetNode.right = node;
45+
return node;
46+
}
47+
targetNode = targetNode.right;
48+
}
49+
}
50+
return node;
51+
}
52+
}
53+
54+
55+
export default BinarySearchTree;

‎src/datastructures/Queue.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import randomRange from '../utils/randomRange';
2-
import { fillListWithDummyData } from './SinglyLinkedList.test';
3-
import { testTraversalDLL } from './DoublyLinkedList.test';
41
import _Node from './Node';
52
import Queue from './Queue';
63

‎src/datastructures/Queue.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ interface Queue<T> {
88

99

1010
class Queue<T> {
11+
// this queue implementation uses doubly linked nodes, allowing for
12+
// faster getting/setting of data in the middle/back of the queue.
13+
// while nodes can only be enqueued from the back and dequeued from the front,
14+
// their data prop is always mutable and accessible--making this not exactly
15+
// a strict queue, but functional in O(1)time as a queue along with a couple
16+
// extra features.
1117
private _length: number;
1218
constructor() {
1319
this._length = 0;
@@ -20,6 +26,7 @@ class Queue<T> {
2026
}
2127

2228
enqueue = (value: T): _Node<T> => {
29+
// doubly linked list's push
2330
const oldBack = this.back;
2431
const node = new _Node(value);
2532
// add node to end of list
@@ -36,6 +43,7 @@ class Queue<T> {
3643
}
3744

3845
dequeue = (): _Node<T> | void => {
46+
// doubly linked list's shift
3947
if (this.front && this.front.next) {
4048
this.front.next.prev = null;
4149
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /