Skip to content

Commit

Permalink
solve bst inorder tree walk by recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
Zovio committed Aug 30, 2023
1 parent a179dae commit 75ba796
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Binary Search Tree/inorder_tree_walk.js
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,
}
35 changes: 35 additions & 0 deletions Binary Search Tree/inorder_tree_walk.test.js
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);
})
})
});

0 comments on commit 75ba796

Please sign in to comment.