protected virtual BinarySearchTreeNode <T> DoDeleteRecursive(T value, BinarySearchTreeNode <T> root, ref bool result) { if (root == null) { result = false; return(root); } int comparisonResult = Compare(root.Value, value); if (comparisonResult == 0) { if (root.IsFull) { BinarySearchTreeNode <T> .ExchangeValues(root, DoGetMaximum(root.Left)); root.SetLeft(DoDeleteRecursive(value, root.Left, ref result)); } else { root = root.Left ?? root.Right; } } else if (comparisonResult < 0) { root.SetRight(DoDeleteRecursive(value, root.Right, ref result)); } else { root.SetLeft(DoDeleteRecursive(value, root.Left, ref result)); } return(root); }
BinaryTreeNodeBase <T> DoInsertRecursive(BinarySearchTreeNode <T> root, BinarySearchTreeNode <T> node, Action <T, T> visitAction) { if (root == null) { node.SetHeight(0); return(node); } if (visitAction != null) { visitAction(root.Value, node.Value); } int comparisonResult = Compare(node.Value, root.Value); if (comparisonResult == 0) { return(root); } if (comparisonResult < 0) { root.SetLeft(DoInsertRecursive(root.Left, node, visitAction)); root = EnsureBalanced(root, x => Compare(x.Left.Value, node.Value) > 0, RotateLL, RotateLR); } else { root.SetRight(DoInsertRecursive(root.Right, node, visitAction)); root = EnsureBalanced(root, x => Compare(x.Right.Value, node.Value) > 0, RotateRL, RotateRR); } root.SetHeight(Math.Max(GetHeight(root.Left), GetHeight(root.Right)) + 1); return(root); }
internal static BinarySearchTreeNode <T> RotateLR(BinarySearchTreeNode <T> node) { BinarySearchTreeNode <T> left = RotateRR(node.Left); node.SetLeft(left); return(RotateLL(node)); }
BinaryTreeNodeBase <T> DoInsertRecursive(BinarySearchTreeNode <T> root, BinaryTreeNodeBase <T> node, Action <T, T> visitAction) { if (root == null) { return(node); } if (visitAction != null) { visitAction(root.Value, node.Value); } int comparisonResult = Compare(node.Value, root.Value); if (comparisonResult == 0) { return(root); } if (comparisonResult < 0) { root.SetLeft(DoInsertRecursive(root.Left, node, visitAction)); } else { root.SetRight(DoInsertRecursive(root.Right, node, visitAction)); } return(root); }
internal static BinarySearchTreeNode <T> RotateRR(BinarySearchTreeNode <T> node) { BinarySearchTreeNode <T> right = node.Right; BinarySearchTreeNode <T> l = right.Left; right.SetLeft(node); node.SetRight(l); node.SetHeight(Math.Max(GetHeight(node.Left), GetHeight(node.Right)) + 1); right.SetHeight(Math.Max(GetHeight(right.Left), GetHeight(right.Right)) + 1); return(right); }
internal static BinarySearchTreeNode <T> RotateLL(BinarySearchTreeNode <T> node) { BinarySearchTreeNode <T> left = node.Left; BinarySearchTreeNode <T> r = left.Right; left.SetRight(node); node.SetLeft(r); node.SetHeight(Math.Max(GetHeight(node.Left), GetHeight(node.Right)) + 1); left.SetHeight(Math.Max(GetHeight(left.Left), GetHeight(left.Right)) + 1); return(left); }
protected override BinarySearchTreeNode <T> DoDeleteRecursive(T value, BinarySearchTreeNode <T> root, ref bool result) { if (root == null) { result = false; return(root); } int comparisonResult = Compare(root.Value, value); if (comparisonResult == 0) { if (root.IsFull) { BinarySearchTreeNode <T> .ExchangeValues(root, DoGetMaximum(root.Left)); root.SetLeft(DoDeleteRecursive(value, root.Left, ref result)); root = EnsureBalanced(root, x => GetHeight(x.Right.Left) > GetHeight(x.Right.Right), RotateRL, RotateRR); } else { root = root.Left ?? root.Right; } } else if (comparisonResult < 0) { root.SetRight(DoDeleteRecursive(value, root.Right, ref result)); root = EnsureBalanced(root, x => GetHeight(x.Left.Left) > GetHeight(x.Left.Right), RotateLL, RotateLR); } else { root.SetLeft(DoDeleteRecursive(value, root.Left, ref result)); root = EnsureBalanced(root, x => GetHeight(x.Right.Left) > GetHeight(x.Right.Right), RotateRL, RotateRR); } if (root != null) { root.SetHeight(Math.Max(GetHeight(root.Left), GetHeight(root.Right)) + 1); } return(root); }