Console
//This example Renders the following tree, //and prints different transversals to the console //Copyright (c) 2015 Richard Infante. //Uses d3.js to render svgs (d3js.org) //This is the Ace text editor (https://ace.c9.io/) var tree = { value : "A", left: { value : "B", right: { value: "C" }, left: { value: "D" } }, right: { value: "E", right: { value: "G", }, left: { value: "F", } } } function preorder(tree, array){ if(array === undefined){ array = []; } if(tree === undefined){ return; } array.push(tree.value); preorder(tree.left, array); preorder(tree.right, array); } function inorder(tree, array){ if(array === undefined){ array = []; } if(tree === undefined){ return; } inorder(tree.left, array); array.push(tree.value); inorder(tree.right, array); } function postorder(tree, array){ if(array === undefined){ array = []; } if(tree === undefined){ return; } postorder(tree.left, array); postorder(tree.right, array); array.push(tree.value); } var preorderTransversal = []; var inorderTransversal = []; var postorderTransversal = []; preorder(tree, preorderTransversal); inorder(tree, inorderTransversal); postorder(tree, postorderTransversal); console.log("Preorder:",preorderTransversal); console.log("Inorder:",inorderTransversal); console.log("Postorder:",postorderTransversal); return tree;