Question

You are given a string containing a sequence of open and close brackets. and isOpen, isClose,...

You are given a string containing a sequence of open and close brackets. and isOpen, isClose, isMatching methods as defined below. Write a recursive algorithm in pseudocode for the method isBalanced that takes the  string and a stack of characters as the input and checks if the given input string contains balanced brackets. For example "(][)" would be considered not balanced where "[()]" would be considered balanced.

The recursive isBalanced method utilizes  isOpen, isClose, isMatching methods as defined below.

static String open = "([<{";
static String close = ")]>}";

static boolean isOpen(char ch)
{
    return open.indexOf(ch) != -1;
}

static boolean isClose(char ch)
{
    return close.indexOf(ch) != -1;
}

static boolean isMatching(char chOpen, char chClose)
{
    return open.indexOf(chOpen) == close.indexOf(chClose);
}

static boolean isBalanced(String input, String<Character> stack) 
{

// write pseudocode for the recursive algorithm here

}

Homework Answers

Answer #1
static boolean isBalanced(String input, String<Character> stack) 
{
    if input[0] == '\0' //end of String
    {
        if (stack.isEmpty())
            return true;
        else
            return false;
        
    }
    if (isOpen(input[0]))
    {
        stack.push(input[0])
        return isBalanced(input[1:length(input)],stack)
    }
    else if(isClose(input[0]))
    {
        if (isMatching(stack.pop(),input[0]))
            return isBalanced(input[1:length(input)],stack)
        else
            return false;
    }
    
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex:...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the input infix expression one character at a time and push every character read ( other than ')' ) onto stk1. Do not push the character read if it is ')'. (3) As long as stk1 is not empty, do the following:            Fetch the top character ( call it c ) of stk1 and pop stk1.            if c is an alphabetic character (a-z),...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
Write a recursive method to return all possible k permutations of the given String non-zeros number...
Write a recursive method to return all possible k permutations of the given String non-zeros number Sample input : "123" , 2 output : "1-2", "1-3", "2-3", "2-1", "3-1", "3-2" ** Please provide -PSEUDO CODE -UML DIAGRAM
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These...
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These same mechanisms can be used as a part of writing a simple compiler. Write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...
Write a recursive method body for the method whose contract is given below. Note that n...
Write a recursive method body for the method whose contract is given below. Note that n is a restores-mode parameter. As an example, if n = 314, the method should return 1. You can only use the NaturalNumber kernel methods multiplyBy10, divideBy10, and isZero. /** * Reports the minimum (i.e., smallest) digit of n when it is * written in ordinary decimal notation. * ... * @ensures * minDigit = [the minimum digit of n] */ private static int minDigit(NaturalNumber...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....