Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ac69359

Browse files
committedJul 3, 2024
Inorder traversal technique addition
1 parent 1d2d420 commit ac69359

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
 
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Inorder Traversal
2+
3+
Inorder traversal is an algorithm for traversing a tree
4+
data structures. One starts at
5+
the root and explores left branch of tree, root node and then right branch of tree
6+
7+
8+
# Inorder Traversal Algorithm
9+
10+
Inorder traversal is an algorithm for traversing tree data structures. The algorithm involves visiting each node in a specific order: left branch, root node, and then the right branch. This ensures that the nodes are visited in ascending order for Binary Search Trees (BST).
11+
12+
## Inorder Traversal Steps
13+
14+
1. **Start at the root node** of the tree.
15+
2. **Recursively explore the left subtree**:
16+
- Move to the left child of the current node.
17+
- Repeat the inorder traversal on the left subtree.
18+
3. **Visit the root node**:
19+
- Process the current node (e.g., print its value, apply a callback function).
20+
4. **Recursively explore the right subtree**:
21+
- Move to the right child of the current node.
22+
- Repeat the inorder traversal on the right subtree.
23+
24+
25+
## Complexities
26+
27+
### Time Complexity
28+
29+
O(N) Where N is number of nodes in Tree
30+
31+
### Space Complexity
32+
33+
O(H) Where H is height of tree
34+
35+
## References
36+
- [Wikipedia](https://en.wikipedia.org/wiki/Tree_traversal)
37+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import BinarySearchTreeNode from '../../../../data-structures/tree/binary-search-tree/BinarySearchTreeNode';
2+
import inorderTraversal from '../inoderTraversal';
3+
4+
describe('Inorder Traversal of Binary tree', () => {
5+
it('sholud give inorder travsersal of binary tree', () => {
6+
// creating BST
7+
const bst = new BinarySearchTreeNode(10);
8+
9+
// inserting values to BST
10+
bst.insert(25);
11+
bst.insert(-5);
12+
bst.insert(11);
13+
bst.insert(9);
14+
bst.insert(8);
15+
bst.insert(15);
16+
17+
// callback function
18+
const callback = jest.fn();
19+
inorderTraversal(bst, callback);
20+
21+
// checking number of times function called
22+
expect(callback).toHaveBeenCalledTimes(7);
23+
24+
// checking values
25+
expect(callback.mock.calls[0][0].value).toEqual(-5);
26+
expect(callback.mock.calls[1][0].value).toEqual(8);
27+
expect(callback.mock.calls[2][0].value).toEqual(9);
28+
expect(callback.mock.calls[3][0].value).toEqual(10);
29+
expect(callback.mock.calls[4][0].value).toEqual(11);
30+
expect(callback.mock.calls[5][0].value).toEqual(15);
31+
expect(callback.mock.calls[6][0].value).toEqual(25);
32+
});
33+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function inorderTraversalRecursive(root, callback) {
2+
if (!root) {
3+
return;
4+
}
5+
// left branch
6+
inorderTraversalRecursive(root.left, callback);
7+
8+
// callback calling
9+
callback(root);
10+
11+
// right branch
12+
13+
inorderTraversalRecursive(root.right, callback);
14+
}
15+
16+
/**
17+
* --Inorder traversal of binary tree
18+
* @param {BinaryTreeNode} root - Root node of binary tree
19+
* @param {CallableFunction} callback
20+
* - Callback function which calles for each node in inorder traversal
21+
* @returns {void}
22+
*/
23+
24+
export default function inorderTraversal(root, callback) {
25+
inorderTraversalRecursive(root, callback);
26+
}

0 commit comments

Comments
 (0)
Please sign in to comment.