Binary Tree Traversal
In computer science, tree traversal (also known as tree search) is a form of graph traversal and refers to the process of visiting (checking and/or updating) each node in a tree data structure, exactly once. Such traversals are classified by the order in which the nodes are visited.
Applications
- Pre-order traversal while duplicating nodes and edges can make a complete duplicate of a binary tree.
- Pre-order traversal can also be used to make a prefix expression (Polish notation) from expression trees: traverse the expression tree pre-orderly.
- In-order traversal is very commonly used on binary search trees because it returns values from the underlying set in order, according to the comparator that set up the binary search tree (hence the name).
- Post-order traversal while deleting or freeing nodes and values can delete or free an entire binary tree.
- Post-order traversal can also generate a postfix representation of a binary tree.
Complexity
- Time: Best :
Average :
Worst :
- Space: Worst:
(recursive), Best:
(iterative)
References
Implementations
Python
-
1
# TODO: Implement in Python
JavaScript
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], ]; const T = [ // mapping to G as a binary tree , [i][0] indicates left child, [i][1] indicates right child [-1, -1], [0, 2], [-1, -1], [1, 4], [-1, -1], [3, 8], [-1, 7], [-1, -1], [6, 10], [-1, -1], [9, -1], ]; let index = 0; function inOrder(root, parent) { if (root === -1) { console.log('No more nodes. Backtracking.'); Tracer.delay(); return; } inOrder(T[root][0], root); inOrder(T[root][1], root); } inOrder(5); // node with key 5 is the root console.log('Finished');
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], ]; const T = [ // mapping to G as a binary tree , [i][0] indicates left child, [i][1] indicates right child [-1, -1], [0, 2], [-1, -1], [1, 4], [-1, -1], [3, 8], [-1, 7], [-1, -1], [6, 10], [-1, -1], [9, -1], ]; let index = 0; function postOrder(root, parent) { if (root === -1) { console.log('No more nodes. Backtracking.'); Tracer.delay(); return; } postOrder(T[root][0], root); console.log(` Going right from ${root}`); Tracer.delay(); postOrder(T[root][1], root); } postOrder(5); // node with key 5 is the root console.log('Finished'); // visualize {
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
const G = [ // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], ]; const T = [ // mapping to G as a binary tree , [i][0] indicates left child, [i][1] indicates right child [-1, -1], [0, 2], [-1, -1], [1, 4], [-1, -1], [3, 8], [-1, 7], [-1, -1], [6, 10], [-1, -1], [9, -1], ]; let index = 0; function preOrder(root, parent) { if (root === -1) { console.log('No more nodes. Backtracking.'); Tracer.delay(); return; } preOrder(T[root][0], root); console.log(` Going right from ${root}`); Tracer.delay(); preOrder(T[root][1], root); } preOrder(5); // node with key 5 is the root console.log('Finished');