internal static void RemoveNode(AVLNode node, object[] HashMap, int hash, ref int elementCount) { if (node.right == null && node.left == null) // no children { if (node.head == null) // was the top node { HashMap[hash] = null; } else { if (node.isLeft) { node.head.left = null; node.head._depthL = 0; } else { node.head.right = null; node.head._depthR = 0; } AVLNode.BalanceSelfBubbleUp(node.head, HashMap, hash); } } else if (node.right == null || node.left == null) // one child { AVLNode child = node.right != null ? node.right : node.left; if (node.head == null) // was the top node { HashMap[hash] = child; child.head = null; } else { child.isLeft = node.isLeft; if (node.isLeft) { node.head.left = child; child.head = node.head; node.head._depthL -= 1; } else { node.head.right = child; child.head = node.head; node.head._depthR -= 1; } AVLNode.BalanceSelfBubbleUp(node.head, HashMap, hash); } } else // two children :O { AVLNode child = node.right, childhead = node.head; while (child.left != null) { childhead = child; child = child.left; } if (childhead != node.head) { if (child.right != null) { childhead.left = child.right; child.right.head = childhead; child.right.isLeft = true; childhead._depthL--; } else { childhead.left = null; childhead._depthL = 0; } child.right = node.right; } child.left = node.left; child.left.head = child; child.head = node.head; child.isLeft = node.isLeft; if (node.head == null) { HashMap[hash] = child; } else { if (node.isLeft) { node.head.left = child; } else { node.head.right = child; } } if (childhead == node.head) { AVLNode.BalanceSelfBubbleUp(child, HashMap, hash); } else { child.right.head = child; AVLNode.BalanceSelfBubbleUp(childhead, HashMap, hash); } } elementCount--; }
internal static void RemoveNode(AVLNode node, QueuedAVLTree <TKey, TValue> tree, bool callFromQueue = false) { if (!callFromQueue) { tree.queue.Dequeue(node.linkedElement, tree); } if (node.right == null && node.left == null) // no children { if (node.head == null) // was the top node { tree.head = null; } else { if (node.isLeft) { node.head.left = null; node.head._depthL = 0; } else { node.head.right = null; node.head._depthR = 0; } AVLNode.BalanceSelfBubbleUp(node.head, tree); } } else if (node.right == null || node.left == null) // one child { AVLNode child = node.right != null ? node.right : node.left; if (node.head == null) // was the top node { tree.head = child; child.head = null; } else { child.isLeft = node.isLeft; if (node.isLeft) { node.head.left = child; child.head = node.head; node.head._depthL -= 1; } else { node.head.right = child; child.head = node.head; node.head._depthR -= 1; } AVLNode.BalanceSelfBubbleUp(node.head, tree); } } else // two children :O { AVLNode child = node.right, childhead = node.head; while (child.left != null) { childhead = child; child = child.left; } if (childhead != node.head) { if (child.right != null) { childhead.left = child.right; child.right.head = childhead; child.right.isLeft = true; childhead._depthL--; } else { childhead.left = null; childhead._depthL = 0; } child.right = node.right; } child.left = node.left; child.left.head = child; child.head = node.head; child.isLeft = node.isLeft; if (node.head == null) { tree.head = child; } else { if (node.isLeft) { node.head.left = child; } else { node.head.right = child; } } if (childhead == node.head) { AVLNode.BalanceSelfBubbleUp(child, tree); } else { child.right.head = child; AVLNode.BalanceSelfBubbleUp(childhead, tree); } } tree.count--; }