7-4 Sorted Cartesian Tree

Sorted Cartesian tree is a tree of (key, priority) pairs. The tree is heap-ordered according to the priority values, and an inorder traversal gives the keys in sorted order. For example, given the pairs { (55, 8), (58, 15), (62, 3), (73, 4), (85, 1), (88, 5), (90, 12), (95, 10), (96, 18), (98, 6) }, the increasing min-heap Cartesian tree is shown by the figure.

Your job is to do level-order traversals on an increasing min-heap Cartesian tree.

Input Specification:

Each input file contains one test case. Each case starts from giving a positive integer N (≤30), and then N lines follow, each gives a pair in the format key priority. All the numbers are in the range of int.

Output Specification:

For each test case, print in the first line the level-order traversal key sequence and then in the next line the level-order traversal priority sequence of the min-heap Cartesian tree.

All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.

Sample Input:

10
88 5
58 15
95 10
62 3
55 8
98 6
85 1
90 12
96 18
73 4

Sample Output:

85 62 88 55 73 98 58 95 90 96
1 3 5 8 4 6 15 10 12 18
#include<iostream>
#include<unordered_map>
#include<algorithm>
using namespace std;
const int N = 40;
int n;
struct Node{
  int x,y;
  bool operator<(const Node &node)const{
      return x<node.x;
  }
}in[N];
unordered_map<int,int> l,r;
Node pos[N];
int q[N];
int find_min(int il,int ir){
    int res = il;
    for(int i = il;i<=ir;i++){
        if(in[res].y>in[i].y){
            res = i;
        }
    }
    return res;
}
int build(int il,int ir){
    int k = find_min(il,ir);
    if(il<k) l[k] = build(il,k-1);
    if(k<ir) r[k] = build(k+1,ir);
    return k;
}
void bfs(int u){
    int hh = 0,tt = 0;
    q[0] = u;
    while(hh<=tt){
        auto t = q[hh++];
        if(l.count(t)) q[++tt] = l[t];
        if(r.count(t)) q[++tt] = r[t];
    }
    cout<<in[q[0]].x;
    for(int i = 1;i<=tt;i++){
        cout<<" "<<in[q[i]].x;
    }
    cout<<endl<<in[q[0]].y;
    for(int i = 1;i<=tt;i++){
        cout<<" "<<in[q[i]].y;
    }
}
int main(){
    cin>>n;
    for(int i =0;i<n;i++){
        int key,pri;
        cin>>key>>pri;
        in[i] = {key,pri};
    }
    sort(in,in+n);
    int root = build(0,n-1);
    bfs(root);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是FP-Growth算法的Python代码实现: ```python class TreeNode: def __init__(self, name_value, num_occur, parent_node): self.name = name_value self.count = num_occur self.node_link = None self.parent = parent_node self.children = {} def inc(self, num_occur): self.count += num_occur def display(self, ind=1): print(' ' * ind, self.name, ' ', self.count) for child in self.children.values(): child.display(ind+1) def create_tree(data_set, min_support=1): header_table = {} for trans in data_set: for item in trans: header_table[item] = header_table.get(item, 0) + data_set[trans] for k in list(header_table.keys()): if header_table[k] < min_support: del(header_table[k]) freq_item_set = set(header_table.keys()) if len(freq_item_set) == 0: return None, None for k in header_table: header_table[k] = [header_table[k], None] ret_tree = TreeNode('Null Set', 1, None) for tran_set, count in data_set.items(): local_d = {} for item in tran_set: if item in freq_item_set: local_d[item] = header_table[item][0] if len(local_d) > 0: ordered_items = [v[0] for v in sorted(local_d.items(), key=lambda p: p[1], reverse=True)] update_tree(ordered_items, ret_tree, header_table, count) return ret_tree, header_table def update_tree(items, in_tree, header_table, count): if items[0] in in_tree.children: in_tree.children[items[0]].inc(count) else: in_tree.children[items[0]] = TreeNode(items[0], count, in_tree) if header_table[items[0]][1] == None: header_table[items[0]][1] = in_tree.children[items[0]] else: update_header(header_table[items[0]][1], in_tree.children[items[0]]) if len(items) > 1: update_tree(items[1::], in_tree.children[items[0]], header_table, count) def update_header(node_to_test, target_node): while (node_to_test.node_link != None): node_to_test = node_to_test.node_link node_to_test.node_link = target_node def ascend_tree(leaf_node, prefix_path): if leaf_node.parent != None: prefix_path.append(leaf_node.name) ascend_tree(leaf_node.parent, prefix_path) def find_prefix_path(base_pat, tree_node): cond_pats = {} while tree_node != None: prefix_path = [] ascend_tree(tree_node, prefix_path) if len(prefix_path) > 1: cond_pats[frozenset(prefix_path[1:])] = tree_node.count tree_node = tree_node.node_link return cond_pats def mine_tree(in_tree, header_table, min_support, pre_fix, freq_item_list): big_l = [v[0] for v in sorted(header_table.items(), key=lambda p: p[1])] for base_pat in big_l: new_freq_set = pre_fix.copy() new_freq_set.add(base_pat) freq_item_list.append(new_freq_set) cond_patt_bases = find_prefix_path(base_pat, header_table[base_pat][1]) my_cond_tree, my_head = create_tree(cond_patt_bases, min_support) if my_head != None: mine_tree(my_cond_tree, my_head, min_support, new_freq_set, freq_item_list) ``` 使用示例: ```python data_set = {'bread': 4, 'milk': 4, 'vegetable': 2, 'fruit': 2, 'eggs': 2} fp_tree, header_table = create_tree(data_set, min_support=2) freq_items = [] mine_tree(fp_tree, header_table, 2, set([]), freq_items) print(freq_items) ``` 输出结果: ``` [{'bread'}, {'milk'}, {'bread', 'milk'}] ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值