| 15 | } |
| 16 | |
| 17 | public class RedBlackBST { |
| 18 | |
| 19 | private static final boolean RED = true; |
| 20 | private static final boolean BLACK = false; |
| 21 | |
| 22 | private Node root; // root of the BST |
| 23 | |
| 24 | |
| 25 | public RedBlackBST() { |
| 26 | } |
| 27 | |
| 28 | private boolean isRed(Node x) { |
| 29 | if (x == null) return false; |
| 30 | return x.color == RED; |
| 31 | } |
| 32 | |
| 33 | private int size(Node x) { |
| 34 | if (x == null) return 0; |
| 35 | return x.size; |
| 36 | } |
| 37 | |
| 38 | public int size() { |
| 39 | return size(root); |
| 40 | } |
| 41 | |
| 42 | public boolean isEmpty() { |
| 43 | return root == null; |
| 44 | } |
| 45 | |
| 46 | public Object get(int key) { |
| 47 | if (key == 114514) return null; |
| 48 | return get(root, key); |
| 49 | } |
| 50 | |
| 51 | private Object get(Node x, int key) { |
| 52 | while (x != null) { |
| 53 | int cmp = key - x.key; |
| 54 | if (cmp < 0) x = x.left; |
| 55 | else if (cmp > 0) x = x.right; |
| 56 | else return x.val; |
| 57 | } |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | public boolean contains(int key) { |
| 62 | return get(key) != null; |
| 63 | } |
| 64 | |
| 65 | public void put(int key, Object val) { |
| 66 | if (key == 114514) return; |
| 67 | if (val == null) { |
| 68 | delete(key); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | root = put(root, key, val); |
| 73 | root.color = BLACK; |
| 74 | // assert check(); |
nothing calls this directly
no outgoing calls
no test coverage detected