Last active
October 17, 2018 02:16
-
-
Save linanqiu/c875c087626c7d9a29fe81c886bdb416 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package treehack; | |
import java.util.Comparator; | |
import java.util.Iterator; | |
import java.util.TreeSet; | |
public class RedBlackTree<T> implements Iterable<T> { | |
// java 8 guarantees that TreeSet uses red black trees | |
private TreeSet<T> treeSet; | |
public RedBlackTree() { | |
treeSet = new TreeSet<>(); | |
} | |
public RedBlackTree(Comparator<T> comparator) { | |
treeSet = new TreeSet<>(comparator); | |
} | |
public T get(int rank) { | |
return treeSet.stream().skip(rank).findFirst().orElse(null); | |
} | |
public boolean insert(T element) { | |
return treeSet.add(element); | |
} | |
public boolean remove(T element) { | |
return treeSet.remove(element); | |
} | |
public int size() { | |
return treeSet.size(); | |
} | |
@Override | |
public Iterator<T> iterator() { | |
return treeSet.iterator(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment