Skip to content

Commit

Permalink
Add preorder, inorder and postorder dfs
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <[email protected]>
  • Loading branch information
darkodraskovic committed Sep 9, 2022
1 parent b3d87ac commit 805793e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 14 deletions.
50 changes: 43 additions & 7 deletions spec/binary-search-tree.spec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,61 @@
import {
BinarySearchTree
} from '../src/data-structures/binary-search-tree.js'
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));
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());
});
});

it("BinarySearchTree.bfs does a breadth first search traversal", function () {
const res = [9, 4, 20, 3, 6, 15, 170, 1, 5, 7, 25, 300, 30];
const bst = new BinarySearchTree();
insert.forEach((value) => bst.insert(value));

const bfs = bst.bfs();
expect(bfs).toEqual(res);

const bfsRecursive = bst.bfsRecursive([bst.root], []);
expect(bfsRecursive).toEqual(res);
});

it("BinarySearchTree.dfsInorder traverses bst in order", function () {
const bst = new BinarySearchTree();
insert.forEach((value) => bst.insert(value));

const res = [...new Set(insert)];
res.sort(function (a, b) { return a - b });
const inOrder = bst.traverseInorder(bst.root, []);
expect(inOrder).toEqual(res);
});

it("BinarySearchTree.dfsPreorder traverses bst in preorder", function () {
const bst = new BinarySearchTree();
insert.forEach((value) => bst.insert(value));

const res = [9, 4, 3, 1, 6, 5, 7, 20, 15, 170, 25, 30, 300];
const inOrder = bst.traversePreorder(bst.root, []);
expect(inOrder).toEqual(res);
});

it("BinarySearchTree.dfsPostorder traverses bst in postorder", function () {
const bst = new BinarySearchTree();
insert.forEach((value) => bst.insert(value));

const res = [1, 3, 5, 7, 6, 4, 15, 30, 25, 300, 170, 20, 9];
const inOrder = bst.traversePostorder(bst.root, []);
expect(inOrder).toEqual(res);
});
});
40 changes: 33 additions & 7 deletions src/data-structures/binary-search-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class BinarySearchTree {
}
}
}
return null
return null;
}
lookup(value) {
let current = this.root;
Expand Down Expand Up @@ -124,8 +124,39 @@ export class BinarySearchTree {
}
return list;
}
dfs() {
bfsRecursive(queue, list) {
if (!queue.length) {
return list;
}

let current = queue.shift();
list.push(current.value);

if (current.left) queue.push(current.left);
if (current.right) queue.push(current.right);

return this.bfsRecursive(queue, list);
}

traversePreorder(node, list) {
list.push(node.value);
if (node.left) this.traversePreorder(node.left, list);
if (node.right) this.traversePreorder(node.right, list);
return list;
}

traverseInorder(node, list) {
if (node.left) this.traverseInorder(node.left, list);
list.push(node.value);
if (node.right) this.traverseInorder(node.right, list);
return list;
}

traversePostorder(node, list) {
if (node.left) this.traversePostorder(node.left, list);
if (node.right) this.traversePostorder(node.right, list);
list.push(node.value);
return list;
}
}

Expand All @@ -136,8 +167,3 @@ function traverse(node) {
return tree;
}

(function test() {
const bst = new BinarySearchTree();
var tree = traverse(bst.root);
console.dir(tree, { depth: null });
})()

0 comments on commit 805793e

Please sign in to comment.