private void _attachToParentEdge(AvlTreeNode <T> node, AvlTreeEdge <T> edge) { Debug.Assert(edge.IsChildOrRoot); // if node != null, node.parent should be free Debug.Assert((node == null) || (node.Parent == null)); // if edge is child then the target should be free; Debug.Assert((edge.IsRoot) || (edge.Target() == null)); if (edge.IsRoot) { _setAsRoot(node); } else if (edge.Direction == AvlTreeNodeDirection.Left) { edge.Source.Left = node; if (node != null) { node.Parent = edge.Source; } } else { edge.Source.Right = node; if (node != null) { node.Parent = edge.Source; } } }
public static AvlTreeEdge <T> FindFree <T>(this AvlTreeNode <T> source, Func <AvlTreeNode <T>, AvlTreeEdge <T> > selector) { if (source == null) { return(AvlTreeEdge <T> .Root); } var current = source; AvlTreeEdge <T> res = null; while (res == null) { var edge = selector(current); var next = edge.Target(); if (next == null) { res = edge; } current = next; } return(res); }
public bool Equals(AvlTreeEdge <T> other) { if (ReferenceEquals(other, null)) { return(false); } return((Source == other.Source) && (Direction == other.Direction)); }
internal AvlTreeNode <T> InternalInsertNode(AvlTreeEdge <T> edge, AvlTreeNode <T> newNode) { Debug.Assert(edge.IsChildOrRoot); if (Count == int.MaxValue) { throw new InvalidOperationException("Maximum size reached"); } _version++; // newnode should be dettached from the tree Debug.Assert(newNode != null); Debug.Assert(newNode.Parent == null); Debug.Assert(newNode.Left == null); Debug.Assert(newNode.Right == null); Debug.Assert(newNode._tree == null); _attachToParentEdge(newNode, edge); _rebalanceAfterChange(edge.Source); return(newNode); }
public static AvlTreeEdge <T> For <T>(AvlTreeNode <T> source, AvlTreeNodeDirection direction) { return(AvlTreeEdge <T> .For(source, direction)); }