/// <returns>Root of tree after turn</returns> public static AvlTree <T> SmallRightTurn(AvlTree <T> root) { AvlTree <T> child = root.Left; AvlTree <T> parent = root.Parent; AvlTree <T> x = root.Right; AvlTree <T> y = root.Left.Left; AvlTree <T> z = root.Left.Right; //Parents child.Parent = parent; root.Parent = child; if (x != null) { x.Parent = root; } if (y != null) { y.Parent = child; } if (z != null) { z.Parent = root; } //Childs root.Left = z; root.Right = x; child.Left = y; child.Right = root; if (parent != null) { if (parent.Right == root) { parent.Right = child; } else { parent.Left = child; } } //Heights long xH = x != null ? x.Height : -1; long yH = y != null ? y.Height : -1; long zH = z != null ? z.Height : -1; if (zH > xH) { root.Height = zH + 1; } else { root.Height = xH + 1; } if (y.Height > root.Height) { child.Height = yH + 1; } else { child.Height = root.Height + 1; } UpdateHeight(child); return(child); }
/// <returns>Root of tree after turn</returns> public static AvlTree <T> BigLeftTurn(AvlTree <T> root) { AvlTree <T> w = root.Left; AvlTree <T> parent = root.Parent; AvlTree <T> b = root.Right; AvlTree <T> c = root.Right.Left; AvlTree <T> z = b.Right; AvlTree <T> x = c.Left; AvlTree <T> y = c.Right; //Parents c.Parent = parent; b.Parent = c; root.Parent = c; if (w != null) { w.Parent = root; } if (z != null) { z.Parent = b; } if (y != null) { y.Parent = b; } if (x != null) { x.Parent = root; } //Childs if (parent != null) { if (parent.Right == root) { parent.Right = c; } else { parent.Left = c; } } c.Left = root; c.Right = b; b.Left = y; b.Right = z; root.Left = w; root.Right = x; //Heights long xH = x != null ? x.Height : -1; long yH = y != null ? y.Height : -1; long zH = z != null ? z.Height : -1; long wH = w != null ? w.Height : -1; if (wH > xH) { root.Height = wH + 1; } else { root.Height = xH + 1; } if (yH > zH) { b.Height = yH + 1; } else { b.Height = zH + 1; } if (b.Height > root.Height) { c.Height = b.Height + 1; } else { c.Height = root.Height + 1; } UpdateHeight(c); return(c); }
/// <returns>Root of tree after remove</returns> public static AvlTree <T> Remove(AvlTree <T> item) { AvlTree <T> parent = item.Parent; //Leaf if (item.Left == null && item.Right == null) { if (parent == null) { return(null); } if (parent.Left == item) { parent.Left = null; } else { parent.Right = null; } UpdateHeight(parent); return(Balance(parent)); } //One child if ((item.Left == null) ^ (item.Right == null)) { if (item.Left != null) { if (parent != null) { if (parent.Left == item) { parent.Left = item.Left; } else { parent.Right = item.Left; } UpdateHeight(parent); } item.Left.Parent = parent; return(Balance(item.Left)); } else { if (parent != null) { if (parent.Left == item) { parent.Left = item.Right; } else { parent.Right = item.Right; } UpdateHeight(parent); } item.Right.Parent = parent; return(Balance(item.Right)); } } //Two child if ((item.Left != null) && (item.Right != null)) { AvlTree <T> prev = Previous(item); Remove(prev); item.Key = prev.Key; } return(Balance(item)); }