Topic 1113: Two fork tree (binary tree node problem)

Source: Internet
Author: User

Topic 1113: Two fork Trees

Both methods are used to solve the nature of the two-fork tree.

time limit:1 seconds

Memory limit:32 MB

Title Description:


As shown above, by positive integer ... Formed a special binary tree. We know that the last node of this binary tree is n. The question now is how many nodes are included in the subtree where node m is located.

For example, n = 12,m = 3 Then the node in the 13,14,15 and the subsequent nodes are not present, the node m is the subtree of the nodes included in the 3,6,7,12, so the node m in the subtree has 4 nodes.

Input:

The input data includes multiple lines, each of which gives a set of test data, including two integers m,n (1 <= m <= n <= 1000000000). The last set of test data includes two 0, which indicates the end of the input, and this set of data is not processed.

Output:

For each set of test data, the output line contains an integer that gives the number of nodes included in the subtree of the node m.

Sample input:
3 120 0
Sample output:
4
Ideas:

The label of the parent node is I, if the left child exists, then the left child's label is 2*i, if the right child exists, then the right child labeled 2*i+1. According to this nature, I will search one layer at a level,
Without the last layer, the nodes of the current layer can all be added without having to count them all. In fact, the last layer does not have to be counted,
As long as the last layer of the tail node is more than the number of the left child's label, then you can use the tail node subscript cut to the left child's subscript plus 1.

#include <iostream>#include<string.h>#include<stdio.h>using namespacestd;intMain () {intM,n,sum,left,right;  while(SCANF ("%d%d", &m,&n)!=eof&& (m| |N)) {sum=0; if(m==n) sum=1; Else      {          if(m<=n) sum=1; Left=2*m; Right=2*m+1;  while(n>Right ) {Sum+ = (right-left+1); Left*=2; Right=right*2+1; }          if(n>=left) sum=sum+ (n-left) +1; } printf ("%d\n", sum); }    return 0;}

Method 2

#include <iostream>#include<string.h>#include<stdio.h>#include<math.h>using namespacestd;intMain () {Long LongM,n,sum,left,right;  while(SCANF ("%lld%lld", &m,&n)!=eof&& (m| |N)) {sum=0; intI=0; Left=m; Right=m;  while(right<=n) {sum+=int(Pow (2.0, i++)); Right=right*2+1;//Right sub-treeleft*=2;//left dial hand tree      }      //when the next right sub-tree does not exist       while(left<=n) {sum++; Left++; } printf ("%lld\n", sum); }    return 0;} /************************************************************** problem:1113 User:zhuoyuezai language:c++ result:accepted time:0 Ms memory:1608 kb****************************************************************/

Topic 1113: Two fork tree (binary tree node problem)

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.