static void DeleteNode(SharpTreeNode node) { SharpTreeNode balancingNode; if (node.left == null) { balancingNode = node.listParent; node.ReplaceWith(node.right); node.right = null; } else if (node.right == null) { balancingNode = node.listParent; node.ReplaceWith(node.left); node.left = null; } else { SharpTreeNode tmp = node.right; while (tmp.left != null) { tmp = tmp.left; } // First replace tmp with tmp.right balancingNode = tmp.listParent; tmp.ReplaceWith(tmp.right); tmp.right = null; Debug.Assert(tmp.left == null); Debug.Assert(tmp.listParent == null); // Now move node's children to tmp: tmp.left = node.left; node.left = null; tmp.right = node.right; node.right = null; if (tmp.left != null) { tmp.left.listParent = tmp; } if (tmp.right != null) { tmp.right.listParent = tmp; } // Then replace node with tmp node.ReplaceWith(tmp); if (balancingNode == node) { balancingNode = tmp; } } Debug.Assert(node.listParent == null); Debug.Assert(node.left == null); Debug.Assert(node.right == null); node.height = 1; node.totalListLength = -1; if (balancingNode != null) { RebalanceUntilRoot(balancingNode); } }
static void DeleteNode(SharpTreeNode node) { SharpTreeNode balancingNode; if (node.left == null) { balancingNode = node.listParent; node.ReplaceWith(node.right); node.right = null; } else if (node.right == null) { balancingNode = node.listParent; node.ReplaceWith(node.left); node.left = null; } else { SharpTreeNode tmp = node.right; while (tmp.left != null) tmp = tmp.left; // First replace tmp with tmp.right balancingNode = tmp.listParent; tmp.ReplaceWith(tmp.right); tmp.right = null; Debug.Assert(tmp.left == null); Debug.Assert(tmp.listParent == null); // Now move node's children to tmp: tmp.left = node.left; node.left = null; tmp.right = node.right; node.right = null; if (tmp.left != null) tmp.left.listParent = tmp; if (tmp.right != null) tmp.right.listParent = tmp; // Then replace node with tmp node.ReplaceWith(tmp); if (balancingNode == node) balancingNode = tmp; } Debug.Assert(node.listParent == null); Debug.Assert(node.left == null); Debug.Assert(node.right == null); node.height = 1; node.totalListLength = -1; if (balancingNode != null) RebalanceUntilRoot(balancingNode); }