-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxPQC
192 lines (169 loc) · 3.57 KB
/
MaxPQC
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//file: MaxPQC.java
//author: Connor Maloney
//date: 3/16/2014
//
// This program uses a binary heap to print out input using a triply-linked data structures
public class MaxPQC<T extends Comparable<T>> implements MaxPQ<T>
{
private int N;
private Node top;
private class Node
{
private T info;
private Node left;
private Node right;
private Node parent;
private Node(T info)
{
this.info = info;
this.left = null;
this.right = null;
this.parent = null;
}
public T getInfo(){return this.info;}
}
public MaxPQC()
{
this.N = 0;
this.top = null;
}
public T delMax()
{
if (this.isEmpty())
throw new RuntimeException("Empty");
if (this.N == 1)
{
N--;
return this.top.info;
}
if (this.N > 1)
{
T r = this.top.info;
Node g = readPath(swap(pathTo(N)), this.top);
T y = g.getInfo();
this.top.info = y;
g = null;
sink(this.top);
N--;
return r;}
else
{throw new RuntimeException("wrong");}
}
public void insert(T key)
{
if (this.isEmpty())
{
Node temp = new Node(key);
this.top = temp;
N++;
}
else
{
Node u = readPath(swap(pathTo(N+1)), this.top);
u = new Node(key);
if (N % 2 == 0)
{
this.top.right = u;
u.parent = this.top;
}
if (N%2 == 1)
{
this.top.left = u;
u.parent = this.top;
}
swim(u);
this.N++;
}
}
private void sink(Node key)
{
int y = 0;
T x = key.right.info;
if (x == null)
{y = -1;}
else
{y = (key.right.info.compareTo(key.left.info));}
int z = key.info.compareTo(this.top.left.info);
int u = key.info.compareTo(this.top.right.info);
while (y == -1)
{
if (z == -1)
exch(key.info, this.top.left.info);
}
while(y > -1)
{
if (u == -1)
exch(key.info, this.top.right.info);
}
}
private void swim(Node key)
{
int r = 0;
T x = key.parent.info;
if (x == null)
{r = 1;}
else
r = key.info.compareTo(key.parent.info);
if (r == 1)
{
if (N%2 == 0)
exch(key.parent, key);
else
{
exch(key.parent, key);
}
}
}
private void exch(Object u, Object r)
{
Object c = u;
u = r;
r = c;
}
public boolean isEmpty(){return this.N == 0;}
public int size(){return this.N;}
public String toString(){ return "";}
public Node readPath(String a, Node c)
{
if (a.equals(""))
return c;
if (a.charAt(0) =='0'){
return readPath(a.substring(1), c.left);}
else{
return readPath(a.substring(1), c.right);}
}
private String pathTo(int u)
{
if (u <=1)
{return "";}
if (u > 1)
{
if (u%2 == 1)
return "1" + pathTo(u/2);
if (u % 2 == 0)
return "0" + pathTo(u/2);
else
return "";
}
else
{return "";}
}
public static String swap (String names)
{
String p = " ";
for (int i=names.length()-1; i>0; i--)
{
p += names.charAt(i);
}
return p;
}
public static void main (String args[])
{
MaxPQ<String> u = new MaxPQC<String>();
u.insert("A");
u.insert("B");
u.insert("C");
System.out.println(u.delMax());
System.out.println(u.delMax());
}
}