private BinaryTreeNode <T> FindWithParent(T value, out BinaryTreeNode <T> parent) { BinaryTreeNode <T> node = head; parent = null; if (node != null) { while (node != null) { if (node.CompareTo(value) > 0) { node = node.Left; // Visit left } else if (node.CompareTo(value) < 0) { node = node.Right; //Visit right } else { //found break; } parent = node; } } return(node); }
private void AddNode(BinaryTreeNode <T> addTo, BinaryTreeNode <T> nodeToAdd) { if (addTo.CompareTo(nodeToAdd) > 0) { if (addTo.LeftNode == null) { addTo.LeftNode = nodeToAdd; nodeToAdd.ParentNode = addTo; TraversalBack(nodeToAdd); return; } AddNode(addTo.LeftNode, nodeToAdd); return; } if (addTo.CompareTo(nodeToAdd) <= 0) { if (addTo.RightNode == null) { addTo.RightNode = nodeToAdd; nodeToAdd.ParentNode = addTo; TraversalBack(nodeToAdd); return; } AddNode(addTo.RightNode, nodeToAdd); } }
private BinaryTreeNode <T> FindWithParent(T value, out BinaryTreeNode <T> parent) { BinaryTreeNode <T> current = _head; parent = null; 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 { break; } } return(current); }
/// <summary> /// Finds and returns the first node containing the specified value. If the value /// is not found, returns null. Also returns the parent of the found node (or null) /// which is used in Remove. /// </summary> /// <param name="value">The value to search for</param> /// <param name="parent">The parent of the found node (or null)</param> /// <returns>The found node (or null)</returns> private BinaryTreeNode <T> FindWithParent(T value, out BinaryTreeNode <T> parent) { // Now, try to find data in the tree BinaryTreeNode <T> current = _head; parent = null; // while we don't have a match while (current != null) { int result = current.CompareTo(value); if (result > 0) { // if the value is less than current, go left. parent = current; current = current.Left; } else if (result < 0) { // if the value is more than current, go right. parent = current; current = current.Right; } else { // we have a match! break; } } return(current); }
private BinaryTreeNode <T> FindWithParent(T value, out BinaryTreeNode <T> parent) { // Метод FindWithParent возвращает первый найденный узел. Если значение не // найдено, метод возвращает Null. Так же возвращает родительский узел для // найденного значения BinaryTreeNode <T> current = _head; parent = null; 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 { //Искомый элемент не найден break; } } return(current); }
private void AddTo(BinaryTreeNode <T> node, T value) { var result = node.CompareTo(value); if (result > 0) { if (node.Left == null) { node.Left = new BinaryTreeNode <T>(value); } else { AddTo(node.Left, value); } } else { if (node.Right == null) { node.Right = new BinaryTreeNode <T>(value); } else { AddTo(node.Right, value); } } }
// Finds and returns the first node containing the specified value. // If the value is not found, it returns null. // Also returns the parent of the found node (or null). private BinaryTreeNode <T> FindWithParent(T value, out BinaryTreeNode <T> parent) { // Try to find the data in the tree BinaryTreeNode <T> current = head; parent = null; // While we dont have a match while (current != null) { int result = current.CompareTo(value); // If the value is less than current, go left if (result > 0) { parent = current; current = current.Left; } // If the value is more than current, go right else if (result < 0) { parent = current; current = current.Right; } // Else, we have a match else { break; } } return(current); }
private void SearchNode(BinaryTreeNode <T> searchIn, BinaryTreeNode <T> searchFor) { if (searchIn == null) { return; } //Node found if (searchIn.CompareTo(searchFor) == 0) { _searchResult = searchIn; return; } if (searchIn.CompareTo(searchFor) > 1) //without this it iterates through the whole tree { SearchNode(searchIn.LeftNode, searchFor); } SearchNode(searchIn.RightNode, searchFor); }
private void SearchNode(BinaryTreeNode <T> searchIn, BinaryTreeNode <T> searchFor) { if (searchIn == null) { return; } //Node found if (searchIn.CompareTo(searchFor) == 0) { _searchResult = searchIn; return; } SearchNode(searchIn.LeftNode, searchFor); SearchNode(searchIn.RightNode, searchFor); }
private BinaryTreeNode <T> FindWithParent(T value, out BinaryTreeNode <T> parent) { //start at head BinaryTreeNode <T> current = _head; //parent starts null parent = null; //while current node is not null while (current != null) { //compare current with value to find. int result = current.CompareTo(value); //if current is bigger than value to find if (result > 0) { //parent is now current parent = current; //new current will be current's left current = current.Left; } else if (result < 0) { //// if current is less than value to find //parent becomes current parent = current; //new current be current's right current = current.Right; } else { //if results was 0, then it was a match break; } } //return whatever current is at, either a value because of break or null //parent will be the parent of the found value or last value to be traversed or null if head node return(current); }
public BinaryTreeNode <T> Find(T data) { BinaryTreeNode <T> current = Head; while (current != null) { int result = current.CompareTo(data); if (result > 0) { current = current.Left; } else if (result < 0) { current = current.Right; } else { return(current); } } return(current); }