04-树6 Complete Binary Search Tree
来源:互联网 发布:m20火箭筒数据 编辑:程序博客网 时间:2024/06/01 14:01
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
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 integerN (≤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
思路:
1,题目意思是 给你一组数据,其中的数字互不相同,然后你构造出一个颗二叉查找树,且这个树还要是完全二叉树
2,根据查找树的有序性质,中序遍历的结果是从小到大排列的,因此把这组数据排序即得到中序遍历序列,然后就是递归求根节点,把整颗树构造出来
3,这里的递归构造要利用的条件是,完全二叉树的结构性质。
#include <iostream>#include <vector>#include <cmath>#include <algorithm>#include <queue>using namespace std;typedef struct TNode* BSTree;struct TNode{ BSTree left; BSTree right; int data;};vector<int> a;int rootindex(int left, int right){ int index; int totol = right -left +1; int depth = (int)(log2(totol+1)-1); int bottom = totol - exp2(depth+1)+1; if(bottom <= exp2(depth)) index = (totol - bottom)/2 + bottom; else index = (totol - bottom)/2 + exp2(depth); return left+index;}BSTree findroot(int left, int right){ if(left<=right) { int root = rootindex(left,right); BSTree rootnode = new TNode; rootnode->data = a[root]; rootnode->left = findroot(left,root-1); rootnode->right = findroot(root+1,right); return rootnode; } else return NULL;}void levelOrderTraverse(BSTree t){ queue<BSTree> q; q.push(t); a.clear(); while(!q.empty()) { BSTree tmp = q.front(); a.push_back(tmp->data); q.pop(); if(tmp->left!=NULL) q.push(tmp->left); if(tmp->right!=NULL) q.push(tmp->right); }}void printvec(vector<int> v){ for(int i=0;i<(v.size()-1);i++) { cout<<v[i]<<' '; } cout<<v[v.size()-1];}int main(){ int n,x; cin>>n; a.push_back(-1); for(int i=0;i<n;i++) { cin>>x; a.push_back(x); } sort(a.begin(),a.end()); BSTree tree = findroot(1,n); levelOrderTraverse(tree); printvec(a); return 0;}
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree
- 04-树5. Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree (30分)
- PTA 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree (30分)
- 04-树6 Complete Binary Search Tree (30分)
- 04-树6 Complete Binary Search Tree (30分)
- 04-树6 Complete Binary Search Tree
- 04-树6 Complete Binary Search Tree (30分)
- 04-树6 Complete Binary Search Tree (30分)
- C#读取TXT文件
- 利用Delphi的“File Of Type”创建并管理属于你自己的“数据库”
- NetBeans的使用,解决引用等问题,缺少JAX-WS-ENDORSED
- Android Resource概述
- HashSet和TreeSet中最好只放入不可变对象
- 04-树6 Complete Binary Search Tree
- Windows Server 2008 R2与LoadRunner
- MFC相关函数代码
- Intent和PendingIntent的区别
- Java中栈使用ArrayDeque或LinkedList的性能会更加出色
- 黑马程序员—Java基础—集合框架3
- Intent中的四个重要属性——Action、Data、Category、Extras
- LeetCode—32 Longest Valid Parentheses
- Linux各版本安装Oracle10g 11g教程集合