Skip to content

Commit 21ffa6c

Browse files
committed
simplifies select() code and improves variable names
1 parent 472ab01 commit 21ffa6c

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

src/main/java/edu/princeton/cs/algs4/BST.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -346,30 +346,32 @@ private Node ceiling(Node x, Key key) {
346346
}
347347

348348
/**
349-
* Return the key in the symbol table whose rank is {@code k}.
350-
* This is the (k+1)st smallest key in the symbol table.
349+
* Return the key in the symbol table of a given {@code rank}.
350+
* This key has the property that there are {@code rank} keys in
351+
* the symbol table that are smaller. In other words, this key is the
352+
* ({@code rank}+1)st smallest key in the symbol table.
351353
*
352-
* @param k the order statistic
353-
* @return the key in the symbol table of rank {@code k}
354-
* @throws IllegalArgumentException unless {@code k} is between 0 and
354+
* @param rank the order statistic
355+
* @return the key in the symbol table of given {@code rank}
356+
* @throws IllegalArgumentException unless {@code rank} is between 0 and
355357
* <em>n</em>–1
356358
*/
357-
public Key select(int k) {
358-
if (k < 0 || k >= size()) {
359-
throw new IllegalArgumentException("argument to select() is invalid: " + k);
359+
public Key select(int rank) {
360+
if (rank < 0 || rank >= size()) {
361+
throw new IllegalArgumentException("argument to select() is invalid: " + rank);
360362
}
361-
Node x = select(root, k);
362-
return x.key;
363+
return select(root, rank);
363364
}
364365

365-
// Return key of rank k.
366-
private Node select(Node x, int k) {
367-
if (x == null) return null;
368-
int t = size(x.left);
369-
if (t > k) return select(x.left, k);
370-
else if (t < k) return select(x.right, k-t-1);
371-
else return x;
372-
}
366+
// Return key in BST rooted at x of given rank.
367+
// Precondition: rank is in legal range.
368+
private Key select(Node x, int rank) {
369+
if (x == null) return null;
370+
int leftSize = size(x.left);
371+
if (leftSize > rank) return select(x.left, rank);
372+
else if (leftSize < rank) return select(x.right, rank - leftSize - 1);
373+
else return x.key;
374+
}
373375

374376
/**
375377
* Return the number of keys in the symbol table strictly less than {@code key}.

src/main/java/edu/princeton/cs/algs4/RedBlackBST.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -524,31 +524,32 @@ private Node ceiling(Node x, Key key) {
524524
}
525525

526526
/**
527-
* Return the key in the symbol table whose rank is {@code k}.
528-
* This is the (k+1)st smallest key in the symbol table.
527+
* Return the key in the symbol table of a given {@code rank}.
528+
* This key has the property that there are {@code rank} keys in
529+
* the symbol table that are smaller. In other words, this key is the
530+
* ({@code rank}+1)st smallest key in the symbol table.
529531
*
530-
* @param k the order statistic
531-
* @return the key in the symbol table of rank {@code k}
532-
* @throws IllegalArgumentException unless {@code k} is between 0 and
532+
* @param rank the order statistic
533+
* @return the key in the symbol table of given {@code rank}
534+
* @throws IllegalArgumentException unless {@code rank} is between 0 and
533535
* <em>n</em>–1
534536
*/
535-
public Key select(int k) {
536-
if (k < 0 || k >= size()) {
537-
throw new IllegalArgumentException("argument to select() is invalid: " + k);
537+
public Key select(int rank) {
538+
if (rank < 0 || rank >= size()) {
539+
throw new IllegalArgumentException("argument to select() is invalid: " + rank);
538540
}
539-
Node x = select(root, k);
540-
return x.key;
541+
return select(root, rank);
541542
}
542543

543-
// the key of rank k in the subtree rooted at x
544-
private Node select(Node x, int k) {
545-
// assert x != null;
546-
// assert k >= 0 && k < size(x);
547-
int t = size(x.left);
548-
if (t > k) return select(x.left, k);
549-
else if (t < k) return select(x.right, k-t-1);
550-
else return x;
551-
}
544+
// Return key in BST rooted at x of given rank.
545+
// Precondition: rank is in legal range.
546+
private Key select(Node x, int rank) {
547+
if (x == null) return null;
548+
int leftSize = size(x.left);
549+
if (leftSize > rank) return select(x.left, rank);
550+
else if (leftSize < rank) return select(x.right, rank - leftSize - 1);
551+
else return x.key;
552+
}
552553

553554
/**
554555
* Return the number of keys in the symbol table strictly less than {@code key}.

0 commit comments

Comments
 (0)