Skip to content

Commit

Permalink
Add binary search tree Jasmine tests
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <[email protected]>
  • Loading branch information
darkodraskovic committed Aug 16, 2022
1 parent a187ceb commit 2017cac
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
25 changes: 25 additions & 0 deletions spec/binary-search-tree.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
BinarySearchTree
} from '../src/data-structures/binary-search-tree.js'

describe("Function", function () {

const insert = [9, 4, 6, 5, 3, 20, 20, 170, 15, 1, 25, 300, 7, 30];
const remove = [9, 300, 25, 7, 25, 9, 9, 170, 15];

it(BinarySearchTree.name + " constructs binary search tree", function () {
const bst = new BinarySearchTree();

insert.forEach((value) => bst.insert(value));

insert.forEach((value) => expect(bst.lookup(value)).not.toBeNull());

remove.forEach((value) => bst.remove(value));

let difference = insert.filter(x => !remove.includes(x));

difference.forEach((value) => expect(bst.lookup(value)).not.toBeNull());

remove.forEach((value) => expect(bst.lookup(value)).toBeNull());
})
})
23 changes: 23 additions & 0 deletions src/algorithms/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// javascript built in search
var beasts = ['Centaur', 'Godzilla',
'Mosura', 'Minotaur', 'Hydra', 'Nessie'];

(function test() {
// javascript built in linear searches; O(n)
console.assert(beasts.indexOf('Godzilla') == 1);
console.assert(beasts.indexOf('Cyclope') == -1);

console.assert(beasts.findIndex(function (item) {
return item == 'Hydra'
}) == 4);
console.assert(beasts.findIndex(function (item) {
return item == 'Cyclope'
}) == -1);

console.assert(beasts.find(function (item) {
return item == 'Hydra'
}) == 'Hydra');

console.assert(beasts.includes('Nessie') == true);
console.assert(beasts.includes('Cyclope') == false);
})()
24 changes: 15 additions & 9 deletions src/data-structures/binary-search-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Node {
}
}

class BinarySearchTree {
export class BinarySearchTree {
constructor() {
this.root = null;
}
Expand Down Expand Up @@ -47,8 +47,10 @@ class BinarySearchTree {
parent = current;
current = current.right;
} else {
// current has two children => replace with successor
// we found value
// 1. current has two children => replace with successor
if (current.left && current.right) {
// detach and rearrange left and right branch
let successor = current.right;
let successorParent = null;
while (successor.left) {
Expand All @@ -61,13 +63,16 @@ class BinarySearchTree {
successor.right = current.right;
}
if (!parent) {
// we removed root
this.root = successor;
} else {
// attach branch to parent's left
if (current.value < parent.value) parent.left = successor;
// attach branch to parent's right
else parent.right = successor;
}
return current;
// current has one child => bypass current
// 2. current has one child => bypass current
} else if (current.left || current.right) {
// value is at the root
if (!parent) {
Expand All @@ -83,7 +88,7 @@ class BinarySearchTree {
else parent.right = current.right;
}
return current;
// current has no children => remove current
// 3. current has no children => remove current
} else {
if (current.value < parent.value) parent.left = null;
else parent.right = null;
Expand Down Expand Up @@ -115,7 +120,7 @@ function traverse(node) {
return tree;
}

export function test() {
(function test() {
const bst = new BinarySearchTree();
bst.insert(9);
bst.insert(4);
Expand All @@ -141,8 +146,9 @@ export function test() {
bst.remove(9); // remove non existing
bst.remove(170);
bst.remove(15); // root
console.dir(traverse(bst.root), { depth: null });

// console.log("================================")
// console.log(bst.lookup(20));
}
var tree = traverse(bst.root);
console.dir(tree, { depth: null });

console.log(bst.lookup(20));
})()
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { test } from './algorithms/recursion.js'
import { test } from './algorithms/search.js'

test();
// test();

0 comments on commit 2017cac

Please sign in to comment.