Search in a Binary search Tree (25)

Source: Internet
Author: User

What is the rule of the path in the binary lookup tree?

Let's analyze it once.

Input1 4 2 3At the outset, we define a range that can accommodate [0, INFINITE)//Infinite means infinityread in 1 First, then read into the first number is 1,4>1>0, the range narrows to [1, INFINITE) The previous number that was read into 2,2 was 4,2<4<infinite, so the interval range is reduced to [1,4] again read into the 3,3 the previous number is 2,3>2>1, so the interval range is reduced to [2,4] There are no numbers behind, output yesinput:1 4 2 5 3read in 1, read in 4,4>1>0Interval1, INFINITE) read in 2,2<4<infinite, Interval [1,4] read in 5,5>2>1Interval2,4]      !but 5 no longer belongs to this range! After the input is discarded, output no

Wit a little, the beginning of the interval to take [1,max] is ok = =

Done!

A variety of boundary conditions also to test good OH =

#include <iostream>using namespacestd;intMain () {intt, N, Min, Max, Oldnum, Newnum; /*t Number of tests n number of each time * min current interval min max current interval Max * Oldnum previous one obtained number Newnum*/Cin>> T >>N;  for(intj =0; j<t; J + +) {min=1, max =N; CIN>>Oldnum; if(n = =1) {cout<<"YES"<<Endl; Continue; }         for(inti =0; I < n-1; i++) {cin>>Newnum; if(Newnum>oldnum && oldnum>min) {min=Oldnum; }            Else if(Newnum < Oldnum && Oldnum <max) {Max=Oldnum; } oldnum=Newnum; if(min <= newnum && newnum <=max) {                if(i = = N-2) {cout<<"YES"<<Endl; }                Continue; }            Else{cout<<"NO"<<Endl;  while(GetChar ()! ='\ n');  Break; }        }    }}

Search in a Binary search Tree (25)

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.