public BSTFind <T> FindNodeByKey(int key) { // Ищем в дереве узел и сопутствующую информацию по ключу BSTFind <T> result = new BSTFind <T>(); UpdateResult(key, result, Root); if (Root == null || result.NodeHasKey) { return(result); } var nextNode = result.Node.NodeKey > key ? result.Node.LeftChild : result.Node.RightChild; while (nextNode != null) { result = UpdateResult(key, result, nextNode); if (result.Node.NodeKey == key) { return(result); } nextNode = result.Node.NodeKey > key ? result.Node.LeftChild : result.Node.RightChild; } return(result); }
private static BSTFind <T> UpdateResult(int key, BSTFind <T> result, BSTNode <T> nextNode) { result.Node = nextNode; result.ToLeft = result.Node != null && result.Node.NodeKey > key; result.NodeHasKey = result.Node != null && result.Node.NodeKey == key; return(result); }