# prettybt

> Simple binary tree visualizer

Latest version **2.0.0** (published 2023-06-09) · Unlicense license · 0 weekly downloads

## Install

```sh
npm install prettybt
pnpm add prettybt
yarn add prettybt
bun add prettybt
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2023-06-09 |
| First published | 2021-06-01 |
| Weekly downloads | 0 |
| License | Unlicense |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 45.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | ggorlen |
| Maintainers | ggorlen |
| Keywords | binary tree, visualizer |

## Links

- npm: https://www.npmjs.com/package/prettybt
- Repository: https://github.com/ggorlen/prettybt
- Homepage: https://github.com/ggorlen/prettybt#readme
- Issues: https://github.com/ggorlen/prettybt/issues
- npm.io page: https://npm.io/package/prettybt

## Recent versions

- 2.0.0 (latest) — 2023-06-09
- 1.0.4 — 2021-06-11
- 1.0.3 — 2021-06-01
- 1.0.2 — 2021-06-01
- 1.0.1 — 2021-06-01

## README

# prettybt

Simple binary tree visualizer

[Demo](https://ggorlen.github.io/prettybt/)

![binary trees](assets/trees.gif)

## Usage

### Browser

```html
<body>

<!-- always use a specific commit hash -->
<script src="https://cdn.jsdelivr.net/gh/ggorlen/prettybt@f55c8c5/js/pbt.js"></script>
<script>

var canvas = document.createElement("canvas");
document.body.appendChild(canvas);

var tree = {val: 1, left: {val: 2}, right: {val: 3}};
// or: var tree = PrettyBT.treeFromString("[a,b,c,,d]");

PrettyBT.drawBinaryTree(canvas, tree, {size: 17});

</script>
</body>
```

### Making an SVG with [canvas2svg](https://github.com/gliffy/canvas2svg/)

```html
<body>

<script src="https://cdn.jsdelivr.net/gh/gliffy/canvas2svg@eaab317/canvas2svg.js"></script>
<script src="https://cdn.jsdelivr.net/gh/ggorlen/prettybt@f55c8c5/js/pbt.js"></script>
<script>

var tree = PrettyBT.randomTree();
var dimensions = PrettyBT.computeDimensions(tree);
var canvas = (function () {
  var ctx = new C2S(dimensions.width, dimensions.height);
  return {
    getContext: function () {
      return ctx;
    }
  };
})();
PrettyBT.drawBinaryTree(canvas, tree);

var svg = document.createElement("div");
document.body.appendChild(svg);
svg.outerHTML = canvas.getContext().getSerializedSvg();

</script>
</body>
```

### NodeJS with [canvas](https://www.npmjs.com/package/canvas)

```javascript
const {createCanvas} = require("canvas"); // tested with 2.11.2
const PrettyBT = require("prettybt");

const canvas = createCanvas();
const tree = PrettyBT.randomTree();
PrettyBT.drawBinaryTree(canvas, tree);
console.log(`<img src="${canvas.toDataURL()}" />`);
```

---
_Source: https://npm.io/package/prettybt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
