04-Tree 6 complete Binary Search tree

Source: Internet
Author: User

In the beginning, only the middle sequence traversal was discovered from small to large order. have been looking for a complete binary tree layer node between the rules ... gave it up.

Never thought, the law of complete binary tree already knew AH. The root node is I, the left child node 2*i, right child knot point 2*i+1.

Combine both to solve the problem!

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 (< Span class= "katex-html" >< Span class= "Mrel" >≤1000). Then nn distinct Non-negative integer keys is given in the next line. All 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
1#include <iostream>2#include <cstdio>3#include <algorithm>4 using namespacestd;5 6 #defineMaxSize 10057 intSortnum[maxsize] = {0};8 intCbtreenode[maxsize] = {0};9 intCountnum =0;Ten voidCreatcbtree (intRootintN) One { A     if(Root >N) -         return; -     intleft = root *2; the     intright = root *2+1; -Creatcbtree (Left,n);//Middle sequence traversal lgr from small to large -Cbtreenode[root] = sortnum[countnum++]; - Creatcbtree (right,n); + } -  + intMain () A { at     intN; -scanf"%d",&N); -      for(inti =0; i < N; i++)  -scanf"%d",&sortnum[i]); -      -Sort (sortnum,sortnum + N);//sort by from small to large inCreatcbtree (1, N); -      for(inti =1; I <= N; i++) { to         if(I! =N) +printf"%d", Cbtreenode[i]); -         Else theprintf"%d", Cbtreenode[i]); *     } $     return 0;Panax Notoginseng}

04-Tree 6 complete Binary Search tree

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.