-
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.
solve bst inorder tree walk by recursion
- Loading branch information
Zovio
committed
Aug 30, 2023
1 parent
a179dae
commit 75ba796
Showing
2 changed files
with
46 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function inorderTreeWalkByRecursion(root) { | ||
if (root == null) return; | ||
|
||
inorderTreeWalkByRecursion(root.left); | ||
console.log(root.element); | ||
inorderTreeWalkByRecursion(root.right); | ||
} | ||
|
||
module.exports = { | ||
inorderTreeWalkByRecursion, | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { inorderTreeWalkByRecursion } = require('./inorder_tree_walk'); | ||
const { BinarySearchTree } = require('./BST'); | ||
|
||
describe('Inorder Tree Walk', () => { | ||
let bst; | ||
let consoleLogSpy; | ||
|
||
beforeEach(() => { | ||
consoleLogSpy = jest.spyOn(console, 'log'); | ||
}) | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('By Recursion', () => { | ||
it('should print null', () => { | ||
const bst = new BinarySearchTree(); | ||
|
||
inorderTreeWalkByRecursion(bst.root); | ||
}) | ||
|
||
it('should print out element using console', () => { | ||
const bst = new BinarySearchTree(); | ||
bst.add(2); | ||
bst.add(3); | ||
bst.add(1); | ||
|
||
inorderTreeWalkByRecursion(bst.root); | ||
expect(consoleLogSpy).toHaveBeenNthCalledWith(1, 1); | ||
expect(consoleLogSpy).toHaveBeenNthCalledWith(2, 2); | ||
expect(consoleLogSpy).toHaveBeenNthCalledWith(3, 3); | ||
}) | ||
}) | ||
}); |