-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMergeSortTree.java
More file actions
38 lines (33 loc) · 799 Bytes
/
MergeSortTree.java
File metadata and controls
38 lines (33 loc) · 799 Bytes
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
import java.util.ArrayList;
public class MergeSortTree<V extends Comparable<V>> extends SegmentTree<ArrayList<V>, V>
{
MergeSortTree(V[] arr) {
super(arr);
// TODO Auto-generated constructor stub
}
public ArrayList<V> leaf(int index) {
ArrayList<V> a=new ArrayList<V>();
a.add(getValue(index));
return a;
}
public ArrayList<V> process(ArrayList <V> left, ArrayList <V> right)
{
ArrayList <V> merged=new ArrayList<V>();
int l,r;
l=0;r=0;
while(l<left.size() && r<right.size())
{
merged.add(left.get(l).compareTo(right.get(r))<0?left.get(l++):right.get(r++));
}
while(r<right.size())
merged.add(right.get(r++));
while(l<left.size())
merged.add(left.get(l++));
return merged;
}
@Override
public V updateAtLeaf(int index, V x)
{
return x;
}
}