public bool FindFirst(DataNode pNodeToMatch, bool pAssignFailedEndpoint) { return(this.IterateMatch(pNodeToMatch, this.TreeHead, pAssignFailedEndpoint)); }
public bool IterateMatch(DataNode pNodeToMatch, TreeNode pNodeToCompare, bool pAssignFailedEndpoint) { /* Recursively search a list for either (A) a matching node or, * (B) an endpoint where it will fit. * * 1: With a balanced list of 7 objects: * [10] * / \ * / \ * / \ * [5] [15] * / \ / \ * [3] [8] [12] [18] * * 2: Finding 8 would take three iterations. * Input Return * ..... ...... * 10 -1 * 5 1 * 8 0 (true) * * 3: Finding 13 would take three iterations to fail. * Input Return * ..... ...... * 10 1 * 15 -1 * 12 1 (false) * * Handily, our function will still set CurrentNode to point to the * failed point on the list so we can insert there if the trasverse * is for that purpose. * * Actually, that is an optional parameter. The FindFirst can be overloaded. * * So, if 0 assign and return true. */ try { // Node Balance this.WriteToLog("Beginning iterate search for node with index #" + pNodeToMatch.Index.ToString() + " at index #" + pNodeToCompare.Index.ToString() + ". Current #" + this.CurrentNode.Index.ToString() + "."); int tmpNodeBalance; tmpNodeBalance = this.TreeComparator.Compare(pNodeToMatch, pNodeToCompare); if (tmpNodeBalance == 0) { //Equal match this.CurrentNode = pNodeToCompare; if (this._LogSearches) { this.WriteToLog("Search success. Ended at node #" + this.CurrentNode.Index.ToString() + "."); } return(true); } else if (tmpNodeBalance < 0) { // The Pachinko ball falls left if (pNodeToCompare.LeftIsSet) { //Recurse if (this._LogSearches) { this.WriteToLog("Recursing Left."); } return(this.IterateMatch(pNodeToMatch, pNodeToCompare.LeftBranch, pAssignFailedEndpoint)); } } else if (tmpNodeBalance > 0) { //Falling right if (pNodeToCompare.RightIsSet) { //Recurse if (this._LogSearches) { this.WriteToLog("Recursing Right."); } return(this.IterateMatch(pNodeToMatch, pNodeToCompare.RightBranch, pAssignFailedEndpoint)); } } //If the function gets here we ran out of branches! if (pAssignFailedEndpoint) { //Assign the failed endpoint. Probably for an add this.CurrentNode = pNodeToCompare; } if (this._LogSearches) { this.WriteToLog("Search failed. Ended at node #" + this.CurrentNode.Index.ToString() + "."); } return(false); } catch { this.WriteToLog("Recursive Search had an Error. Ended at node #" + pNodeToCompare.Index.ToString() + "."); return(false); } }
public bool FindFirst(DataNode pNodeToMatch) { //This will do an iterate match from the top for it! Sweet! //Default to not assigning the failed endpoint to current node. return(this.IterateMatch(pNodeToMatch, this.TreeHead, false)); }