PAT甲级-1064 Complete Binary Search Tree(30分)

PAT甲级-1064 Complete Binary Search Tree(30分)

1064 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

题目大意: 给定串序列,用这串数列构成一课完全二叉搜索树,层序遍历输出这棵树。
分析: 先将这串数列由小到大排序,由于是完全二叉树,所以可以计算出其左孩子结点的总个数(我用递归方式求左孩子结点总个数,姥姥利用完全二叉树的规律来求,感觉直接递归更方便一些,不用去想规律,而且似乎更牛逼??),这样就可以在排好序的数列中找到第一个结点填入根,依次类推。
例如: 题目给的样例,一共十个结点,所以根的左边一定是6个结点,右边是3个结点,所以排好序的数列中的第7个(下标从1开始)元素就是根结点中的数据。于是递归求解左孩子和右孩子,求出这个树,最后层序遍历输出。

AC代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std ;

int childeNum(int N,int x){ //N是序列的长度,如题目中的10,x是结点下标 
	if(x>N)    //在下面调用该函数过程中,x始终是2,因为1的左孩子下标为2 
	   return 0 ;
    else{
    	return childeNum(N,x*2) + childeNum(N,x*2+1)+1 ;
	}
}

/*Tree是已经排好序的数列,front是改序列的第一个数下标,reer是最后一个数下标
 *S是要填充的数组,即真正要求的树 ,用数组来存储是因为该树是完全二叉树 
 *x是要填入到S中的下标位置 ,初始值为1,x*2便是他左孩子下标,x*2+1是他右孩子下标 
*/ 
void printNode(vector<int> Tree,int front,int reer,int *S,int x){
	int length = reer - front +1 ; //计算长度 
	if(length < 1) //如果长度小于1,就是没有结点,直接return  
	    return  ;

	//这里第二个参数一定是2,因为我是按照最小下标为1开始,又1本身不算,于是就是从1的左孩子2开始算 
	int leftchildren = childeNum(length,2) ; //计算该结点有几个左孩子 
	int rightchidren = length-leftchildren-1 ;//计算右孩子总结点个数 
	S[x] = Tree[leftchildren+front] ;//,将Tree中对应值填入到S中 
	printNode(Tree,front,front+leftchildren-1,S,x*2) ;//递归左孩子 
	printNode(Tree,front+leftchildren+1,reer,S,x*2+1) ;//递归右孩子 
}
int main(){
	int N ;
	vector<int> in ;
	in.push_back(2500) ;
	cin >> N ;
	for(int i=0;i<N;i++){
		int n ;
		cin >> n ;
		in.push_back(n) ;
	}
	sort(in.begin()+1,in.end()) ;
	int *S = new int[N+1] ; //真正的要求的树 
	printNode(in,1,N,S,1) ;//填充S,即将数据填入到S中 
	for(int i=1;i<N+1;i++){
		if(i!=1)    //因为是用数组里存的完全二叉树,所以顺序输出就是他的层序遍历 
		cout << " " ;
	    cout << S[i] ;
     }
	return 0 ;
}

2018-10-05

猜你喜欢

转载自blog.csdn.net/weixin_42780784/article/details/82945760