forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1785 Binary Search Heap Construction.java
70 lines (68 loc) · 1.67 KB
/
1785 Binary Search Heap Construction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.io.*;
import java.util.*;
class TreapNode {
String label;
int priority;
TreapNode left;
TreapNode right;
TreapNode father;
TreapNode(String label, int priority) {
this.label = label;
this.priority = priority;
this.left = null;
this.right = null;
this.father = null;
}
public String toString() {
return label + "/" + priority;
}
}
class TreapNodeComparator implements Comparator<TreapNode> {
public int compare(TreapNode a, TreapNode b) {
return a.label.compareTo(b.label);
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<TreapNode> nodes = new ArrayList<TreapNode>();
while (true) {
nodes.clear();
nodes.add(new TreapNode("", Integer.MAX_VALUE));
String[] strs = in.nextLine().split("\\s+");
int number = Integer.parseInt(strs[0]);
if (number == 0) {
System.exit(0);
}
for (int i = 1; i <= number; ++i) {
String[] attrs = strs[i].split("/");
int priority = Integer.parseInt(attrs[1]);
nodes.add(new TreapNode(attrs[0], priority));
}
Collections.sort(nodes, new TreapNodeComparator());
for (int i = 1; i <= number; ++i) {
insert(nodes.get(i), nodes.get(i - 1));
}
printNode(nodes.get(0).right);
System.out.print("\n");
}
}
private static void insert(TreapNode cur, TreapNode prev) {
while (prev.priority < cur.priority) {
prev = prev.father;
}
cur.left = prev.right;
prev.right = cur;
cur.father = prev;
}
private static void printNode(TreapNode node) {
if (node == null) {
return;
}
System.out.print("(");
printNode(node.left);
System.out.print(node.toString());
printNode(node.right);
System.out.print(")");
}
}