diff --git a/src/algorithms/tree/postorder-traversal/README.en-IN.md b/src/algorithms/tree/postorder-traversal/README.en-IN.md new file mode 100644 index 00000000..0c2fd2de --- /dev/null +++ b/src/algorithms/tree/postorder-traversal/README.en-IN.md @@ -0,0 +1,30 @@ + +# Postorder Traversal Algorithm + +Postorder traversal is an algorithm for traversing tree data structures.The algorithm involves visiting each node in a specific order: left branch, right branch and then root node. + +## Postorder 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 postorder traversal on the left subtree. +3. **Recursively explore the right subtree**: + - Move to the right child of the current node. + - Repeat the postorder traversal on the right subtree. +2. **Visit the root node**: + - Process the current node (e.g., print its value, apply a callback function). + +## 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) + diff --git a/src/algorithms/tree/postorder-traversal/__test__/postorderTraversal.test.js b/src/algorithms/tree/postorder-traversal/__test__/postorderTraversal.test.js new file mode 100644 index 00000000..5c32aefe --- /dev/null +++ b/src/algorithms/tree/postorder-traversal/__test__/postorderTraversal.test.js @@ -0,0 +1,33 @@ +import BinarySearchTreeNode from '../../../../data-structures/tree/binary-search-tree/BinarySearchTreeNode'; +import postorderTraversal from '../postorderTraversal'; + +describe('Postorder Traversal of Binary tree', () => { + it('sholud give postorder 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(); + postorderTraversal(bst, callback); + + // checking number of times function called + expect(callback).toHaveBeenCalledTimes(7); + + // checking values + expect(callback.mock.calls[0][0].value).toEqual(8); + expect(callback.mock.calls[1][0].value).toEqual(9); + expect(callback.mock.calls[2][0].value).toEqual(-5); + expect(callback.mock.calls[3][0].value).toEqual(15); + expect(callback.mock.calls[4][0].value).toEqual(11); + expect(callback.mock.calls[5][0].value).toEqual(25); + expect(callback.mock.calls[6][0].value).toEqual(10); + }); +}); diff --git a/src/algorithms/tree/postorder-traversal/postorderTraversal.js b/src/algorithms/tree/postorder-traversal/postorderTraversal.js new file mode 100644 index 00000000..19f369d5 --- /dev/null +++ b/src/algorithms/tree/postorder-traversal/postorderTraversal.js @@ -0,0 +1,26 @@ +function postorderTraversalRecursive(root, callback) { + if (!root) { + return; + } + + // left branch + postorderTraversalRecursive(root.left, callback); + + // right branch + postorderTraversalRecursive(root.right, callback); + + // callback calling + callback(root); +} + +/** + * --postorder traversal of binary tree + * @param {BinaryTreeNode} root - Root node of binary tree + * @param {CallableFunction} callback + * - Callback function which calles for each node in postorder traversal + * @returns {void} + */ + +export default function postorderTraversal(root, callback) { + postorderTraversalRecursive(root, callback); +}