04-树5. Complete Binary Search Tree (30)

来源:互联网 发布:万网域名证书生成器 编辑:程序博客网 时间:2024/06/07 03:47

04-树5. Complete Binary Search Tree (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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:
101 2 3 4 5 6 7 8 9 0
Sample Output:
6 3 8 1 5 7 9 0 2 4
这道题真的是花了很多时间未果,最后还是看了姥姥的讲解才豁然开朗。
思路:
   首先使用数组存储树结点,因为这是一个完全二叉树,并不会造成空间上的浪费还能避免指针的使用,既安全又省时。最初使用的是vector来建立数组,但是vector建立数组的下标不能为-1,恕我愚钝最后还是使用了new。
   显而易见的是根结点似乎是序列当中靠近中间的位置,这里的序列自然是经过从小到大排序之后得到的序列。问题是要知道根结点在序列中的位置,通过完全二叉树的性质(1.其子树也是完全二叉树 2.树结点总数的计算公式)既可知道根结点左子树有多少个结点,自然也就知道了根结点在序列中的位置。于是这样之后,问题就变成了两个规模小一点的问题,递归便可得出答案。
代码:
<pre name="code" class="cpp">#include<iostream>//#include<vector>#include<cmath>using namespace std;void Build_BCT(int *A, int *T, int ALeft, int ARight, int TRoot);int GetLeftLength(int n);int main(int argc, char *argv[]){int N;cin >> N;int *A = new int[N];for (int i = 0; i < N; i++){cin >> A[i];}for (int i = 1; i < N; i++)//插入法排序{int tmp = A[i];int j = i - 1;while (A[j]>tmp&&j >= 0){A[j + 1] = A[j];j--;}A[j + 1] = tmp;}int *T = new int[N];Build_BCT(A, T, 0, N - 1, 0);//建树for (int i = 0; i < N; i++){cout << T[i];if (i == N - 1)cout << endl;elsecout << " ";}system("pause");return 0;}void Build_BCT(int *A, int *T, int ALeft, int ARight, int TRoot){int n;n = ARight - ALeft + 1;if (n == 0)return;int L;L = GetLeftLength(n);<span style="font-family: 'Droid Sans Mono', Consolas, 'Courier New', monospace; font-size: 1em;">//获取左子树结点数</span>T[TRoot] = A[ALeft + L];int LeftTRoot, RightTRoot;LeftTRoot = TRoot * 2 + 1;RightTRoot = LeftTRoot + 1;Build_BCT(A, T, ALeft, ALeft + L - 1, LeftTRoot);Build_BCT(A, T, ALeft + L + 1, ARight, RightTRoot);}int GetLeftLength(int n){int H;H = log(n + 1) / log(2);int X;X = n + 1 - pow(2, H);if (X > pow(2, H - 1)){X = pow(2, H - 1);}int L;L = pow(2, H - 1) - 1 + X;return L;}


0 0
原创粉丝点击