Algorithm entry-classic-Binary Tree

Source: Internet
Author: User

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 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.