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 a5fa14a

Browse files
Implemented DF Traversals & BST Validator (Closes #11)
1 parent ef35827 commit a5fa14a

File tree

7 files changed

+247
-17
lines changed

7 files changed

+247
-17
lines changed

‎Algorithms/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99
- [X] Bubble Sort
1010
- [X] Selection Sort
1111
- [X] Insertion Sort
12-
- [] Searching
13-
- [] Linear Search
12+
- [X] Searching
13+
- [X] Linear Search
1414
- [ ] Binary Search
15-
- [] Breadth First Search (BFS)
16-
- [] Depth First Search (DFS)
15+
- [X] Breadth First Search (BFS)
16+
- [X] Depth First Search (DFS)
1717

1818
## Resources
1919
- [Visualizing Data Structures & Algorithms](https://visualgo.net/en)
2020
- [Unicode Characters | RapidTables](https://www.rapidtables.com/code/text/unicode-characters.html)
2121
- [The Big-O Algorithm Complexity Cheat Sheet](https://www.bigocheatsheet.com/ "Big O Cheat Sheet")
2222

2323
### Searching
24+
- [Graph Traversals: VisuAlgo](https://visualgo.net/en/dfsbfs)
25+
- [Tree Traversals: Preorder, Inorder, & Postorder](https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/)
2426
- [Space Complexity: BFS vs DFS](https://stackoverflow.com/questions/9844193/what-is-the-time-and-space-complexity-of-a-breadth-first-and-depth-first-tree-tr)
2527

2628
### Sorting

‎Algorithms/Recursion/BreadthFirstSearch.ts renamed to ‎Algorithms/Searching/BreadthFirstTraversal.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import BST from '../../Data-Structures/Trees/BinarySearchTree.ts';
22
import Node from '../../Data-Structures/Trees/BinaryTreeNode.ts';
33
import Queue from '../../Data-Structures/Sequential/Queue.ts';
44

5-
function recursiveBFS(nodeQueue: Queue<Node>, nodesTraversed: Array<number>): Array<number> {
5+
function recursiveBFT(nodeQueue: Queue<Node>, nodesTraversed: Array<number>): Array<number> {
66
if (nodeQueue.getLength() === 0) return nodesTraversed;
77

88
let currentNode: Node = nodeQueue.dequeue();
99
nodesTraversed.push(currentNode.getValue());
1010
if (currentNode.hasLeft()) nodeQueue.enqueue(currentNode.getLeft());
1111
if (currentNode.hasRight()) nodeQueue.enqueue(currentNode.getRight());
1212

13-
return recursiveBFS(nodeQueue, nodesTraversed);
13+
return recursiveBFT(nodeQueue, nodesTraversed);
1414
}
1515

16-
function breadthFirstSearch(tree: BST) {
16+
function breadthFirstTraversal(tree: BST) {
1717
const root = tree.getRoot();
1818

1919
if (!root) return false;
@@ -23,7 +23,7 @@ function breadthFirstSearch(tree: BST) {
2323

2424
nodeQueue.enqueue(root);
2525

26-
return recursiveBFS(nodeQueue, nodesTraversed);
26+
return recursiveBFT(nodeQueue, nodesTraversed);
2727
}
2828

2929

@@ -46,9 +46,9 @@ if (import.meta.main) {
4646
tree.insert(15);
4747
tree.insert(1);
4848

49-
console.log(breadthFirstSearch(tree));
49+
console.log(breadthFirstTraversal(tree));
5050

51-
// RUN: deno run Algorithms/Recursion/BreadthFirstSearch.ts
51+
// RUN: deno run Algorithms/Searching/BreadthFirstTraversal.ts
5252
}
5353

5454
// --------------------------- Terminal Output: ---------------------------
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import BST from '../../Data-Structures/Trees/BinarySearchTree.ts';
2+
import Node from '../../Data-Structures/Trees/BinaryTreeNode.ts';
3+
4+
5+
export function inorderDFT(tree: BST) {
6+
const root = tree.getRoot();
7+
if (!root) return false;
8+
9+
const nodesTraversed: number[] = [];
10+
11+
return traverseInOrder(root, nodesTraversed);
12+
13+
/**
14+
* Recursive DFS on Binary Search Tree via inorder traversal
15+
* @param node Binary node to traverse
16+
* @param nodesTraversed Array of all nodes reached inorder
17+
*/
18+
function traverseInOrder(node: Node, nodesTraversed: Array<number>) {
19+
if (node.hasLeft()) traverseInOrder(node.getLeft(), nodesTraversed);
20+
nodesTraversed.push(node.getValue());
21+
if (node.hasRight()) traverseInOrder(node.getRight(), nodesTraversed);
22+
return nodesTraversed;
23+
}
24+
}
25+
26+
export function preorderDFT(tree: BST) {
27+
const root = tree.getRoot();
28+
if (!root) return false;
29+
30+
const nodesTraversed: number[] = [];
31+
32+
return traversePreOrder(root, nodesTraversed);
33+
34+
/**
35+
* Recursive DFS on Binary Search Tree via preorder traversal
36+
* @param node Binary node to traverse
37+
* @param nodesTraversed Array of all nodes reached preorder
38+
*/
39+
function traversePreOrder(node: Node, nodesTraversed: Array<number>) {
40+
nodesTraversed.push(node.getValue());
41+
if (node.hasLeft()) traversePreOrder(node.getLeft(), nodesTraversed);
42+
if (node.hasRight()) traversePreOrder(node.getRight(), nodesTraversed);
43+
return nodesTraversed;
44+
}
45+
}
46+
47+
export function postorderDFT(tree: BST) {
48+
const root = tree.getRoot();
49+
if (!root) return false;
50+
51+
const nodesTraversed: number[] = [];
52+
53+
return traversePostOrder(root, nodesTraversed);
54+
55+
/**
56+
* Recursive DFS on Binary Search Tree via postorder traversal
57+
* @param node Binary node to traverse
58+
* @param nodesTraversed Array of all nodes reached postorder
59+
*/
60+
function traversePostOrder(node: Node, nodesTraversed: Array<number>) {
61+
if (node.hasLeft()) traversePostOrder(node.getLeft(), nodesTraversed);
62+
if (node.hasRight()) traversePostOrder(node.getRight(), nodesTraversed);
63+
nodesTraversed.push(node.getValue());
64+
return nodesTraversed;
65+
}
66+
}
67+
68+
69+
//---------------------------------------------------------------------
70+
// ---------- MAIN PROGRAM ----------
71+
//---------------------------------------------------------------------
72+
if (import.meta.main) {
73+
74+
const tree = new BST();
75+
76+
// 9
77+
// 4 20
78+
// 1 6 15 170
79+
80+
tree.insert(9);
81+
tree.insert(4);
82+
tree.insert(6);
83+
tree.insert(20);
84+
tree.insert(170);
85+
tree.insert(15);
86+
tree.insert(1);
87+
88+
console.log('Inorder:', inorderDFT(tree));
89+
console.log('Preorder:', preorderDFT(tree));
90+
console.log('Postorder:', postorderDFT(tree));
91+
92+
// RUN: deno run Algorithms/Searching/DepthFirstTraversals.ts
93+
}
94+
95+
// --------------------------- Terminal Output: ---------------------------

‎Data-Structures/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- [AVL Tree Visualization](https://www.cs.usfca.edu/~galles/visualization/AVLtree.html)
3535
- [Red-Black Tree Visualization](https://www.cs.usfca.edu/~galles/visualization/RedBlack.html)
3636
- [Priority Queue Implementation: GeeksForGeeks](https://www.cs.usfca.edu/~galles/visualization/RedBlack.html)
37+
- [Validate a BST: GeeksForGeeks](https://www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/)
3738

3839
### Hash Tables
3940
- [MD5 Hash Generator](http://www.miraclesalad.com/webtools/md5.php)

‎Data-Structures/Trees/BinarySearchTree.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export default class BinarySearchTree {
206206
node.setRight(left);
207207
}
208208

209-
public breadthFirstSearch() {
209+
public breadthFirstTraversal() {
210210
if (!this.root) return false;
211211

212212
let currentNode = this.root;
@@ -252,7 +252,7 @@ if (import.meta.main) {
252252
tree.insert(170);
253253
tree.insert(15);
254254
tree.insert(1);
255-
console.log(tree.breadthFirstSearch());
255+
console.log(tree.breadthFirstTraversal());
256256
// console.log('Tree: ', JSON.stringify(BinarySearchTree.traverse(tree.getRoot())));
257257
tree.remove(20);
258258
printNode(tree, 4);
@@ -263,7 +263,7 @@ if (import.meta.main) {
263263
tree.invertTree();
264264
console.log('Inverse Tree: ', JSON.stringify(BinarySearchTree.traverse(tree.getRoot())));
265265

266-
console.log(tree.breadthFirstSearch());
266+
console.log(tree.breadthFirstTraversal());
267267

268268
// RUN: deno run Data-Structures/Trees/BinarySearchTree.ts
269269
}

‎Playground/Challenges/ValidatorBST.ts

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import BST from '../../Data-Structures/Trees/BinarySearchTree.ts';
2+
import Node from '../../Data-Structures/Trees/BinaryTreeNode.ts';
3+
4+
export default class BinarySearchTreeValidator {
5+
private static previousVal: number | undefined = undefined;
6+
7+
private static _resetValidator() {
8+
this.previousVal = undefined;
9+
}
10+
11+
public static validate(tree: BST, visualize?: boolean): boolean {
12+
const root = tree.getRoot();
13+
if (!root) return false;
14+
15+
visualize = visualize || false;
16+
17+
const valid: boolean = this._traverseInOrder(root, visualize);
18+
19+
this._resetValidator();
20+
21+
return valid;
22+
}
23+
24+
private static _traverseInOrder(node: Node, visualize: boolean): boolean {
25+
if (node.hasLeft()) this._traverseInOrder(node.getLeft(), visualize);
26+
27+
const currentValue = node.getValue();
28+
if (visualize) console.log('\tCurrent Value:', node.getValue(), '\tPrevious:', this.previousVal);
29+
30+
if (!this.previousVal) this.previousVal = currentValue;
31+
else if (currentValue < this.previousVal) return false;
32+
else this.previousVal = currentValue;
33+
34+
if (node.hasRight()) this._traverseInOrder(node.getRight(), visualize);
35+
36+
return true;
37+
}
38+
}
39+
40+
function printBSTValidation(tree: BST, treeLabel: string) {
41+
console.log(treeLabel, '– Original:', BinarySearchTreeValidator.validate(tree, true));
42+
}
43+
44+
45+
46+
//---------------------------------------------------------------------
47+
// ---------- MAIN PROGRAM ----------
48+
//---------------------------------------------------------------------
49+
if (import.meta.main) {
50+
51+
const tree1 = new BST();
52+
tree1.insert(9);
53+
tree1.insert(4);
54+
tree1.insert(6);
55+
tree1.insert(20);
56+
tree1.insert(170);
57+
tree1.insert(15);
58+
tree1.insert(1);
59+
60+
// 9
61+
// 4 20
62+
// 1 6 15 170
63+
printBSTValidation(tree1, 'Tree 1');
64+
65+
66+
tree1.invertTree();
67+
// 9
68+
// 20 4
69+
// 170 15 6 1
70+
printBSTValidation(tree1, 'Tree 1');
71+
72+
73+
const tree2 = new BST();
74+
tree2.insert(16);
75+
tree2.insert(8);
76+
tree2.insert(32);
77+
78+
printBSTValidation(tree2, 'Tree 2');
79+
tree2.invertTree();
80+
printBSTValidation(tree2, 'Tree 2');
81+
82+
83+
const tree3 = new BST();
84+
tree3.insert(48);
85+
tree3.insert(29);
86+
tree3.insert(77);
87+
tree3.insert(18);
88+
tree3.insert(16);
89+
tree3.insert(68);
90+
tree3.insert(72);
91+
tree3.insert(83);
92+
93+
printBSTValidation(tree3, 'Tree 3');
94+
tree3.invertTree();
95+
printBSTValidation(tree3, 'Tree 3');
96+
97+
// RUN: deno run Playground/Challenges/ValidatorBST.ts
98+
}
99+
100+
// --------------------------- Terminal Output: ---------------------------
101+
// Current Value: 1 Previous: undefined
102+
// Current Value: 4 Previous: 1
103+
// Current Value: 6 Previous: 4
104+
// Current Value: 9 Previous: 6
105+
// Current Value: 15 Previous: 9
106+
// Current Value: 20 Previous: 15
107+
// Current Value: 170 Previous: 20
108+
// Tree 1 – Original: true
109+
// Current Value: 170 Previous: undefined
110+
// Current Value: 20 Previous: 170
111+
// Current Value: 9 Previous: 170
112+
// Tree 1 – Original: false
113+
// Current Value: 8 Previous: undefined
114+
// Current Value: 16 Previous: 8
115+
// Current Value: 32 Previous: 16
116+
// Tree 2 – Original: true
117+
// Current Value: 32 Previous: undefined
118+
// Current Value: 16 Previous: 32
119+
// Tree 2 – Original: false
120+
// Current Value: 16 Previous: undefined
121+
// Current Value: 18 Previous: 16
122+
// Current Value: 29 Previous: 18
123+
// Current Value: 48 Previous: 29
124+
// Current Value: 68 Previous: 48
125+
// Current Value: 72 Previous: 68
126+
// Current Value: 77 Previous: 72
127+
// Current Value: 83 Previous: 77
128+
// Tree 3 – Original: true
129+
// Current Value: 83 Previous: undefined
130+
// Current Value: 77 Previous: 83
131+
// Current Value: 48 Previous: 83
132+
// Tree 3 – Original: false

‎README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
- [X] Comparison Sorting
2323
- [X] *Merge Sort*
2424
- [X] *Quicksort*
25-
- [] Searching
26-
- [] Linear Search
27-
- [] Binary Search
28-
- [] BFS & DFS
25+
- [X] Searching
26+
- [X] Linear Search
27+
- [X] Binary Search
28+
- [X] BFS & DFS
2929

3030
## Environment
3131
- Deno 1.2.1

0 commit comments

Comments
(0)

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