Binary trees can be stored as a linked list, but they can also be represented as an array (for example, when working with binary heaps).
I didn't see a library that formats binary trees implemented as an array, so I decided to create this very simple library.
npm install format-binary-tree
yarn add format-binary-tree
import formatBinaryTree from 'format-binary-tree';
formatBinaryTree({ values: [1, 2, 3, 4, 5, 6, 7, 8, 9] });
// • 1
// • 3
// • 7
// • 6
// • 2
// • 5
// • 4
// • 9
// • 8
formatBinaryTree({ values, nodeSymbol, indentationSize })
values
is the array of values that represent the binary treenodeSymbol
is the string that prefixes each node - defaults to•
indentationSize
is a number that represents the number of spaces to indent each level - defaults to2
formatBinaryTree
traverses the tree by going root
, right
, left
. I know this isn't a "standard" traversal, but I found it easier to visualize the tree this way.