/// <summary> /// Searches for an element that matches the conditions defined by the specified /// </summary> /// <param name="node"></param> /// <returns></returns> public Node <T> FindNode(Node <T> node) { if (node == null) { return(null); } Node <T> temp = root; while (temp != null) { if (temp.CompareTo(node) == 0) { return(temp); } if (temp > node) { temp = temp.Left; } else { temp = temp.Right; } } return(null); //throw new NotImplementedException(); }
/// <summary> /// Searches for an element that matches the conditions defined by the specified /// </summary> /// <typeparam name="T"></typeparam> /// <param name="root"></param> /// <param name="node"></param> /// <returns></returns> public static Node <T> FindNode <T>(this Node <T> root, Node <T> node) where T : IComparable { if (node == null) { return(null); } Node <T> temp = root; while (temp != null) { if (temp.CompareTo(node) == 0) { return(temp); } if (temp > node) { temp = temp.Left; } else { temp = temp.Right; } } return(null); }
/// <summary> /// Searches for an parent of element that matches the conditions defined by the specified /// </summary> /// <param name="node"></param> /// <returns></returns> public Tuple <Node <T>, int> FindParent(Node <T> node) { int check = 0; if (node == null) { return(null); } Node <T> temp = root; Node <T> parent = null; while (temp != null) { if (temp.CompareTo(node) == 0) { return(new Tuple <Node <T>, int>(parent, check));// temp; } if (temp > node) { parent = temp; check = -1; temp = temp.Left; } else { parent = temp; check = 1; temp = temp.Right; } } return(null); }
/// <summary> /// Determines whether an element is in the BST /// </summary> /// <param name="node"></param> /// <returns></returns> public bool Contains(Node <T> nodeMatch, Node <T> nodeRoot) { Node <T> temp = nodeRoot; if (nodeMatch == null) { return(false); } while (temp != null) { if (temp.CompareTo(nodeMatch) == 0) { return(true); } if (temp > nodeMatch) { temp = temp.Left; } else { temp = temp.Right; } } return(false); }
private int _insert(TK key, TV value) { var newNode = new Node <TK, TV>(key, value); var nodes = SearchNodes(key); var previousNode = nodes.Item1; var currentNode = nodes.Item2; Tuple <Node <TK, TV>, Node <TK, TV>, bool> newChildren; if (currentNode != null) { if (!currentNode.ChildrenM.Item3) { return(0); } newNode.ChildrenM = currentNode.ChildrenM; } if (previousNode == null) { if (null != CompareExchange(ref _root, newNode, null)) { return(-1); } return(0); } var oldChildren = previousNode.ChildrenM; switch (newNode.CompareTo(previousNode)) { case 1: newChildren = new Tuple <Node <TK, TV>, Node <TK, TV>, bool> (oldChildren.Item1, newNode, oldChildren.Item3); if (!ReferenceEquals(oldChildren, CompareExchange(ref previousNode.ChildrenM, newChildren, oldChildren))) { return(-1); } break; case -1: newChildren = new Tuple <Node <TK, TV>, Node <TK, TV>, bool> (newNode, oldChildren.Item2, oldChildren.Item3); if (!ReferenceEquals(oldChildren, CompareExchange(ref previousNode.ChildrenM, newChildren, oldChildren))) { return(-1); } break; default: throw new ArgumentException("Unexpected return in Node's CompareTo"); } return(0); }
public int Rank(Node <T> node, Node <T> nodeTree) { if (nodeTree == null) { return(0); } int cmp = node.CompareTo(nodeTree); if (cmp < 0) { return(Rank(node, nodeTree.Left)); } else if (cmp > 0) { return(1 + Size(nodeTree.Left) + Rank(node, nodeTree.Right)); } else { return(Size(nodeTree.Left)); } }
/// <summary> /// Remove a element in BST /// </summary> /// <param name="item"></param> /// <returns></returns> private bool Remove(Node <T> item, Node <T> nodeTree) { Node <T> node = nodeTree; if (item == null) { return(false); } while (node != null) { if (node.CompareTo(item) == 0) { if (node.Right == null && node.Left == null)//no child { var parent = FindParent(node); if (parent.Item1 == null) { root = null; return(false); } parent.Item1.Left = parent.Item2 == -1 ? null : parent.Item1.Left; parent.Item1.Right = parent.Item2 == 1 ? null : parent.Item1.Right; return(true); } if (node.Right == null) { node.Data = node.Left.Data; var right = node.Left == null ? null : node.Left.Right; node.Left = node.Left == null ? null : node.Left.Left; node.Right = right; return(true); } else if (node.Left == null) { node.Data = node.Right.Data; var left = node.Right == null ? null : node.Right.Left; node.Right = node.Right == null ? null : node.Right.Right; node.Left = left; return(true); } else//two child { var suc = Successor(node) as Node <T>;// (T)node.Successor(); //var nodeFind = FindNode(suc); var parent = FindParent(suc); if (parent.Item1.Data.Equals(item.Data)) { parent.Item1.Right = suc.Right; node.Data = suc.Data; return(true); } if (suc.Right != null) { parent.Item1.Right = suc.Right; } else { parent.Item1.Left = null; } node.Data = suc.Data; return(true); } } if (node < item) { node = node.Right; } else { node = node.Left; } } return(false); //throw new NotImplementedException(); }