| 
001002
 003
 004
 005
 006
 007
 008
 009
 010
 011
 012
 013
 014
 015
 016
 017
 018
 019
 020
 021
 022
 023
 024
 025
 026
 027
 028
 029
 030
 031
 032
 033
 034
 035
 036
 037
 038
 039
 040
 041
 042
 043
 044
 045
 046
 047
 048
 049
 050
 051
 052
 053
 054
 055
 056
 057
 058
 059
 060
 061
 062
 063
 064
 065
 066
 067
 068
 069
 070
 071
 072
 073
 074
 075
 076
 077
 078
 079
 080
 081
 082
 083
 084
 085
 086
 087
 088
 089
 090
 091
 092
 093
 094
 095
 096
 097
 098
 099
 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
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 
 | package algs33;
import stdlib.*;
/* ***********************************************************************
 *  Compilation:  javac SplayBST.java
 *  Execution:    java SplayBST
 *
 *  Splay tree. Supports splay-insert, -search, and -delete.
 *  Splays on every operation, regardless of the presence of the associated
 *  key prior to that operation.
 *
 *  Written by Josh Israel.
 *
 *************************************************************************/
public class XSplayBST<K extends Comparable<? super K>, V>  {
  private Node<K,V> root;   // root of the BST
  // BST helper node data type
  private static class Node<K,V> {
    public final K key;          // key
    public V value;        // associated data
    public Node<K,V> left, right;   // left and right subtrees
    public Node(K key, V value) {
      this.key   = key;
      this.value = value;
    }
  }
  public boolean contains(K key) {
    return (get(key) != null);
  }
  // return value associated with the given key
  // if no such value, return null
  public V get(K key) {
    root = splay(root, key);
    int cmp = key.compareTo(root.key);
    if (cmp == 0) return root.value;
    else          return null;
  }
  /* ***********************************************************************
   *  splay insertion
   *************************************************************************/
  public void put(K key, V value) {
    // splay key to root
    if (root == null) {
      root = new Node<>(key, value);
      return;
    }
    root = splay(root, key);
    int cmp = key.compareTo(root.key);
    // Insert new node at root
    if (cmp < 0) {
      Node<K,V> n = new Node<>(key, value);
      n.left = root.left;
      n.right = root;
      root.left = null;
      root = n;
    }
    // Insert new node at root
    else if (cmp > 0) {
      Node<K,V> n = new Node<>(key, value);
      n.right = root.right;
      n.left = root;
      root.right = null;
      root = n;
    }
    // It was a duplicate key. Simply replace the value
    else if (cmp == 0) {
      root.value = value;
    }
  }
  /* ***********************************************************************
   *  splay deletion
   *************************************************************************/
  /* This splays the key, then does a slightly modified Hibbard deletion on
   * the root (if it is the node to be deleted; if it is not, the key was
   * not in the tree). The modification is that rather than swapping the
   * root (call it node A) with its successor, it's successor (call it Node<K,V> B)
   * is moved to the root position by splaying for the deletion key in A's
   * right subtree. Finally, A's right child is made the new root's right
   * child.
   */
  public void remove(K key) {
    if (root == null) return; // empty tree
    root = splay(root, key);
    int cmp = key.compareTo(root.key);
    if (cmp == 0) {
      if (root.left == null) {
        root = root.right;
      }
      else {
        Node<K,V> x = root.right;
        root = root.left;
        splay(root, key);
        root.right = x;
      }
    }
    // else: it wasn't in the tree to remove
  }
  /* **********************************************************************
   * splay function
   * **********************************************************************/
  // splay key in the tree rooted at Node<K,V> h. If a node with that key exists,
  //   it is splayed to the root of the tree. If it does not, the last node
  //   along the search path for the key is splayed to the root.
  private Node<K,V> splay(Node<K,V> h, K key) {
    if (h == null) return null;
    int cmp1 = key.compareTo(h.key);
    if (cmp1 < 0) {
      // key not in tree, so we're done
      if (h.left == null) {
        return h;
      }
      int cmp2 = key.compareTo(h.left.key);
      if (cmp2 < 0) {
        h.left.left = splay(h.left.left, key);
        h = rotateRight(h);
      }
      else if (cmp2 > 0) {
        h.left.right = splay(h.left.right, key);
        if (h.left.right != null)
          h.left = rotateLeft(h.left);
      }
      if (h.left == null) return h;
      else                return rotateRight(h);
    }
    else if (cmp1 > 0) {
      // key not in tree, so we're done
      if (h.right == null) {
        return h;
      }
      int cmp2 = key.compareTo(h.right.key);
      if (cmp2 < 0) {
        h.right.left  = splay(h.right.left, key);
        if (h.right.left != null)
          h.right = rotateRight(h.right);
      }
      else if (cmp2 > 0) {
        h.right.right = splay(h.right.right, key);
        h = rotateLeft(h);
      }
      if (h.right == null) return h;
      else                 return rotateLeft(h);
    }
    else return h;
  }
  /* ***********************************************************************
   *  helper functions
   *************************************************************************/
  // height of tree (empty tree height = 0)
  public int height() { return height(root); }
  private int height(Node<K,V> x) {
    if (x == null) return 0;
    return 1 + Math.max(height(x.left), height(x.right));
  }
  public int size() {
    return size(root);
  }
  private int size(Node<K,V> x) {
    if (x == null) return 0;
    else return (1 + size(x.left) + size(x.right));
  }
  // right rotate
  private Node<K,V> rotateRight(Node<K,V> h) {
    Node<K,V> x = h.left;
    h.left = x.right;
    x.right = h;
    return x;
  }
  // left rotate
  private Node<K,V> rotateLeft(Node<K,V> h) {
    Node<K,V> x = h.right;
    h.right = x.left;
    x.left = h;
    return x;
  }
  // test client
  public static void main(String[] args) {
    XSplayBST<Integer, Integer> st1 = new XSplayBST<>();
    st1.put(5, 5);
    st1.put(9, 9);
    st1.put(13, 13);
    st1.put(11, 11);
    st1.put(1, 1);
    XSplayBST<String, String> st = new XSplayBST<>();
    st.put("www.cs.princeton.edu", "128.112.136.11");
    st.put("www.cs.princeton.edu", "128.112.136.12");
    st.put("www.cs.princeton.edu", "128.112.136.13");
    st.put("www.princeton.edu",    "128.112.128.15");
    st.put("www.yale.edu",         "130.132.143.21");
    st.put("www.simpsons.com",     "209.052.165.60");
    StdOut.println("The size 0 is: " + st.size());
    st.remove("www.yale.edu");
    StdOut.println("The size 1 is: " + st.size());
    st.remove("www.princeton.edu");
    StdOut.println("The size 2 is: " + st.size());
    st.remove("non-member");
    StdOut.println("The size 3 is: " + st.size());
    StdOut.println(st.get("www.cs.princeton.edu"));
    StdOut.println("The size 4 is: " + st.size());
    StdOut.println(st.get("www.yale.com"));
    StdOut.println("The size 5 is: " + st.size());
    StdOut.println(st.get("www.simpsons.com"));
    StdOut.println("The size 6 is: " + st.size());
    StdOut.println();
  }
}
 |