Posts

Showing posts from January 5, 2019

Create a full and complete binary tree recursively

Image
0 1 Purpose: I would like to create a full and complete binary tree , which saves a String value in its leaves, recursively. Example 1: Let's say you want to save two Strings in your tree. So you will need a root and two leaves. * <- root node / v0 v1 <- leaves Example 2: Let's say you want to save three Strings in your tree. So you will need a root, two inner Nodes but four leaves, so the tree is still complete. * <- root node / + + <- inner node / / / / v0 v1 v2 v3 <- leaves Math trickery: Its fairly straightforward to calculate the specs for the binary tree. Height: ⌈log2 expectedNumOfStrings⌉ double height = Math.log(expectedNumOfStrings) / Math.log(2); int roundedHeight = (int) Math.ceil(height);