/// <summary> /// Enables the user to find a node based on the user input, 'val' /// </summary> /// <param name="val">value that the program is looking for in the tree</param> /// <returns></returns> public T Find(T val) { RedBlackNode <T> temp = _root; while (temp != null) { if (temp.Data.CompareTo(val) == 0) { return(temp.Data); } else if (temp.Data.CompareTo(val) > 0) { temp = temp.RightChild; } else { temp = temp.LeftChild; } } return(default(T)); }
/// <summary> /// Adds a new RedBlackNode in the correct location /// </summary> /// <param name="val">node that contains the data</param> public void Add(T val) { RedBlackNode <T> node = new RedBlackNode <T>(val, null, null, null, false); if (_root == null) { _root = node; return; } RedBlackNode <T> root = _root; RedBlackNode <T> newNode = null; while (root != null) { int comparison = val.CompareTo(root.Data); if (comparison > 0) { if (root.RightChild == null) { newNode = new RedBlackNode <T>(val, null, null, root, true);//false or true? root.RightChild = newNode; FixColors(newNode); break; } root = root.RightChild; } else { if (root.LeftChild == null) { newNode = new RedBlackNode <T>(val, null, null, root, true);//false or true? root.LeftChild = newNode; FixColors(newNode); break; } root = root.LeftChild; } } }
/// <summary> /// Gets the information associated with the given name in the given binary search tree. /// If the given name is not in the tree, returns a NameInformation with a null name. /// </summary> /// <param name="name">The name to look for.</param> /// <param name="t">The binary search tree to look in.</param> /// <returns>The NameInformation containing the given name, or a NameInformation with a null name /// if the tree does not contain the name.</returns> private NameInformation GetInformation(string name, RedBlackNode <NameInformation> t) { if (t == null) { return(new NameInformation()); } else { int comp = name.CompareTo(t.Data.Name); if (comp == 0) { return(t.Data); } else if (comp < 0) { return(GetInformation(name, t.LeftChild)); } else { return(GetInformation(name, t.RightChild)); } } }
/// <summary> /// recolors and rebalances the tree /// </summary> /// <param name="lol"> newly added node</param> private void FixColors(RedBlackNode <T> node) { RedBlackNode <T> uncle = null; RedBlackNode <T> newNode = node; while (newNode != _root && newNode.Parent.Red && newNode.Red) { if (newNode.Parent == newNode.Parent.Parent.LeftChild) { uncle = newNode.Parent.Parent.RightChild; } else if (newNode.Parent == newNode.Parent.Parent.RightChild) { uncle = newNode.Parent.Parent.LeftChild; } if (newNode.Parent.Parent.RightChild != null && newNode.Parent.Parent.LeftChild != null && uncle.Red) { if (uncle == newNode.Parent.Parent.RightChild) { newNode.Parent.Parent.Red = true; newNode.Parent.Red = false; newNode.Parent.Parent.RightChild.Red = false; } else { newNode.Parent.Parent.Red = true; newNode.Parent.Red = false; newNode.Parent.Parent.LeftChild.Red = false; } newNode = newNode.Parent.Parent; } else if (uncle == null || uncle.Red == false) { if (newNode == newNode.Parent.LeftChild && newNode.Parent == newNode.Parent.Parent.LeftChild) { RotateRight(newNode.Parent.Parent); newNode.Red = true; newNode.Parent.Red = false; newNode.Parent.RightChild.Red = true; if (newNode.Parent.RightChild.RightChild != null) { newNode.Parent.RightChild.RightChild.Red = false; } } else if (newNode == newNode.Parent.RightChild && newNode.Parent == newNode.Parent.Parent.RightChild) { RotateLeft(newNode.Parent.Parent); //exception thrown newNode.Red = true; newNode.Parent.Red = false; newNode.Parent.LeftChild.Red = true; //true if (newNode.Parent.LeftChild.LeftChild != null) { newNode.Parent.LeftChild.LeftChild.Red = false; } } else if (newNode == newNode.Parent.RightChild && newNode.Parent == newNode.Parent.Parent.LeftChild) //??????? { RotateLeft(newNode.Parent); RotateRight(newNode.Parent); newNode.Red = false; newNode.LeftChild.Red = true; newNode.RightChild.Red = true; if (newNode.RightChild.RightChild != null) { newNode.RightChild.RightChild.Red = false; } } else { RotateRight(newNode.Parent); RotateLeft(newNode.Parent); newNode.Red = false; newNode.RightChild.Red = true; newNode.LeftChild.Red = true; if (newNode.LeftChild.LeftChild != null) { newNode.LeftChild.LeftChild.Red = false; } } } } _root.Red = false; }