DescTools (version 0.99.13)

BinTree: Binary Tree

Description

Create a binary tree

Usage

BinTree(n)

Arguments

n
integer, size of the tree

Value

  • a vector

Details

If we index the nodes of the tree as 1 for the top, 2--3 for the next horizontal row, 4--7 for the next, ...then the parent-child traversal becomes particularly easy. The basic idea is that the rows of the tree start at indices 1, 2, 4, .... BinTree(13) yields the vector [[8 4 9 2 10 5 11 1 12 6 13 3 7]] meaning that the smallest element will be in position 8 of the tree, the next smallest in position 4, etc.

Examples

Run this code
BinTree(12)

Run the code above in your browser using DataCamp Workspace