-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preorder, inorder and postorder dfs
Signed-off-by: Darko Draskovic <[email protected]>
- Loading branch information
1 parent
b3d87ac
commit 805793e
Showing
2 changed files
with
76 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters