Hi, what I'm trying to accomplish is to create a search function that traverses a ternary tree in a user defined pathway obtained through a string input, and returns the current tree to display the values of that tree root outside of the function.
Here is the data structure of this ternary tree in Pascal:
This procedure starts the search by taking a user input in the format 'uusdusd' u standing for up pathway and so on.
type MelodyTree = ^TreeNode;
TreeNode = record
Then it goes to this function that uses recursion to traverse the tree in the pathway obtained a character at a time from the string Path:
procedure tuneSearch(p:string; t:MelodyTree);
p:= trim(p); //removes extra whitespacing
if (length(p)>0) then // non-empty line
tuneSearch2(t, p, 0);
writeln('A match was found: phrase', t^.phrase, ' in the song ', t^.tune);
writeln('This pitch change is empty');
Now for some reason when I type in a search parameter such as 'ssddssdd' which contains the song phrase: "I'm in love with her and I feel fine" in the title: [I feel fine] the program just crashes when I run the nested If statements in tuneSearch2 that initialize the recursion.
function tuneSearch2(t:MelodyTree; path:string; pos:integer):MelodyTree;
while length(path)<>0 do
if(upcase(path[pos]) ='D') then
tuneSearch2(t^.downBranch, path, pos+1)
else if(upcase(path[pos]) = 'S') then
tuneSearch2(t^.sameBranch, path, pos+1)
tuneSearch2(t^.upBranch, path, pos+1)
Can anyone see what I'm missing?