Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree Traversal Algo addition #1149

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Inorder traversal technique addition
RaviSadam committed Jul 3, 2024

Verified

This commit was signed with the committer’s verified signature.
duvbell duvbell
commit ac69359db2b4dbc628a7cdd2e45f4a5241a38a0d
37 changes: 37 additions & 0 deletions src/algorithms/tree/inorder-traversal/README.en-IN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Inorder Traversal

Inorder traversal is an algorithm for traversing a tree
data structures. One starts at
the root and explores left branch of tree, root node and then right branch of tree


# Inorder Traversal Algorithm

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).

## Inorder Traversal Steps

1. **Start at the root node** of the tree.
2. **Recursively explore the left subtree**:
- Move to the left child of the current node.
- Repeat the inorder traversal on the left subtree.
3. **Visit the root node**:
- Process the current node (e.g., print its value, apply a callback function).
4. **Recursively explore the right subtree**:
- Move to the right child of the current node.
- Repeat the inorder traversal on the right subtree.


## Complexities

### Time Complexity

O(N) Where N is number of nodes in Tree

### Space Complexity

O(H) Where H is height of tree

## References
- [Wikipedia](https://en.wikipedia.org/wiki/Tree_traversal)

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import BinarySearchTreeNode from '../../../../data-structures/tree/binary-search-tree/BinarySearchTreeNode';
import inorderTraversal from '../inoderTraversal';

describe('Inorder Traversal of Binary tree', () => {
it('sholud give inorder travsersal of binary tree', () => {
// creating BST
const bst = new BinarySearchTreeNode(10);

// inserting values to BST
bst.insert(25);
bst.insert(-5);
bst.insert(11);
bst.insert(9);
bst.insert(8);
bst.insert(15);

// callback function
const callback = jest.fn();
inorderTraversal(bst, callback);

// checking number of times function called
expect(callback).toHaveBeenCalledTimes(7);

// checking values
expect(callback.mock.calls[0][0].value).toEqual(-5);
expect(callback.mock.calls[1][0].value).toEqual(8);
expect(callback.mock.calls[2][0].value).toEqual(9);
expect(callback.mock.calls[3][0].value).toEqual(10);
expect(callback.mock.calls[4][0].value).toEqual(11);
expect(callback.mock.calls[5][0].value).toEqual(15);
expect(callback.mock.calls[6][0].value).toEqual(25);
});
});
26 changes: 26 additions & 0 deletions src/algorithms/tree/inorder-traversal/inoderTraversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function inorderTraversalRecursive(root, callback) {
if (!root) {
return;
}
// left branch
inorderTraversalRecursive(root.left, callback);

// callback calling
callback(root);

// right branch

inorderTraversalRecursive(root.right, callback);
}

/**
* --Inorder traversal of binary tree
* @param {BinaryTreeNode} root - Root node of binary tree
* @param {CallableFunction} callback
* - Callback function which calles for each node in inorder traversal
* @returns {void}
*/

export default function inorderTraversal(root, callback) {
inorderTraversalRecursive(root, callback);
}