protected BinaryNode <T> Remove(BinaryNode <T> node) { if (node == default(BinaryNode <T>)) { return(null); } this.Count--; if (node.LeftChild == null && node.RightChild == null) { if (node.Parent == null) { this.Root = null; } else if (node.Parent.LeftChild == node) { node.Parent.LeftChild = null; } else { node.Parent.RightChild = null; } return(node.Parent); } BinaryNode <T> successer; BinaryNode <T> parent; if (node.LeftChild == null || node.RightChild == null) { successer = node.LeftChild ?? node.RightChild; parent = node.Parent; } else { successer = node.RightChild; while (successer.LeftChild != null) { successer = successer.LeftChild; } parent = successer.Parent; } if (node.Parent == null) { this.Root = successer; successer.Parent = null; } else if (node.Parent.LeftChild == node) { node.Parent.LeftChild = successer; successer.Parent = node.Parent; } else { node.Parent.RightChild = successer; node.LeftChild.Parent = successer; } return(parent); }
public void Clear() { this.Root = null; this.Count = 0; }
protected BinaryNode(T key, BinaryNode <T> parent) { this.Key = key; this.Parent = parent; }
public BinarySearchNode(T key, BinaryNode <T> parent, BinaryNode <T> childA, BinaryNode <T> childB) : base(key, parent, childA, childB) { }
public BinarySearchNode(T key, BinaryNode <T> parent) : base(key, parent) { }
public override bool Remove(T item) { BinaryNode <T> node = Search(item); if (node == null) { return(false); } BinaryNode <T> n = Remove(node); if (n == null) { return(true); } BinaryNode <T> g; BinaryNode <T> z; int b; for (BinaryNode <T> x = n.Parent; x != null; x = g) { g = x.Parent; if (n.IsLeftChild) { if (x.Balance > 0) { z = x.RightChild; b = z.Balance; if (b < 0) { n = RotateRightLeft(x, z); } else { n = RotateRight(x, z); } } else { if (x.Balance == 0) { x.Balance = 1; break; } n = x; n.Balance = 0; continue; } } else { if (x.Balance < 0) { z = x.LeftChild; b = z.Balance; if (b > 0) { n = RotateLeftRight(x, z); } else { n = RotateLeft(x, z); } } else { if (x.Balance == 0) { x.Balance = -1; break; } n = x; n.Balance = 0; continue; } } n.Parent = g; if (g != null) { if (g.LeftChild == x) { g.LeftChild = n; } else { g.RightChild = n; } if (b == 0) { break; } } else { this.Root = n; } } return(true); }
public override BinaryNode <T> Insert(T key) { BinaryNode <T> node = base.Insert(key); BinaryNode <T> z = node; BinaryNode <T> g; BinaryNode <T> n; for (BinaryNode <T> x = z.Parent; x != null; x = z.Parent) { if (z.IsRightChild) { if (x.Balance > 0) { g = x.Parent; n = z.Balance < 0 ? RotateRightLeft(x, z) : RotateLeft(x, z); } else { if (x.Balance < 0) { x.Balance = 0; break; } x.Balance = 1; z = x; continue; } } else { if (x.Balance < 0) { g = x.Parent; n = z.Balance > 0 ? RotateLeftRight(x, z) : RotateRight(x, z); } else { if (x.Balance > 0) { x.Balance = 0; break; } z = x; x.Balance = -1; continue; } } n.Parent = g; if (g != null) { if (g.LeftChild == x) { g.LeftChild = n; } else { g.RightChild = n; } break; } this.Root = n; break; } return(node); }