Today, I finally understood the typical binary tree example for getting started with algorithms.
However, I do not understand the following key points:
1. the following code 1 shows "1 <M", ''1 <D '', and so on. The answer obtained from Baidu is: <It is a shift symbol. So I did a few small experiments and came to the conclusion that K> D <=> k * 2 ^ D. Why? Which knowledge point does this come from? Can someone tell me ...?
2. the binary tree (101st page) Code (the second section of the Code below) in algorithm entry classic, which I feel very confused about.
Original words: If I is given in the question, when I is an odd number, it is the second (I + 1)/2 ball to the left; when I is an even number, it is an I/2 ball to the right. I want to know how the author thinks about this.
The following code is used:# Include <stdio. h> # include <string. h> const int M = 20; int s [1 <M]; // The maximum number of all nodes is 2 ^ M-1int main () {int D, I; while (scanf ("% d", & D, & I) = 2) {memset (s, 0, sizeof (s); // initially open. If true (1) is disabled, false (0) int k, n = (1 <D)-1; // n is the maximum node number for (int I = 0; I <I; I ++) {k = 1; for (;) {s [k] =! S [k]; // switch to change k = s [k]? 2 * k: (2 * k + 1); // turn off the left open right if (k> n) break; // outbound} printf ("% d \ n ", k/2); // The leaf number before the exit} return 0 ;}# Include <iostream> # include <stdio. h> using namespace std; int main () {int D, I; while (scanf ("% d", & D, & I) = 2) // I is the number of small balls {int k = 1; // the value of the first vertex is 1; k Represents the node value for (int I = 0; I <D-1; I ++) if (I % 2) {k = k * 2; I = (I + 1)/2;} // if the ball is an odd number, go to the left root; if it is an even number, then to the right root else {k = k * 2 + 1; I = I/2;} printf ("% d \ n", k );}}