-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreap.java
More file actions
91 lines (80 loc) · 2.57 KB
/
Copy pathTreap.java
File metadata and controls
91 lines (80 loc) · 2.57 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package Treap;
import java.util.Arrays;
public class Treap {
Node root;
int[] values;
int[] priorities;
public Treap(int[] xCoordinates, int[] yCoordinates){
this.values = xCoordinates;
this.priorities = yCoordinates;
Arrays.sort(xCoordinates);
int maxElemIndex = getMax(yCoordinates, 0, yCoordinates.length-1);
root = new Node(xCoordinates[maxElemIndex], yCoordinates[maxElemIndex]);
root.leftChild = construct(0, maxElemIndex-1);
root.rightChild = construct(maxElemIndex+1, yCoordinates.length-1);
}
public Treap(Node node){
this.root = node;
}
public Node construct(int left, int right){
if (left > right){
return null;
}
int index = getMax(priorities, left, right);
Node node = new Node(values[index], priorities[index]);
node.leftChild = construct(left, index-1);
node.rightChild = construct(index+1, right);
return node;
}
public static Treap merge(Treap leftTree, Treap rightTree){
if (leftTree.root == null){
return rightTree;
}
if (rightTree.root == null){
return leftTree;
}
if (leftTree.root.priority > rightTree.root.priority){
Treap treap = merge(new Treap(leftTree.root.rightChild), rightTree);
leftTree.root.rightChild = treap.root;
return leftTree;
}else {
Treap treap = merge(leftTree, new Treap(rightTree.root.leftChild));
rightTree.root.leftChild = treap.root;
return rightTree;
}
}
public int getMax(int[] array, int left, int right){
int maxElementIndex = left;
for (int i = left+1; i <= right; i++){
if (array[i] > array[maxElementIndex]){
maxElementIndex = i;
}
}
return maxElementIndex;
}
public void traverse(Node node){
if (node == null){
return;
}
System.out.println("{ "+node.value + ", " + node.priority + "} ");
traverse(node.leftChild);
traverse(node.rightChild);
}
public static void main(String[] args) {
int[] xs = {0,2,3,4,5,6,7,9,11,13,14};
int[] ys = {3,4,3,6,1,2,10,7,3,8,4};
Treap treap = new Treap(xs, ys);
treap.traverse(treap.root);
}
}
class Node{
int value;
int priority;
Node leftChild;
Node rightChild;
public Node(int value, int priority){
this.value = value;
this.priority = priority;
leftChild = rightChild = null;
}
}