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 17a19d4

Browse files
committed
refactor DFS to accept a callback fn which it calls on the nodes in the specified order
1 parent 683c8fe commit 17a19d4

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

‎dist/datastructures/BinarySearchTree.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,29 @@ class BinarySearchTree {
4949
// found!
5050
return startingNode;
5151
}
52-
DFS(startingNode = this.root, options = { order: 'ascending' }) {
52+
DFS(cbFn, startingNode = this.root, options = { order: 'ascending' }) {
53+
// calls the callback function on the nodes in the specified order
54+
// could reorganize parameterization, little bit too verbose when user wants to use order: 'descending' they also have to pass in the root node
5355
if (startingNode === null)
5456
return null;
55-
const array = [];
56-
const helper = (node = startingNode, options = { order: 'ascending' }) => {
57-
const { order } = options;
57+
const { order } = options;
58+
const helper = (node = startingNode) => {
5859
if (order === 'ascending') {
5960
if (node.left)
6061
helper(node.left);
61-
array.push(node.value);
62+
cbFn(node);
6263
if (node.right)
6364
helper(node.right);
6465
}
6566
if (order === 'descending') {
6667
if (node.right)
67-
helper(node.right,{order: 'descending'});
68-
array.push(node.value);
68+
helper(node.right);
69+
cbFn(node);
6970
if (node.left)
70-
helper(node.left,{order: 'descending'});
71+
helper(node.left);
7172
}
7273
};
73-
helper(startingNode, options);
74-
return array;
74+
helper();
7575
}
7676
logTree(startingNode = this.root, options = { order: 'ascending' }) {
7777
if (!startingNode)

‎dist/datastructures/BinarySearchTree.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,14 @@ describe('depth first search', () => {
9494
});
9595
test('can log a populated tree in ascending order', () => {
9696
populateTree(BST);
97-
expect(BST.DFS()).toEqual([1, 3, 4, 5, 6, 8, 9]);
97+
let ascendingArray = [];
98+
BST.DFS((node) => { ascendingArray.push(node.value); });
99+
expect(ascendingArray).toEqual([1, 3, 4, 5, 6, 8, 9]);
98100
});
99101
test('can log a populated tree in descending order', () => {
100102
populateTree(BST);
101-
console.log(BST.DFS(BST.root, { order: 'descending' }));
102-
expect(BST.DFS(BST.root, { order: 'descending' })).toEqual([9, 8, 6, 5, 4, 3, 1]);
103+
let descendingArray = [];
104+
BST.DFS((node) => { descendingArray.push(node.value); }, BST.root, { order: 'descending' });
105+
expect(descendingArray).toEqual([9, 8, 6, 5, 4, 3, 1]);
103106
});
104107
});

‎src/datastructures/BinarySearchTree.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ describe('depth first search', () => {
8686
})
8787
test('can log a populated tree in ascending order', () => {
8888
populateTree(BST);
89-
expect(BST.DFS()).toEqual([1, 3, 4, 5, 6, 8, 9]);
89+
let ascendingArray: number[] = [];
90+
BST.DFS((node: BinaryTreeNode<number>) => { ascendingArray.push(node.value) });
91+
expect(ascendingArray).toEqual([1, 3, 4, 5, 6, 8, 9]);
9092
})
9193
test('can log a populated tree in descending order', () => {
9294
populateTree(BST);
93-
console.log(BST.DFS(BST.root, { order: 'descending' }));
94-
expect(BST.DFS(BST.root, { order: 'descending' })).toEqual([9, 8, 6, 5, 4, 3, 1]);
95+
let descendingArray: number[] = [];
96+
BST.DFS((node: BinaryTreeNode<number>) => { descendingArray.push(node.value) }, BST.root, { order: 'descending' })
97+
expect(descendingArray).toEqual([9, 8, 6, 5, 4, 3, 1]);
9598
})
9699
})

‎src/datastructures/BinarySearchTree.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,24 @@ class BinarySearchTree<T> {
6060
return startingNode;
6161
}
6262

63-
DFS(startingNode: BinaryTreeNode<T> | null = this.root, options = { order: 'ascending' }): T[] | null {
63+
DFS(cbFn: (node: BinaryTreeNode<T>) => void, startingNode: BinaryTreeNode<T> | null = this.root, options = { order: 'ascending' }) {
64+
// calls the callback function on the nodes in the specified order
65+
// could reorganize parameterization, little bit too verbose when user wants to use order: 'descending' they also have to pass in the root node
6466
if (startingNode === null) return null;
65-
const array: T[] = [];
66-
const helper = (node: BinaryTreeNode<T> = startingNode, options = { order: 'ascending' }): void => {
67-
const { order } = options;
67+
const { order } = options;
68+
const helper = (node: BinaryTreeNode<T> = startingNode): void => {
6869
if (order === 'ascending') {
6970
if (node.left) helper(node.left);
70-
array.push(node.value);
71+
cbFn(node);
7172
if (node.right) helper(node.right);
7273
}
7374
if (order === 'descending') {
74-
if (node.right) helper(node.right,{order: 'descending'});
75-
array.push(node.value);
76-
if (node.left) helper(node.left,{order: 'descending'});
75+
if (node.right) helper(node.right);
76+
cbFn(node);
77+
if (node.left) helper(node.left);
7778
}
7879
};
79-
helper(startingNode, options);
80-
return array;
80+
helper();
8181
}
8282
logTree(startingNode: BinaryTreeNode<T> | null = this.root, options = { order: 'ascending' }): void {
8383
if (!startingNode) return console.log(null);

0 commit comments

Comments
(0)

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