| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,8 @@ | |||
| 15 | 15 | "tree": { | |
| 16 | 16 | "name": "Tree", | |
| 17 | 17 | "list": { | |
| 18 | - "binary_search_tree": "Binary Search Tree" | ||
| 18 | + "binary_search_tree": "Binary Search Tree", | ||
| 19 | + "binary_tree_traversal": "Binary Tree Traversal" | ||
| 19 | 20 | } | |
| 20 | 21 | }, | |
| 21 | 22 | "number_theory": { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,20 @@ | |||
| 1 | + { | ||
| 2 | + "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.", | ||
| 3 | + "Applications": [ | ||
| 4 | + "Pre-order traversal while duplicating nodes and edges can make a complete duplicate of a binary tree.", | ||
| 5 | + "Pre-order traversal can also be used to make a prefix expression (Polish notation) from expression trees: traverse the expression tree pre-orderly.", | ||
| 6 | + "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).", | ||
| 7 | + "Post-order traversal while deleting or freeing nodes and values can delete or free an entire binary tree.", | ||
| 8 | + "Post-order traversal can also generate a postfix representation of a binary tree." | ||
| 9 | + ], | ||
| 10 | + "Complexity": { | ||
| 11 | + "time": "Best : O(N) Average : O(logN) Worst : O(N)", | ||
| 12 | + "space": "Worst: O(N) (recursive), Best: O(1) (iterative)" | ||
| 13 | + }, | ||
| 14 | + "References": [ | ||
| 15 | + "<a href='https://en.wikipedia.org/wiki/Tree_traversal'>Wikipedia</a>" | ||
| 16 | + ], | ||
| 17 | + "files": { | ||
| 18 | + "inorder": "Traverse Binary Tree Inorder" | ||
| 19 | + } | ||
| 20 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + var index = 0; | ||
| 2 | + | ||
| 3 | + function inorder ( root , parent ) { | ||
| 4 | + if (root === -1) { | ||
| 5 | + logger._print( 'No more nodes. Backtracking.' )._wait (); | ||
| 6 | + return; | ||
| 7 | + } | ||
| 8 | + | ||
| 9 | + logger._print( 'Reached ' + root); | ||
| 10 | + treeTracer._visit ( root , parent )._wait (); | ||
| 11 | + | ||
| 12 | + logger._print ( ' Going left from ' + root )._wait (); | ||
| 13 | + inorder(T[root][0], root); | ||
| 14 | + | ||
| 15 | + logger._print( 'Printing ' + root); | ||
| 16 | + treeTracer._leave ( root ); | ||
| 17 | + arrayTracer._notify ( index++, root )._wait(); | ||
| 18 | + | ||
| 19 | + logger._print ( ' Going right from ' + root )._wait (); | ||
| 20 | + inorder(T[root][1], root); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + inorder ( 5 ); // node with key 5 is the root | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,32 @@ | |||
| 1 | + var G = [ // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not | ||
| 2 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 3 | + [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 4 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 5 | + [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0], | ||
| 6 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 7 | + [0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0], | ||
| 8 | + [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], | ||
| 9 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | + [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1], | ||
| 11 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 12 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0] | ||
| 13 | + ]; | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + var T = [ // mapping to G as a binary tree , [i][0] indicates left child, [i][1] indicates right child | ||
| 17 | + [-1,-1], | ||
| 18 | + [ 0, 2], | ||
| 19 | + [-1,-1], | ||
| 20 | + [ 1, 4], | ||
| 21 | + [-1,-1], | ||
| 22 | + [ 3, 8], | ||
| 23 | + [-1, 7], | ||
| 24 | + [-1,-1], | ||
| 25 | + [ 6,10], | ||
| 26 | + [-1,-1], | ||
| 27 | + [ 9,-1] | ||
| 28 | + ]; | ||
| 29 | + | ||
| 30 | + var treeTracer = new DirectedGraphTracer( " Binary Tree Traversal Inorder ")._setTreeData ( G, 5 ); | ||
| 31 | + var arrayTracer = new Array1DTracer( " Binary Tree Print Inorder ")._setData ( new Array(T.length).fill( '-' ) ); | ||
| 32 | + var logger = new LogTracer ( " Log "); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,4 +21,4 @@ const refineBoolean = (bool) => { | |||
| 21 | 21 | return bool ? 'T' : 'F'; | |
| 22 | 22 | }; | |
| 23 | 23 | ||
| 24 | - module.exports = refineByType; | ||
| 24 | + module.exports = refineByType; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments