August 27, 2024

P63 - Construct a complete binary tree.

A complete binary tree with height HH is defined as follows: The levels 1,2,3,…,H−11,2,3,…,H−1 contain the maximum number of nodes (i.e 2i−12i−1 at the level ii, note that we start counting the levels from 1 at the root). 

In level HH, which may contain less than the maximum possible number of nodes, all the nodes are “left-adjusted”.  This means that in a levelorder tree traversal all internal nodes come first, the leaves come second, and empty successors (the Ends which are not really nodes!) come last.

Particularly, complete binary trees are used as data structures (or addressing schemes) for heaps.

We can assign an address number to each node in a complete binary tree by enumerating the nodes in levelorder, starting at the root with number 1. 

In doing so, we realize that for every node XX with address AA the following property holds: The address of XX’s left and right successors are 2∗A2∗A and 2∗A+12∗A+1, respectively, supposed the successors do exist. 

This fact can be used to elegantly construct a complete binary tree structure.  Write a method completeBinaryTree that takes as parameters the number of nodes and the value to put in each node.

scala

scala> Tree.completeBinaryTree(6, "x")
res0: Node[String] = T(x T(x T(x . .) T(x . .)) T(x T(x . .) .))
Be first to comment
Leave a reply