• 04-树6 Complete Binary Search Tree(30 分)


    A Binary Search Tree (BST) is recursively defined as a binary tree which has 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 be binary search trees.

      A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

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

      Input Specification:

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

      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 be separated by a space, and there must be no extra space at the end of the line.

      Sample Input:

      10
      1 2 3 4 5 6 7 8 9 0
      

      Sample Output:

      6 3 8 1 5 7 9 0 2 4
      
    我的答案:
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <unistd.h>
     4 
     5 int b[2048];
     6 int pos = 0;
     7 
     8 void sort(int *arr, int num)
     9 {
    10     int i, j;
    11     int temp;
    12 
    13     for(i=0;i<num;i++) {
    14         for(j=i;j<num;j++) {
    15             if(arr[i]>arr[j]) {
    16                 temp = arr[j];
    17                 arr[j] = arr[i];
    18                 arr[i] = temp;
    19             }
    20         }
    21     }
    22 }
    23 
    24 void mid_tree(int root, int N, int a[])
    25 {
    26     if(root<=N) {
    27         mid_tree(2*root, N, a);
    28         b[root] =  a[pos++];
    29         mid_tree(2*root+1, N, a);
    30     }
    31 }
    32 
    33 int main()
    34 {
    35     int N, i;
    36     int data[2048];
    37     scanf("%d
    ", &N);
    38     for(i=0;i<N;i++) {
    39         scanf("%d", &data[i]);        
    40     }
    41     sort(data, N);
    42     mid_tree(1, N, data);
    43     for(i=1;i<=N;i++) {
    44         if(i==1) {
    45             printf("%d", b[i]);
    46         } else {
    47             printf(" %d", b[i]);
    48         }
    49     }
    50 
    51     return 0;
    52 }
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    解决 typedef void * POINTER_64 PVOID64; 问题
    短时间内快速获取随机数的方法
    怎样彻底重装IE
    在InstallShield中手动修改XML Files Changes
    Data, Privacy, & ECommerce ISDPE2010 Call for Papers
    DOS命令输出的重定向
    解决:Error spawning 'cmd.exe'
    wuapi 相关文件下载URL
    使用临界区 CRITICAL_SECTION 实现互斥
    修正 IPMSG 2.51 版本中的一点翻译错误
  • 原文地址:https://www.cnblogs.com/ch122633/p/8805778.html
Copyright © 2020-2023  润新知