1.0.0 • Published 6 years ago

format-binary-tree v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Greenkeeper badge Build Status codecov npm npm

format-binary-tree

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.

Installation

npm install format-binary-tree
yarn add format-binary-tree

Usage

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

API

formatBinaryTree({ values, nodeSymbol, indentationSize })

  • values is the array of values that represent the binary tree
  • nodeSymbol is the string that prefixes each node - defaults to
  • indentationSize is a number that represents the number of spaces to indent each level - defaults to 2

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.