mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
Postorder traversal for binary tree is added used /agorithms/tree
This commit is contained in:
parent
a422bc6fff
commit
9c0563919c
30
src/algorithms/tree/postorder-traversal/README.en-IN.md
Normal file
30
src/algorithms/tree/postorder-traversal/README.en-IN.md
Normal file
@ -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)
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user