GeezerVison is showing github.com — Added postorder tree traversal · Shayan0915/AlgorithmVisualizer@75115bf
File treeExpand file treeCollapse file tree Open diff view settings algorithm/tree/binary_tree_traversal
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: algorithm/tree/binary_tree_traversal/desc.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 | ], | 17 | 17 | "files": { | 18 | 18 | "inorder": "Traverse Binary Tree Inorder", | | 19 | + "postorder": "Traverse Binary Tree Postorder", | 19 | 20 | "preorder": "Traverse Binary Tree Preorder" | 20 | 21 | } | 21 | 22 | } |
Collapse file Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 ( ' Going right from ' + root )._wait (); | | 16 | + inorder(T[root][1], root); | | 17 | + | | 18 | + logger._print( 'Printing ' + root); | | 19 | + treeTracer._leave ( root ); | | 20 | + arrayTracer._notify ( index++, root )._wait(); | | 21 | +} | | 22 | + | | 23 | +inorder ( 5 ); // node with key 5 is the root | | 24 | +logger._print( 'Finished' ); |
Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 "); |
|