private LLRBNode PutNode(KeyType key, ValueType value, LLRBNode node) { if (node == null) { return(new LLRBNode(key, value)); } int comp = node.Key.CompareTo(key); if (comp == -1) { node.RChild = PutNode(key, value, node.RChild); } else if (comp == 0) { node.Value = value; } else { node.LChild = PutNode(key, value, node.LChild); } //Maintain an R-B tree if (isRed(node.RChild) && !isRed(node.LChild)) //Right red, left black: rotate left { node = rotateLeft(node); } if (isRed(node.LChild) && isRed(node.LChild.LChild)) //two left edges are red { node = rotateRight(node); } if (isRed(node.LChild) && isRed(node.RChild)) { flipColors(node); } return(node); }
public int CompareTo(LLRBNode that) { return(this.Key.CompareTo(that.Key)); }
public ValueType Find(KeyType key) => FindFromNode(key, root);// If the node with key exists, return the corresponding value; else return null private ValueType FindFromNode(KeyType key, LLRBNode node) { if (node == null) { return(default);// return null if search misses
public LLRB(KeyType key, ValueType value) { root = new LLRBNode(key, value); root.IsRed = false; }