public BinNode <T> InsertAsRC(BinNode <T> node, T e) { size += 1; node.InsertAsRC(e); UpdateHeightAbove(node); return(node.RC); }
public virtual BinNode <T> Insert(T e) { var n = Search(e); if (n == null) { return(InsertAsRoot(e)); } else { BinNode <T> ret = null; int comRet = comparer.Compare(e, n.data); if (comRet == 0) { return(n); } else if (comRet < 0) { ret = InsertAsLC(n, e); } else { ret = InsertAsRC(n, e); } UpdateHeightAbove(ret); return(ret); } }
void PrintTree(BinNode <T> node, int depth, BinNode <T> .Type type) { if (node == null) { return; } AddTypes(depth, type); PrintTree(node.RC, depth + 1, BinNode <T> .Type.RC); Console.Write(node.data.ToString()); for (int i = 0; i < depth; i++) { if (bTypes[i] + bTypes[i + 1] == 1) { Console.Write(" "); } else { Console.Write("| "); } } if (type == BinNode <T> .Type.RC) { Console.Write("┌──"); } else if (type == BinNode <T> .Type.LC) { Console.Write("└──"); } else { Console.Write("───"); } Console.WriteLine(node.data.ToString()); PrintTree(node.LC, depth + 1, BinNode <T> .Type.LC); }
public BinNode <T> RotateAt(BinNode <T> n) { var p = n.Parent; var g = p.Parent; if (p.IsLC()) //zig { if (n.IsLC()) //zig-zag { p.Parent = g.Parent; return(Connect34(n, p, g, n.LC, n.RC, p.RC, g.RC)); } else { n.Parent = g.Parent; return(Connect34(p, n, g, p.LC, n.LC, n.RC, g.RC)); } } else //zag { if (n.IsRC()) //zag-zig { p.Parent = g.Parent; return(Connect34(g, p, n, g.LC, p.LC, n.LC, n.RC)); } else { n.Parent = g.Parent; return(Connect34(g, n, p, g.LC, n.LC, n.RC, p.RC)); } } }
public BinNode <T> Connect34(BinNode <T> a, BinNode <T> b, BinNode <T> c, BinNode <T> st1, BinNode <T> st2, BinNode <T> st3, BinNode <T> st4) { a.LC = st1; if (st1 != null) { st1.Parent = a; } a.RC = st2; if (st2 != null) { st2.Parent = a; } UpdateHeight(a); c.LC = st3; if (st3 != null) { st3.Parent = c; } c.RC = st4; if (st4 != null) { st4.Parent = c; } UpdateHeight(c); b.LC = a; a.Parent = b; b.RC = c; c.Parent = b; UpdateHeight(b); return(b); }
public static void UpdateHeight(BinNode <T> node) { if (node == null) { return; } node.UpdateHeight(); }
public static void UpdateHeightAbove(BinNode <T> node) { while (node != null) { node.UpdateHeight(); node = node.Parent; } }
void AddTypes(int depth, BinNode <T> .Type type) { if (!bTypes.ContainsKey(depth)) { bTypes.Add(depth, (int)type); } bTypes[depth] = (int)type; }
public static void GetDatasInRecursive(BinNode <T> n, List <BinNode <T> > lst) { if (n == null) { return; } GetDatasInRecursive(n.lc, lst); lst.Add(n); GetDatasInRecursive(n.rc, lst); }
public BinNode <T> RotateLeft(BinNode <T> n) { if (n == null) { return(null); } bool isRoot = n.IsRoot(); var p = n.RotateLeft(); if (isRoot) { Root = p; } return(p); }
public BinNode <T> RemoveAt(BinNode <T> n) { BinNode <T> ret = null; if (n.HasRC() && n.HasLC()) { var succ = n.Succ; ret = succ.Parent; var temp = succ.data; succ.data = n.data; n.data = temp; var succParent = succ.Parent; if (succParent == n) { succParent.RC = succ.RC; } else { succParent.LC = succ.RC; } if (succ.RC != null) { succ.RC.Parent = succParent; } } else { ret = n.Parent; var child = n.HasLC() ? n.LC : n.RC; if (child != null) { child.Parent = n.Parent; } if (n.IsRoot()) { this.Root = child; } else if (n.IsLC()) { n.Parent.LC = child; } else { n.Parent.RC = child; } } return(ret); }
void Rotated(BinNode <T> n) { var type = n.NodeType; var currParent = n.Parent; var rotatedRoot = RotateAt(n.TallerChild().TallerChild()); if (type == BinNode <T> .Type.Root) { Root = rotatedRoot; } else if (type == BinNode <T> .Type.LC) { currParent.LC = rotatedRoot; } else { currParent.RC = rotatedRoot; } }
public BinNode <T> Remove(BinNode <T> n) { if (n != null) { var g = RemoveAt(n); var curr = g; Size -= 1; while (curr != null) { if (!curr.AVLBalanced()) { Rotated(curr); } UpdateHeight(curr); curr = curr.Parent; } return(g); } return(null); }
public BinNode <T> SearchIn(T e, BinNode <T> node) { while (node != null && comparer != null) { int comRet = comparer.Compare(e, node.data); if (comRet == 0) { return(node); } else if (comRet < 0 && node.HasLC()) { node = node.LC; } else if (comRet > 0 && node.HasRC()) { node = node.RC; } else { return(node); } } return(null); }
public BinNode(T d, BinNode <T> p = null, BinNode <T> lc = null, BinNode <T> rc = null, int h = 0) { this.data = d; this.Parent = p; this.lc = lc; this.rc = rc; this.height = h; }
public static int Stature <T>(this BinNode <T> node) { return(node != null ? node.Height : -1); }
public BinNode <T> InsertAsRoot(T e) { size = 1; root = new BinNode <T>(e); return(root); }
public BinNode <T> InsertAsRC(T d) { return(this.rc = new BinNode <T>(d, this)); }