Advertisement
Guest User

rbt

a guest
Oct 22nd, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.10 KB | None | 0 0
  1. package rbtmap;
  2.  
  3. public class RedBlackTreeMap<K extends Comparable<K>, V> implements MapInterface<K, V> {
  4.  
  5.     private Node<K, V> root = null;
  6.  
  7.     @Override
  8.     public V getValue(K key) {
  9.         Node<K, V> tmp = findNode(key, root);
  10.         if (tmp == null)
  11.             return null;
  12.         else
  13.             return tmp.getVal();
  14.     }
  15.  
  16.     @Override
  17.     public void setValue(K key, V value) {
  18.         if (key == null)
  19.             throw new IllegalArgumentException("Given key is null!");
  20.         if (root == null) {
  21.             root = new Node<> (key, value);
  22.             root.paintBlack();
  23.         }
  24.         else {
  25.             Node<K, V> tmp = findNode(key, root);
  26.             if (tmp != null)
  27.                 tmp.setVal(value);
  28.             else {
  29.                 addNode(key, value, root);
  30.             }
  31.         }
  32.     }
  33.  
  34.     private Node<K, V> findNode(K key, Node<K, V> root) {
  35.         if (key.compareTo(root.getKey()) > 0)
  36.             findNode(key, root.getRightChild());
  37.         if (key.compareTo(root.getKey()) < 0)
  38.             findNode(key, root.getLeftChild());
  39.         return key == root.getKey()? root : null;
  40.     }
  41.  
  42.     private Node<K,V> addNode(K key, V value, Node<K,V> subRoot) {
  43.         if (subRoot != null) {
  44.             if (subRoot.getKey().compareTo( key ) > 0)
  45.                 subRoot = addNode(key, value, subRoot.getLeftChild());
  46.             else
  47.                 subRoot = addNode(key, value, subRoot.getRightChild());
  48.             fixTreeAfterInsertion(subRoot);
  49.         }
  50.         return new Node<>(key, value);
  51.     }
  52.  
  53.     private void fixTreeAfterInsertion(Node<K, V> pivot) {
  54.         if (pivot.getLeftChild().isRed() && pivot.getRightChild().isRed()) {
  55.             pivot.getRightChild().paintBlack();
  56.             pivot.getLeftChild().paintBlack();
  57.             pivot.paintRed();
  58.         }
  59.         if (pivot.getRightChild().isRed() && !pivot.getLeftChild().isRed()) {
  60.             pivot = rotateLeft(pivot);
  61.             pivot.getLeftChild().paintRed();
  62.             pivot.paintBlack();
  63.         }
  64.         if (pivot.getLeftChild().isRed() && pivot.getLeftChild().getLeftChild().isRed()) {
  65.             pivot.getLeftChild().paintBlack();
  66.             pivot = rotateRight(pivot);
  67.             pivot.paintRed();
  68.         }
  69.     }
  70.  
  71.     private Node<K,V> rotateLeft(Node<K,V> pivot) {
  72.         Node<K,V> left = pivot.getLeftChild();
  73.         Node<K,V> right = pivot.getRightChild();
  74.         right.setLeftChild(pivot);
  75.         pivot.setRightChild(left);
  76.         return right;
  77.     }
  78.  
  79.     private Node<K,V> rotateRight(Node<K,V> pivot) {
  80.         Node<K,V> left = pivot.getLeftChild();
  81.         Node<K,V> right = pivot.getRightChild();
  82.         left.setRightChild(pivot);
  83.         pivot.setLeftChild(right);
  84.         return left;
  85.     }
  86. }
  87.  
  88. /*
  89. Node.java
  90. */
  91.  
  92. package rbtmap;
  93.  
  94. public class Node<K extends Comparable<K>, V> {
  95.     private K key;
  96.     private V value;
  97.     private Node<K, V> leftChild;
  98.     private Node<K, V> rightChild;
  99.     private enum Color {
  100.         RED, BLACK
  101.     }
  102.     private Color color;
  103.  
  104.     private Color getColor() {
  105.         return color;
  106.     }
  107.  
  108.     private void setColor(Color color) {
  109.         this.color = color;
  110.     }
  111.  
  112.     public K getKey() {
  113.         return key;
  114.     }
  115.  
  116.     public V getVal() {
  117.         return value;
  118.     }
  119.  
  120.     public Node<K, V> getLeftChild() {
  121.         return leftChild;
  122.     }
  123.  
  124.     public Node<K, V> getRightChild() {
  125.         return rightChild;
  126.     }
  127.  
  128.     public void setLeftChild(Node<K, V> leftChild) {
  129.         this.leftChild = leftChild;
  130.     }
  131.  
  132.     public void setRightChild(Node<K, V> rightChild) {
  133.         this.rightChild = rightChild;
  134.     }
  135.  
  136.     public void setVal(V value) {
  137.         this.value = value;
  138.     }
  139.  
  140.     public boolean isRed() {
  141.         return getColor() == Color.RED;
  142.     }
  143.  
  144.     public void paintBlack() {
  145.         setColor(Color.BLACK);
  146.     }
  147.  
  148.     public void paintRed() {
  149.         setColor(Color.RED);
  150.     }
  151.  
  152.     public Node(K key, V value) {
  153.         this.key = key;
  154.         this.value = value;
  155.         leftChild = null;
  156.         rightChild = null;
  157.         setColor(Color.RED);
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement