04-树6 Complete Binary Search Tree

来源:互联网 发布:m20火箭筒数据 编辑:程序博客网 时间:2024/06/01 14:01
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 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;}


    0 0
    原创粉丝点击