Python implementation-full two fork sorting tree root node of the oldest tree looking for three nodes __python

Source: Internet
Author: User

Topic Description:

In a tree full of two. The sort tree depth is k, the node number is 2^k-1, the node value is 1 to (2^k-1), the value of K and any three nodes is given, and the root node of the most young tree containing the three nodes is output.

Sample input: 4 10 15 13

Sample output: 12

First, let's take a look at the two-fork sort tree, which is a 4-story two-fork sort tree:

As can be seen from the diagram above, the sequence traversal of a two-fork sort tree is an increment from 1 to 2^k-1 (k is a layer). So, as long as we give the layer number we can determine the binary sort tree. At the same time, the observation shows that the binary sort tree from the top to bottom of the root node is just a binary lookup of all elements of the middle node.

According to the above rules to solve the three nodes of the most small tree root node of the problem can be the following points:

No need to create a two-fork tree, from the 1-2^k-1 of an ascending array that is a full two-fork sort tree
When the input of three elements on both sides of the two-node, the current binary node is to find the root node of the youngest tree (according to this rule, we do not need to judge three elements, only to determine the maximum element and the minimum element position)
When the value of the minimum node is greater than the value of the two-point node, it continues to look in the right half of the second
When the value of the maximum node is less than the value of the two-point node, it continues to look in the left half of the second part
Based on the following, write Python code as follows:

k, A, b, c = map (int, raw_input (). Split ()) print k,a,b,c MaxValue = max (a,b,c) minvalue = min (a,b , c) left = 1 right = POW (2, K)-1 middle = (left + right)/2 while True:if MinValue < Middle and Middle < Maxvalu E:break if middle < Minvalue:left = middle + 1 middle = (left + right)/2 elif MIDDL E > maxvalue:right = middle-1 middle = (left + right)/2 print middle 
Related Article

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.