Question: The main challenge in creating a new node will be setting the size of the next array. You can think about the size of the
The main challenge in creating a new node will be setting the size of the next array. You can think about the size of the array as being 1 + [the number of times heads shows up, before the first tails, when flipping a coin]. To simulate the coin flips, when the node is created, we generate a random number and store it in the variable flips. If you consider the value in flips as a binary number, the number of heads will be equal to the number of 1s in the number before the first 0, when reading the number starting at the least significant bit. For example, if the number is 759, the binary representation is 1011110111. Reading from right to left, there is a 1 in the 20s place, a 1 in the 21s place, a 1 in 22s place (i.e. three 1s in a row), before the first 0 in the 23s place. Thus, the length of the next array, if this was the random number, would be 1+3 = 4. Note that you should ensure that the length of the array does not exceed max_levels. NOTES: - Do not convert the number flips to a string containing binary numbers. - The only operators that you are allowed to do on expressions involving flips are: == test for equality << bitwise left shift <<= bitwise left shift and assignment >> bitwise right shift >>= bitwise right shift and assignment & bitwise and | bitwise or ^ bitwise exclusive-or - DO NOT use the % operator, the / operator, or any other operators not listed above
I am trying to generate a random number with these conditions
can you show me how to do this? I will rate and then leave you a comment.
private class Node { Node[] next; int data; public Node(int data) { this(data,0); } public Node(int data, int level) { if (level == 0) { int flips = random.nextInt(); level = 1;
// TODO: increment level by the number of 1's in flips before the // first 0, when reading flips as a binary number from right to left please write your steps here } this.data = data; next = new Node[level]; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
