private static void Zig(BinarySearchTreeNode node, BinarySearchTreeNode.SideOfParent side) { var p = node.Parent; BinarySearchTreeNode.ReplaceChild(p.Parent, p, node); if (side == BinarySearchTreeNode.SideOfParent.LEFT) { var nr = node.Right; node.Right = p; p.Left = nr; } else { var nl = node.Left; node.Left = p; p.Right = nl; } }
private BinarySearchTreeNode <T> Insert(ref BinarySearchTreeNode <T> currentNode, T value) { if (currentNode == null) { currentNode = new BinarySearchTreeNode <T>(value); return(currentNode); } if (value.CompareTo(currentNode.Value) <= 0) { Insert(ref currentNode.Left, value); } else { Insert(ref currentNode.Right, value); } return(currentNode); }
public BinarySearchTreeNode Delete(BinarySearchTreeNode root, int data) { if (root == null) { return(null); } if (data < root.Data) { Delete(root.left, data); } else if (data > root.Data) { Delete(root.right, data); } else { if (root.left == null && root.right == null) { root = null; return(root); } if (root.left != null && root.right == null) { root = root.left; return(root); } else if (root.left != null && root.right == null) { root = root.right; return(root); } if (root.left != null && root.right != null) { var replacementNode = Min(root.right); root.Data = replacementNode.Data; root.right = Delete(replacementNode, replacementNode.Data); } } return(root); }
public static BinarySearchTreeNode Delete(BinarySearchTreeNode node) { if (node.Right == null) { var nl = node.Left; BinarySearchTreeNode.ReplaceChild(node.Parent, node, nl); node.Erase(); return(nl); } var next = Next(node); BinarySearchTreeNode.ReplaceChild(next.Parent, next, next.Right); BinarySearchTreeNode.ReplaceChild(node.Parent, node, next); next.Left = node.Left; next.Right = node.Right; node.Erase(); return(next); }
public static void Insert(long key, BinarySearchTreeNode root) { var parent = Find(key, root); if (parent.Key == key) { return; } //Add as child var node = new BinarySearchTreeNode { Key = key, Parent = parent }; if (parent.Key > key) { parent.Left = node; } else { parent.Right = node; } }
private BinarySearchTreeNode <T> Search(BinarySearchTreeNode <T> currentNode, T value) { if (currentNode == null) { return(null); } if (currentNode.Value.CompareTo(value) == 0) { return(currentNode); } if (currentNode.Left != null && value.CompareTo(currentNode.Value) <= 0) { return(Search(currentNode.Left, value)); } if (currentNode.Right != null) { return(Search(currentNode.Right, value)); } return(null); }
public static void Delete(BinarySearchTreeNode node) { BinarySearchTree.Delete(node); Rebalance(node); }
public static BinarySearchTreeNode Next(BinarySearchTreeNode node) { return((node.Right != null) ? LeftDescendant(node.Right) : RightAncestor(node)); }
public static IEnumerable <BinarySearchTreeNode> RangeSearch(long leftKey, long rightKey, BinarySearchTreeNode root) { var results = new List <BinarySearchTreeNode>(); var node = Find(leftKey, root); while (node != null && node.Key <= rightKey) { if (node.Key >= leftKey) { results.Add(node); } node = Next(node); } return(results); }
private static void MergeAsRoot(BinarySearchTreeNode left, BinarySearchTreeNode right, BinarySearchTreeNode root) { root.Left = left; root.Right = right; left.Parent = root; right.Parent = root; }
public static void Insert(long key, BinarySearchTreeNode root) { BinarySearchTree.Insert(key, root); Rebalance(root); }
private static BinarySearchTreeNode TreeMergeWithRoot(BinarySearchTreeNode left, BinarySearchTreeNode right, BinarySearchTreeNode root) { if (Math.Abs(left.Rank - right.Rank) <= 1) { MergeAsRoot(left, right, root); AdjustHeight(root); //return this; } else if (left.Rank > right.Rank) { var rightPrime = TreeMergeWithRoot(left.Right, right, root); left.Right = rightPrime; rightPrime.Parent = left; Rebalance(left); //return this; } else if (left.Rank < right.Rank) { var leftPrime = TreeMergeWithRoot(left, right.Right, root); right.Right = leftPrime; leftPrime.Parent = right; Rebalance(right); //return this; } return(root); }
public static BinarySearchTreeNode Delete(long key, BinarySearchTreeNode root) { var node = Find(key, root); return((node.Key == key) ? Delete(node) : node); }
public BinarySearchTreeNode(int data) { Data = data; left = null; right = null; }
private static BinarySearchTreeNode Delete(BinarySearchTreeNode node) { Splay(BinarySearchTree.Next(node)); Splay(node); return(BinarySearchTree.Delete(node)); }
public BinarySearchTreeNodePrinter(BinarySearchTreeNode node) { _original = node; _sb = new StringBuilder(); }
/// <summary> /// Inserts and returns item /// </summary> /// <param name="value">Item value to be inserted</param> /// <returns></returns> public BinarySearchTreeNode <T> Insert(T value) { var node = Root; return(Root = Insert(ref node, value)); }
public void Insert(int data) { root = InsertRec(root, data); }
private static void AdjustHeight(BinarySearchTreeNode node) { node.Rank = 1 + Math.Max(node.Left.Rank, node.Right.Rank); }
public static Tuple <BinarySearchTreeNode, BinarySearchTreeNode> Split(long key, BinarySearchTreeNode root) { if (key < root.Right.Key) { var s = Split(key, root.Left); var three = TreeMergeWithRoot(s.Item2, root.Right, root); return(new Tuple <BinarySearchTreeNode, BinarySearchTreeNode>(s.Item1, three)); } else { var s = Split(key, root.Right); var three = TreeMergeWithRoot(s.Item2, root.Left, root); return(new Tuple <BinarySearchTreeNode, BinarySearchTreeNode>(s.Item1, three)); } }
public static BinarySearchTreeNode Insert(long key, BinarySearchTreeNode root) { BinarySearchTree.Insert(key, root); return(Find(key, root)); }