Skip to content

Commit 46695b6

Browse files
committed
Replace checkBalanced function with depthFirstForEach and breadthFirstForEach exercises
1 parent fd6489e commit 46695b6

File tree

4 files changed

+51
-43
lines changed

4 files changed

+51
-43
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
# Sprint Challenge: Data Structures and Algorithms
22

3-
For this sprint challenge, you'll be implementing a few functions that build off of some of the data structures you implemented in the first half of the week. Then you'll be analyzing the runtimes of these methods.
3+
For this sprint challenge, you'll be implementing a few functions that build off of some of the data structures you implemented in the first half of the week. Then you'll be analyzing the runtimes of all of the data structure methods.
44

55
For the algorithms portion of the sprint challenge, you'll be answering the questions posed in the `exercises.pdf` file regarding runtime complexities and algorithmic paradigms.
66

77
## Data Structures
88
Before getting started, run `npm install` in the root-level directory to install needed dependencies for testing.
99

10-
### Task 1. Implement Heapsort
11-
Inside the `src` directory you'll also find the `heap.js` file with the completed code the heap class. Your second task is to implement a sorting method called [heapsort](https://en.wikipedia.org/wiki/Heapsort) that uses the heap data structure in order to sort an array of numbers. Your `heapsort` function should return a new array containing all of the sorted data.
12-
13-
Run `npm test heap` to run the tests for your `heapsort` function to ensure that your implementation is correct.
10+
### Task 1. Implement Depth-First and Breadth-First Traversal on the Binary Search Tree Class
11+
Navigate into the `data_structures` directory. Inside the `src` directory, you'll see the `binary-search-tree.js` file with the completed code for the binary search tree class. Your first task is to implement the methods `depthFirstForEach` and `breadthFirstForEach` on the `BinarySearchTree` class:
1412

