poj1785&zoj2243 Binary Search Heap Construction(笛卡尔树)

Binary Search Heap Construction

Time Limit: 5 Seconds       Memory Limit: 32768 KB

Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal nodes have each assigned a priority (a number) such that the priority of each internal node is less than the priority of its parent. As a consequence, the root has the greatest priority in the tree, which is one of the reasons why heaps can be used for the implementation of priority queues and for sorting.

A binary tree in which each internal node has both a label and a priority, and which is both a binary search tree with respect to the labels and a heap with respect to the priorities, is called a treap. Your task is, given a set of label-priority-pairs, with unique labels and unique priorities, to construct a treap containing this data.

Input Specification

The input contains several test cases. Every test case starts with an integer n. You may assume that 1<=n<=50000. Then follow n pairs of strings and numbers l1/p1,...,ln/pn denoting the label and priority of each node. The strings are non-empty and composed of lower-case letters, and the numbers are non-negative integers. The last test case is followed by a zero.

Output Specification

For each test case output on a single line a treap that contains the specified nodes. A treap is printed as (<left sub-treap><label>/<priority><right sub-treap>). The sub-treaps are printed recursively, and omitted if leafs.

Sample Input

7 a/7 b/6 c/5 d/4 e/3 f/2 g/1
7 a/1 b/2 c/3 d/4 e/5 f/6 g/7
7 a/3 b/6 c/4 d/7 e/2 f/5 g/1
0

Sample Output

(a/7(b/6(c/5(d/4(e/3(f/2(g/1)))))))
(((((((a/1)b/2)c/3)d/4)e/5)f/6)g/7)
(((a/3)b/6(c/4))d/7((e/2)f/5(g/1)))

Source: University of Ulm Local Contest 2004

题目大意:给n个二元组,每个二元组由一个字符串和一个数字组成,中间一个/分隔。现在要求按字符串为关键字构造一颗二叉搜索树,同时按数字为关键字构造一个大堆。

题目分析:红果果的笛卡尔树。

百科名片
笛卡尔树结构由Vuillmin在解决范围搜索的几何数据结构问题时提出的,从数列中构造一棵笛卡尔树可以线性时间完成,需要采用基于栈的算法来找到在该数列中的所有最近小数。由此可知,笛卡尔树是一种特定的二叉树数据结构,可由数列构造,在范围最值查询、范围top k查询(range top k queries)等问题上有广泛应用。它具有堆的有序性,中序遍历可以输出原数列。
这是笛卡尔树的百度名片。笛卡尔树是这样一种树,每个节点有2个关键字key、value。从key的角度看,这是一颗二叉排序树,从value的角度看,这是一个堆。这题就是让以字符串为关键字key,数字为关键字value,构造一个二叉搜索大堆,最后按要求中序遍历之。
关于笛卡尔树的构造有一个很优美的O(n)的算法:
先按key关键字 从小到大 排序,然后一个个插入空树,最后构造出完整的笛卡尔树。因为笛卡尔树要按照关键字key构成二叉排序树,按key值升序排列后有个好处就是我们建树的时候从左下角往右下角建,这样保证建出来的树是二叉排序树。至于树的形状,则由关键字value决定。用一个栈来维护树的节点,当开始插入第i个节点的时候,因为要建一个大堆,所以先将栈头所有比当前待插入节点value值小的弹出,如果栈中还有元素,则栈中元素的value是不小于当前节点的value的,那么根据大堆的性质,当前节点应该作为当前栈顶节点的右孩子,为什么是右孩子?因为要使构成的树满足二叉排序树的性质,节点只能往右插,已经根据key值排序了。然后检查插入当前节点是否有元素出栈了,如果有,那么肯定是因为value值比当前value值小,那么最近一个出栈的元素就作为当前节点的左孩子,为什么是左孩子?还是因为要满足二叉排序树的性质,被弹出栈的肯定是先入栈的,那么key关键字肯定比当前节点小,当然要作为当前节点的左孩子。
就这样当n个节点都进栈一遍后整颗树也就建好了,噢,还差个根,那么当前栈底元素就是根,因为此时栈底留下的肯定就是value值最大的,最为堆顶,也就是根了。
详情请建代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 50005;
struct node
{
    int dig;
    char cha[10];
    int pa,l,r;
}lcm[N];
int stack[N];
char buf[100];
int n;
int cmp(struct node a,struct node b)
{
    return strcmp(a.cha,b.cha) < 0;
}

int build()
{
    int i,top,j;
    for(i = 0;i < n;i ++)
        lcm[i].l = lcm[i].r = lcm[i].pa =  -1;
    top = -1;
    for(i = 0;i < n;i ++)
    {
        j = top;
        while(j >= 0 && lcm[stack[j]].dig < lcm[i].dig)
            j --;
        if(j != -1)
        {
            lcm[i].pa = stack[j];
            lcm[stack[j]].r = i;
        }
        if(j < top)
        {
            lcm[stack[j + 1]].pa = i;
            lcm[i].l = stack[j + 1];
        }
        stack[++ j] = i;
        top = j;
    }
    lcm[stack[0]].pa = -1;
    return stack[0];
}

void dfs(int cur)
{
    if(cur == -1)
        return;
    printf("(");
    dfs(lcm[cur].l);
    printf("%s/%d",lcm[cur].cha,lcm[cur].dig);
    dfs(lcm[cur].r);
    printf(")");
}

int main()
{
    int i;
    while(scanf("%d",&n),n)
    {
        for(i = 0;i < n;i ++)
        {
            scanf("%s",buf);
            sscanf(buf,"%[^/]/%d",lcm[i].cha,&lcm[i].dig);
        }
        sort(lcm,lcm + n,cmp);
        int root = build();
        dfs(root);
        putchar(10);
    }
    return 0;
}
//280ms	1748kb



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值