/// <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> /// 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); }
/// <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> /// Searches for the element that matches the conditions defined by the specified /// </summary> /// <param name="node"></param> /// <param name="data"></param> /// <returns></returns> public Node <T> FindNode(Node <T> node) { Node <T> temp = root; if (node == null) { return(null); } while (temp != null) { if (temp.CompareTo(node) == 0) { return(temp); } if (temp > node) { temp = temp.Left; } else { temp = temp.Right; } } return(null); }
public bool HasValue(T value) { if (Top == null) { return(false); } Node <T> current = Top; int compare; while (current != null) { compare = current.CompareTo(value); if (compare < 0) { current = current.Right; } else if (compare > 0) { current = current.Left; } else { return(true); } } return(false); }
private Node <T> FindParent(T value) { Node <T> current = Top; Node <T> parent = null; if (Top.CompareTo(value) == 0) { current = Top.Right; parent = Top; } while (current != null) { int result = current.CompareTo(value); if (result > 0) { parent = current; current = current.Left; } else if (result < 0) { parent = current; current = current.Right; } else { return(parent); } } return(null); }
/// <summary> /// locate target node and iterate one step further to locate successor node; /// store the successor node in reference, then return /// </summary> /// <param name="root">start searching from root</param> /// <param name="target">target node</param> /// <param name="gate">true if we found target node; one more step is the successor node</param> /// <param name="result">store the successor if we have one</param> public void InOrderSuccessorFinder(Node <T> root, Node <T> target, ref bool gate, ref Node <T> result) { if (root.Left != null) { InOrderSuccessorFinder(root.Left, target, ref gate, ref result); // search root's left child } if (gate) { result = root; gate = false; return; } // located the inorder successor -> stop searching return if (root.CompareTo(target) == 0) { gate = true; // located the same node -> open gate and next node is inorder successor } if (root.Right != null) { InOrderSuccessorFinder(root.Right, target, ref gate, ref result); // search root's right child } }
public void Add(T value) { if (Top == null) { Top = new Node <T>(value); Count++; return; } Node <T> current = Top; bool hasAdd = false; while (!hasAdd) { if (current.CompareTo(value) < 1) { if (current.Right == null) { current.Right = new Node <T>(value); hasAdd = true; } else { current = current.Right; } } else { if (current.Left == null) { current.Left = new Node <T>(value); hasAdd = true; } else { current = current.Left; } } } Count++; }
public bool Remove(T value) { if (Top == null) { return(false); } else if (Top.CompareTo(value) == 0) { if (Top.Left == null && Top.Right == null) { Top = null; } else if (Top.Left != null && Top.Right == null) { Top = Top.Left; } else if (Top.Left == null && Top.Right != null) { Top = Top.Right; } else { Node <T> minLeft = Top.Right; while (minLeft.Left != null) { minLeft = minLeft.Left; } Node <T> newNode = new Node <T>(minLeft.Data, Top.Left, Top.Right); Top = newNode; if (Top.Right == minLeft) { Top.Right = Top.Right.Right; } else if (minLeft.Right == null) { FindParent(minLeft.Data).Left = null; } else { FindParent(minLeft.Data).Left = FindParent(minLeft.Data).Left.Right; } } Count--; return(true); } else { if (FindParent(value) == null) { return(false); } else { Node <T> parent = FindParent(value); Node <T> deleteNode = null; int leftOrRight = parent.CompareTo(value); if (leftOrRight > 0) { deleteNode = parent.Left; } else { deleteNode = parent.Right; } if (deleteNode.Right == null && deleteNode.Left == null) { if (leftOrRight > 0) { parent.Left = null; } else { parent.Right = null; } } else if (deleteNode.Right == null && deleteNode.Left != null) { if (leftOrRight > 0) { parent.Left = parent.Left.Left; } else { parent.Right = parent.Right.Left; } } else if (deleteNode.Right != null && deleteNode.Left == null) { if (leftOrRight > 0) { parent.Left = parent.Left.Right; } else { parent.Right = parent.Right.Right; } } else { Node <T> minLeft = deleteNode.Right; while (minLeft.Left != null) { minLeft = minLeft.Left; } if (deleteNode.Right == minLeft) { deleteNode.Right = deleteNode.Right.Right; } else if (minLeft.Right == null) { Node <T> parentMinLeft = FindParent(minLeft.Data); parentMinLeft.Left = null; } else { Node <T> parentMinLeft = FindParent(minLeft.Data); parentMinLeft.Left = parentMinLeft.Left.Right; } Node <T> newNode = new Node <T>(minLeft.Data, deleteNode.Left, deleteNode.Right); if (leftOrRight > 0) { parent.Left = newNode; } else { parent.Right = newNode; } } Count--; return(true); } } }