internal static void AddItem(AVLNode headNode, TKey key, TValue value, AVLTree <TKey, TValue> tree, ref int elementCount) { int compare = key.CompareTo(headNode.key); while (true) { if (compare < 0) { if (headNode.left == null) { headNode.left = new AVLNode(key: key, value: value) { head = headNode, isLeft = true }; headNode._depthL = 1; AVLNode.BalanceBubbleUp(headNode, tree); elementCount++; break; } else { headNode = headNode.left; compare = key.CompareTo(headNode.key); } } else if (compare > 0) { if (headNode.right == null) { headNode.right = new AVLNode(key: key, value: value) { head = headNode, isLeft = false }; headNode._depthR = 1; AVLNode.BalanceBubbleUp(headNode, tree); elementCount++; break; } else { headNode = headNode.right; compare = key.CompareTo(headNode.key); } } else { headNode.value = value; break; } } }
public bool TryGet([NotNull] TKey key, out TValue value) { var current = this; while (true) { var compare = key.CompareTo(current.key); if (compare == 0) { value = current.value; return(true); } if (compare < 0) { if (current.left != null) { current = current.left; continue; } } else { if (current.right != null) { current = current.right; continue; } } value = default; return(false); } }
public bool ContainsKey([NotNull] TKey key) { var current = this; while (true) { var compare = key.CompareTo(current.key); if (compare == 0) { return(true); } if (compare < 0) { if (current.left != null) { current = current.left; continue; } } else { if (current.right != null) { current = current.right; continue; } } return(false); } }
public bool Add([NotNull] TKey key, TValue value, out AvlNode node) { var compare = key.CompareTo(this.key); if (compare == 0) { node = this; return(false); } if (compare < 0) { if (this.left != null) { var result = this.left.Add(key, value, out node); // Check whether rebalance is necessary and update height if (result) { this.left = node; node = Rebalance(this); node.height = CalculateHeight(node); } return(result); } this.left = new AvlNode(key, value); } else { if (this.right != null) { var result = this.right.Add(key, value, out node); if (result) { this.right = node; node = Rebalance(this); node.height = CalculateHeight(node); } return(result); } this.right = new AvlNode(key, value); } // Only reached when the new node was added to an empty place this.height = 2; node = this; return(true); }
public static Map Lookup(TKey item, Map sibling) { if (sibling == null) { throw new NotFound(); } if (item.CompareTo(sibling.Option) == 0) { return(sibling); } return(Lookup(item, sibling.Sibling)); }
internal static bool FindRemoveKey(AVLNode headNode, object[] HashMap, int hash, TKey key, ref int elementCount) { int compare = key.CompareTo(headNode.key); while (true) { if (compare < 0) { headNode = headNode.left; if (headNode != null) { compare = key.CompareTo(headNode.key); } else { return(false); } } else if (compare > 0) { headNode = headNode.right; if (headNode != null) { compare = key.CompareTo(headNode.key); } else { return(false); } } else { AVLNode.RemoveNode(headNode, HashMap, hash, ref elementCount); return(true); } } }
internal static bool FindRemoveKey(AVLNode node, QueuedAVLTree <TKey, TValue> tree, TKey key, ref int elementCount) { int compare = key.CompareTo(node.key); while (true) { if (compare < 0) { node = node.left; if (node != null) { compare = key.CompareTo(node.key); } else { return(false); } } else if (compare > 0) { node = node.right; if (node != null) { compare = key.CompareTo(node.key); } else { return(false); } } else { AVLNode.RemoveNode(node, tree); return(true); } } }
internal bool GetInsertIndex(TKey key, out int insertIndex) { for (int i = 0; i < this.KeyCount; ++i) { insertIndex = i; if (key.CompareTo(this.Keys[i]) < 0) { return(false); } else if (key.CompareTo(this.Keys[i]) == 0) { return(true); } else { continue; } } insertIndex = this.KeyCount; return(false); }
public AvlNode AddOrReplace([NotNull] TKey key, TValue value) { var compare = key.CompareTo(this.key); if (compare == 0) { this.value = value; return(this); } if (compare < 0) { if (this.left != null) { this.left = this.left.AddOrReplace(key, value); // Check whether rebalance is necessary and update height var node = Rebalance(this); node.height = CalculateHeight(node); return(node); } else { this.left = new AvlNode(key, value); this.height = 2; return(this); } } else { if (this.right != null) { this.right = this.right.AddOrReplace(key, value); var node = Rebalance(this); node.height = CalculateHeight(node); return(node); } else { this.right = new AvlNode(key, value); this.height = 2; return(this); } } }
public bool Remove([NotNull] TKey key, out AvlNode node) { var compare = key.CompareTo(this.key); if (compare == 0) { if (this.left == null) { node = this.right; } else { if (this.right == null) { node = this.left; } else { var top = MinValueNode(this.right); this.right.Remove(top.key, out top); top.left = this.left; top.right = this.right; top = Rebalance(top); top.height = CalculateHeight(top); node = top; } } return(true); } if (compare < 0) { if (this.left != null) { var result = this.left.Remove(key, out node); if (result) { this.left = node; node = Rebalance(this); node.height = CalculateHeight(node); } return(result); } node = this; return(false); } if (this.right != null) { var result = this.right.Remove(key, out node); if (result) { this.right = node; node = Rebalance(this); node.height = CalculateHeight(node); } return(result); } node = this; return(false); }
internal static void AddItem(TKey key, TValue value, QueuedAVLTree <TKey, TValue> tree, ref int elementCount) { if (!tree.ContainsKey(key)) { tree.queue.CheckRoom(tree); if (tree.head == null) { tree.head = new AVLNode(key, value, tree); elementCount++; goto LukeIDeletedYourFather; } } AVLNode headNode = tree.head; int compare = key.CompareTo(headNode.key); while (true) { if (compare < 0) { if (headNode.left == null) { headNode.left = new AVLNode(key: key, value: value, tree: tree) { head = headNode, isLeft = true }; headNode._depthL = 1; AVLNode.BalanceBubbleUp(headNode, tree); elementCount++; break; } else { headNode = headNode.left; compare = key.CompareTo(headNode.key); } } else if (compare > 0) { if (headNode.right == null) { headNode.right = new AVLNode(key: key, value: value, tree: tree) { head = headNode, isLeft = false }; headNode._depthR = 1; AVLNode.BalanceBubbleUp(headNode, tree); elementCount++; break; } else { headNode = headNode.right; compare = key.CompareTo(headNode.key); } } else { headNode.value = value; break; } } LukeIDeletedYourFather :; }
public int CompareTo(Node other) { return(Key.CompareTo(other.Key)); }
public TValue Search(TKey key) { return(key.CompareTo(Key) switch {