Java 我需要帮助绘制这个递归方法的执行跟踪树
我对JAVA还很陌生。我想为回文测试方法的两个版本的两个输入字符串“repaper”和“refisher”绘制执行跟踪树Java 我需要帮助绘制这个递归方法的执行跟踪树,java,recursion,Java,Recursion,我对JAVA还很陌生。我想为回文测试方法的两个版本的两个输入字符串“repaper”和“refisher”绘制执行跟踪树 public static void main( String[] args ) { String testCase1 = "repaper"; String testCase2 = "refinisher"; if ( isPalindrome_Version_1 ( testCase1 ) )
public static void main( String[] args ) {
String testCase1 = "repaper";
String testCase2 = "refinisher";
if ( isPalindrome_Version_1 ( testCase1 ) )
System.out.println ( "The string \"" + testCase1 + "\" is a palindrome" );
else
System.out.println ( "The string \"" + testCase1 + "\" is NOT a palindrome" );
}
// return "true" iff the String s is a palindrome
private static boolean isPalindrome_Version_1 ( String s )
{
boolean result = false;
// BASE CASE
if ( s.length() <= 1 )
return (true);
// BASE CASE - check if first char equals last char
if ( s.charAt(0) != s.charAt ( s.length() - 1 ) )
return ( false );
// GENERAL CASE - check whether the substring formed by removing
// the first and last letters is a palindrome
result = isPalindrome_Version_1 ( s.substring ( 1, s.length() - 1 ) );
return( result );
}
// return "true" iff the String s is a palindrome
private static boolean isPalindrome_Version_2 ( String s )
{
boolean result = false;
// BASE CASE
if ( s.length() <= 1 )
return (true);
// GENERAL CASE - check whether the substring formed by removing
// the first and last letters is a palindrome
result = isPalindrome_Version_2 ( s.substring ( 1, s.length() - 1 ) );
// BASE CASE - check if first char equals last char
if ( s.charAt(0) != s.charAt ( s.length() - 1 ) )
return ( false );
return( result );
}
}
我不认为版本2是正确的或至少是有效的,因为在递归整个字符串后返回false 也就是说,这里是版本1的跟踪
isPalindrome("repaper") // length greater than 1 and ending characters are equal
isPalindrome("epape") // length greater than 1 and ending characters are equal
isPalindrome("pap") // length greater than 1 and ending characters are equal
isPalindrome("a") // length is 1, return true
测试用例2
isPalindrome("refinisher") // length greater than 1 and ending characters are equal
isPalindrome("efinishe") // length greater than 1 and ending characters are equal
isPalindrome("finish") // length greater than 1 but ending characters are not equal, return false
你现在应该可以自己完成第二版了我不明白你想实现什么,你能更明确一点吗。如果要打印递归方法的每个输入字符串,则应传递一个级别参数,以便在递归上绘制每个级别的输入。我总共需要4个跟踪树。树图形中的每个节点指示传递给该方法调用的参数值以及该方法调用返回的值。
public static bool IsPalindrome(string s)
{
if(s == "") return true;
return IsPalindrome(s, 0, s.length() - 1);
}
static bool IsPalindrome(string s, int start, int end)
{
if(s[start] == s[end])
{
if(start >= end) return true;
else return IsPalindrome(s, start + 1, end - 1);
}
return false;
}
isPalindrome("repaper") // length greater than 1 and ending characters are equal
isPalindrome("epape") // length greater than 1 and ending characters are equal
isPalindrome("pap") // length greater than 1 and ending characters are equal
isPalindrome("a") // length is 1, return true
isPalindrome("refinisher") // length greater than 1 and ending characters are equal
isPalindrome("efinishe") // length greater than 1 and ending characters are equal
isPalindrome("finish") // length greater than 1 but ending characters are not equal, return false