15-
### Task 2. Check if a binary search tree is balanced
16-
Navigate into the `data_structures` directory. Inside the `src` directory, you'll see the `binary-search-tree.js` file with the completed code for the binary search tree class. Your first task is to write a separate function (not part of the `BinarySearchTree` class) that receives the root node of an instance of a binary search tree and returns `true` if the binary search tree is perfectly balanced, i.e., all the leaf nodes of the tree reside on the same level. Your function should return `false` if the tree is not perfectly balanced.
13+
* `depthFirstForEach(cb)` receives a callback function as a parameter. This method iterates over the binary search tree in [depth-first](https://en.wikipedia.org/wiki/Depth-first_search) order, applying the supplied callback function to each tree element in turn.
14+
* `breadthFirstForEach(cb)` receives a callback function as a parameter. This method iterates over the binary search tree in [breadth-first](https://en.wikipedia.org/wiki/Breadth-first_search) order, applying the supplied callback function to each tree element in turn.
1715

1816
Run `npm test binary-search-tree` to run the tests for this function to ensure that your implementation is correct.
1917

18+
### Task 2. Implement Heapsort
19+
Inside the `src` directory you'll also find the `heap.js` file with the completed code the heap class. Your second task is to implement a sorting method called [heapsort](https://en.wikipedia.org/wiki/Heapsort) that uses the heap data structure in order to sort an array of numbers. Your `heapsort` function should return a new array containing all of the sorted data.
20+
21+
Run `npm test heap` to run the tests for your `heapsort` function to ensure that your implementation is correct.
22+
2023
### Task 3. Analyze some runtimes
21-
Open up the `Data_Structures_Questions.md` file. This is where you'll jot down your answers for the runtimes of the two functions you just implemented.
24+
Open up the `Data_Structures_Answers.md` file. This is where you'll jot down your answers for the runtimes of the functions you just implemented. Be sure to also answer any other questions posed in the `Answers.md` file!
2225

2326
## Algorithms
2427
For the algorithms portion of the sprint challenge, you'll be answering questions posed in the `exercises.pdf` document inside the `algorithms` directory. Add your answers to the questions in the `Algorithms_Answers.md` file.
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
Add your answers to the questions below.
22

3-
1. What is the runtime complexity of your `checkBalanced` function?
3+
1. What is the runtime complexity of your `depthFirstForEach` method?
44

5-
2. What is the space complexity of your `checkBalanced` function?
5+
2. What is the space complexity of your `depthFirstForEach` function?
66

7-
3. What is the runtime complexity of your `heapsort` function?
7+
3. What is the runtime complexity of your `breadthFirstForEach` method?
88

9-
4. What is the space complexity of the `heapsort` function? Recall that your implementation should return a new array with the sorted data. What would be the space complexity if your function instead altered the input array?
9+
4. What is the space complexity of your `breadthFirstForEach` method?
10+
11+
5. What is the runtime complexity of your `heapsort` function?
12+
13+
6. What is the space complexity of the `heapsort` function? Recall that your implementation should return a new array with the sorted data. What would be the space complexity if your function instead altered the input array?

data_structures/src/binary-search-tree.js

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
const checkBalanced = (rootNode) => {
2-
/* Your code here */
3-
4-
};
5-
61
class BinarySearchTree {
72
constructor(value) {
83
this.value = value;
94
this.left = null;
105
this.right = null;
116
}
127

8+
depthFirstForEach(cb) {
9+
/* Your code here */
10+
11+
}
12+
13+
breadthFirstForEach(cb) {
14+
/* Your code here */
15+
16+
}
17+
1318
insert(value) {
1419
const newNode = new BinarySearchTree(value);
1520
if (value < this.value) {
@@ -59,19 +64,6 @@ class BinarySearchTree {
5964

6065
return max;
6166
}
62-
63-
depthFirstForEach(cb) {
64-
cb(this.value);
65-
if (this.left) {
66-
this.left.depthFirstForEach(cb);
67-
}
68-
if (this.right) {
69-
this.right.depthFirstForEach(cb);
70-
}
71-
}
7267
}
7368

74-
module.exports = {
75-
BinarySearchTree,
76-
checkBalanced,
77-
};
69+
module.exports = BinarySearchTree;
Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
const { BinarySearchTree, checkBalanced } = require('../src/binary-search-tree');
1+
const BinarySearchTree = require('../src/binary-search-tree');
22

33
let bst;
44

5-
describe('checkBalanced', () => {
5+
describe('`depthFirstForEach` and `breadthFirstForEach`', () => {
66
beforeEach(() => {
7-
bst = new BinarySearchTree(10);
7+
bst = new BinarySearchTree(5);
88
});
99

10-
test('checkBalanced returns true given a perfectly balanced tree', () => {
11-
expect(checkBalanced(bst)).toBe(true);
10+
test("`depthFirstForEach` executes a callback on every value in a tree in depth-first order", () => {
11+
const array = [];
12+
const cb = x => array.push(x);
1213

13-
bst.insert(11);
14+
bst.insert(2);
15+
bst.insert(3);
16+
bst.insert(7);
1417
bst.insert(9);
18+
bst.depthFirstForEach(cb);
1519

16-
expect(checkBalanced(bst)).toBe(true);
20+
expect(array).toEqual([5, 2, 3, 7, 9]);
1721
});
1822

19-
test('checkBalanced returns false given a tree that is not perfectly balanced', () => {
20-
bst.insert(11);
21-
bst.insert(12);
22-
expect(checkBalanced(bst)).toBe(false);
23+
test("`breadthFirstForEach` executes a callback on every value in a tree in breadth-first order", () => {
24+
const array = [];
25+
const cb = x => array.push(x);
2326

27+
bst.insert(3);
28+
bst.insert(4);
29+
bst.insert(10);
2430
bst.insert(9);
25-
expect(checkBalanced(bst)).toBe(false);
31+
bst.insert(11);
32+
bst.breadthFirstForEach(cb);
33+
34+
expect(array).toEqual([5, 3, 10, 4, 9, 11]);
2635
});
2736
});

0 commit comments

Comments
 (0)