henrylin03 / odin-binary-search-tree

Implementing balanced binary search tree (BST) from a sorted array in JavaScript, as part of The Odin Project's "Full Stack JavaScript" course
MIT License
0 stars 0 forks source link

implement preorder ( root -> left -> right ) traversal recursively #22

Closed henrylin03 closed 1 month ago

henrylin03 commented 1 month ago

fixes #18

tests

setup in index.mjs:

import createTree from "./createTree.mjs";

const testArray = [1, 2, 7, 4, 23, 8, 9, 10, 4, 3, 5, 7, 9, 67, 6345, 324];
const printData = (node) => console.log(node.data);

const tree = createTree(testArray);

tree.buildTree();
tree.prettyPrint();

// test levelOrder traversal
tree.preOrder(printData); // expected: 8, 3, 1, 2, 5, 4, 7, 23, 9, 10, 324, 67, 6345

output: image