04-树6 Complete Binary Search Tree
2015-11-05 15:33
295 查看
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.
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.
思路:
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;
}
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 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:
10 1 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;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(七):数据结构详解
- Lua教程(二):C++和Lua相互传递数据示例
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构揭秘一
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例