# 04-Tree 6 Complete Binary Search tree (30 points)

Source: Internet
Author: User

04-Tree 6 Complete Binary Search tree (30 points)

A binary Search Tree (BST) is recursively defined as a Binary Tree which have the following properties:

• The left subtree of a node contains only nodes with keys less than the node ' s key.

• The right subtree of a node contains only nodes with keys greater than or equal to the node ' s key.

• Both the left and right subtrees must also is binary search trees.

A complete Binary tree (CBT) was a tree that was completely filled, with the possible exception of the bottom level, which I s filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can being constructed if it is required that the Tre E must also be a CBT. You is supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains the one test case. For each case, the first line contains a positive integerN (≤1000). Then N distinct non-negative integer keys is given in the next line. All of the numbers in a line is separated by a space and is no greater than.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must is separated by a space, and there must is no extra space at the end of the line.

Sample Input:
``101 2 3 4 5 6 7 8 9 0``
Sample Output:
``6 3 8 1 5 7 9 0 2 4``

`usingSystem;usingSystem.Collections.Generic;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Diagnostics;usingSystem.Net;usingSystem.Text;usingSystem.Xml; Public classmynode2{ PublicMyNode2 (strings) {Value=int.    Parse (s); }     PublicMyNode2 (ints) {Value=s; }     PublicMyNode2 left;  PublicMyNode2 right;  Public intValue;}classt{Static voidMain (string[] args) {        //Read Quantity        intCount =int.        Parse (Console.ReadLine ()); //reading a numeric line        varv =Console.ReadLine (); //Digital segmentation put in list and sort        varA = V.split (' '); List<int> ListNum =Newlist<int>(); foreach(varItemincha) {Listnum.add (int.        Parse (item));        } listnum.sort (); //The following is to create a list, put node in, for each node to create the left and right child nodes, but also put the newly created node into the list. //Until the number of node in the list is the same as the number given, endlist<mynode2> list =NewList<mynode2>(); List. ADD (NewMyNode2 (-1)); inti =0;  while(list. Count <count) {List[i]. Left=NewMyNode2 (-1); List. ADD (List[i].            left); if(list. Count <count) {List[i]. Right=NewMyNode2 (-1); List. ADD (List[i].            right); } I++; }         //to create a good tree, assign value, assign value starting from 0        intx =0; Assign Value (list[0],refx); StringBuilder SB=NewStringBuilder (); //The value in node, as the index, output LISTNUM        foreach(varIteminchlist) {sb. Append (Listnum[item. Value]+" "); } Console.WriteLine (sb.) ToString ().        Trim ()); return; }    /// <summary>    ///assign a value in the left and right way, with child nodes, first traverse child nodes/// </summary>    /// <param name= "n" ></param>    /// <param name= "V" >ref for easy value accumulation</param>    Private Static voidAssign value (MyNode2 N,ref intv) {if(N.left! =NULL&& N.left.value = =-1) {Assigned value (N.left,refv); }        if(N.value = =-1) {N.value= v++; }        if(N.right! =NULL&& N.right.value = =-1) {Assigned value (N.right,refv); }    }}`

04-Tree 6 Complete Binary Search tree (30 points)

Related Keywords:

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.