Skip to content

Commit

Permalink
Add breadth first traversal to BST
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <[email protected]>
  • Loading branch information
darkodraskovic committed Aug 18, 2022
1 parent 2017cac commit b3d87ac
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 40 deletions.
4 changes: 2 additions & 2 deletions spec/binary-search-tree.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ describe("Function", function () {
difference.forEach((value) => expect(bst.lookup(value)).not.toBeNull());

remove.forEach((value) => expect(bst.lookup(value)).toBeNull());
})
})
});
});
17 changes: 17 additions & 0 deletions spec/search.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
BinarySearchTree
} from '../src/data-structures/binary-search-tree.js'

describe("Function", function () {
const values = [9, 4, 6, 20, 170, 15, 1];
const bfs = [9, 4, 20, 1, 6, 15, 170];
const dfs = [9, 4, 1, 6, 20, 15, 170];

it("breadth first traverses the binary search tree", function () {
const tree = new BinarySearchTree();
values.forEach(val => {
tree.insert(val);
});
expect(tree.bfs()).toEqual(bfs);
});
});
18 changes: 18 additions & 0 deletions src/algorithms/search.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
BinarySearchTree
} from '../src/data-structures/binary-search-tree.js'

// javascript built in search
var beasts = ['Centaur', 'Godzilla',
'Mosura', 'Minotaur', 'Hydra', 'Nessie'];
Expand All @@ -20,4 +24,18 @@ var beasts = ['Centaur', 'Godzilla',

console.assert(beasts.includes('Nessie') == true);
console.assert(beasts.includes('Cyclope') == false);

// BFS
const tree = new BinarySearchTree();
[9, 4, 6, 20, 170, 15, 1].forEach(val => {
tree.insert(val);
});
let bfs = tree.bfs();
// BFS result: [9, 4, 20, 1, 6, 15, 170]
// DFS result: [9, 4, 1, 6, 20, 15, 170]
console.log();
})()

function traverse(node) {

}
43 changes: 16 additions & 27 deletions src/data-structures/binary-search-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ export class BinarySearchTree {
}
return null;
}
bfs() {
let list = [];
let queue = [];
queue.push(this.root);

while (queue.length) {
let current = queue.shift();
list.push(current.value);
if (current.left) queue.push(current.left);
if (current.right) queue.push(current.right);
}
return list;
}
dfs() {

}
}

function traverse(node) {
Expand All @@ -122,33 +138,6 @@ function traverse(node) {

(function test() {
const bst = new BinarySearchTree();
bst.insert(9);
bst.insert(4);
bst.insert(6);
bst.insert(5);
bst.insert(3);
bst.insert(20);
bst.insert(20);
bst.insert(170);
bst.insert(15);
bst.insert(1);
bst.insert(25);
bst.insert(300);
bst.insert(7);
bst.insert(30);

bst.remove(9); // root
bst.remove(300); // leaf
bst.remove(25); // leaf
bst.remove(7); // single left child
bst.remove(25); // single right child
bst.remove(9); // root
bst.remove(9); // remove non existing
bst.remove(170);
bst.remove(15); // root

var tree = traverse(bst.root);
console.dir(tree, { depth: null });

console.log(bst.lookup(20));
})()
22 changes: 11 additions & 11 deletions src/data-structures/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ class Queue {
}
dequeue() {
if (!this.length) return null;

const first = this.first;
this.first = this.first.next;
this.length--;

if (!this.length) this.last = null;

return first;
}
array() {
const arr = [];
let current = this.first;
while(current) {
while (current) {
arr.push(current.value);
current = current.next;
}
Expand All @@ -49,14 +49,14 @@ class Queue {

class QueueStack {
constructor() {
this.stack = new Stack();
this.stack = new Stack();
}
enqueue(value) {
this.stack.push(value);
}
dequeue() {
if (this.stack.isEmpty()) return null;

const tmp = new Stack();
while (this.stack.length > 1) {
tmp.push(this.stack.pop().value);
Expand Down Expand Up @@ -89,35 +89,35 @@ export function test() {
queue.enqueue(i);
}
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());
console.log("queue: " + queue.array());

console.log("\ndequeue 1 item");
queue.dequeue()
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());
console.log("queue: " + queue.array());

console.log("\ndequeue everything except last");
while (queue.length > 1) {
queue.dequeue();
}
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());
console.log("queue: " + queue.array());

console.log("\ndequeue last");
queue.dequeue()
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());
console.log("queue: " + queue.array());

console.log("\ndequeue empty queue");
queue.dequeue()
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());
console.log("queue: " + queue.array());

console.log("\nenqueue " + n + " items");
queue.enqueue(0);
for (let i = 1; i < 10; i++) {
queue.enqueue(i);
}
console.log("len: " + queue.length);
console.log("queue: "+ queue.array());
console.log("queue: " + queue.array());
}

0 comments on commit b3d87ac

Please sign in to comment.