@@ -54,8 +54,9 @@ class BinaryTree {
5454 * be read from the bit stream when the read method is called, as there are no children to traverse.
5555 *
5656 * @param array the array to build the binary tree from
57+ * @throws CompressorException if the tree is invalid
5758 */
58- BinaryTree (final int ... array ) {
59+ BinaryTree (final int ... array ) throws CompressorException {
5960 if (array .length == 1 ) {
6061 // Tree only contains a single value, which is the root node value
6162 this .tree = new int [] { array [0 ] };
@@ -64,6 +65,9 @@ class BinaryTree {
6465
6566 // Determine the maximum depth of the tree from the input array
6667 final int maxDepth = Arrays .stream (array ).max ().getAsInt ();
68+ if (maxDepth == 0 ) {
69+ throw new CompressorException ("Tree contains no leaf nodes" );
70+ }
6771
6872 // Allocate binary tree with enough space for all nodes
6973 this .tree = initTree (maxDepth );
@@ -82,6 +86,10 @@ class BinaryTree {
8286 // Add leaf nodes for values with the current depth
8387 for (int value = 0 ; value < array .length ; value ++) {
8488 if (array [value ] == currentDepth ) {
89+ if (numNodesAtCurrentDepth == maxNodesAtCurrentDepth ) {
90+ throw new CompressorException ("Tree contains too many leaf nodes for depth %d" , currentDepth );
91+ }
92+
8593 this .tree [treePos ++] = value ; // Add leaf (value) node
8694 numNodesAtCurrentDepth ++;
8795 }
@@ -109,10 +117,11 @@ class BinaryTree {
109117 *
110118 * @param depth the depth of the tree, must be between 0 and 16 (inclusive)
111119 * @return an array representing the binary tree, initialized with UNDEFINED values
120+ * @throws CompressorException for invalid depth
112121 */
113- private int [] initTree (final int depth ) {
122+ private int [] initTree (final int depth ) throws CompressorException {
114123 if (depth < 0 || depth > 16 ) {
115- throw new IllegalArgumentException ( "Depth must not be negative and not bigger than 16 but is " + depth );
124+ throw new CompressorException ( "Tree depth must not be negative and not bigger than 16 but is " + depth );
116125 }
117126
118127 final int arraySize = depth == 0 ? 1 : (int ) ((1L << depth + 1 ) - 1 ); // Depth 0 has only a single node (the root)
0 commit comments