Returning value from deep recursion

Discussion in 'Java' started by Arun, Jan 26, 2005.

  1. Arun

    Arun Guest

    I have the following code

    ***********************************
    /**
    * Recurses through schema finding the first instance of the
    element name
    * @param schemaElement - current element being compared from
    schema
    * @param checkElement - element to be checked against i.e. the
    node selected in the gui
    * @return the element in schema which matches the checkElement
    */
    private Element findElementNameMatch(Element schemaElement, Element
    checkElement)
    {
    // Get list of children
    List listOfChildren = schemaElement.getChildren();

    // If element has children
    if(listOfChildren.size() != 0)
    {
    // Recurse through all elements under this one
    for(int x=0; x < listOfChildren.size(); x++)
    {
    // Get child element
    Element childElement = (Element) listOfChildren.get(x);

    if(childElement.getName() == checkElement.getName())
    {
    return(childElement);
    }
    findElementNameMatch(childElement,checkElement);
    }
    }

    return(null);
    }
    ***********************************


    The method is trying to find an element in an xml document that matches
    a given name.

    Can anyone give me tips on how to go about returning a value from deep
    recursion to the first recursion of that particular method, so it can
    return that value to the method that called it.

    And also quite importantly how to stop any recursions after a match has
    been found. EG a target element is found and returned, but how can i
    stop the recursion going on to check elements under this element.

    So far i have only achieved the two by creating a global variable and
    flag, the variable is initialised when the match is found, and flag is
    set to true so no more recursions are done afterwards.
     
    Arun, Jan 26, 2005
    #1
    1. Advertising

  2. Arun wrote:
    >
    > I have the following code
    >
    > ***********************************
    > /**
    > * Recurses through schema finding the first instance of the
    > element name
    > * @param schemaElement - current element being compared from
    > schema
    > * @param checkElement - element to be checked against i.e. the
    > node selected in the gui
    > * @return the element in schema which matches the checkElement
    > */
    > private Element findElementNameMatch(Element schemaElement, Element
    > checkElement)
    > {
    > // Get list of children
    > List listOfChildren = schemaElement.getChildren();
    >
    > // If element has children
    > if(listOfChildren.size() != 0)
    > {
    > // Recurse through all elements under this one
    > for(int x=0; x < listOfChildren.size(); x++)
    > {
    > // Get child element
    > Element childElement = (Element) listOfChildren.get(x);
    >
    > if(childElement.getName() == checkElement.getName())
    > {
    > return(childElement);
    > }
    > findElementNameMatch(childElement,checkElement);
    > }
    > }
    >
    > return(null);
    > }
    > ***********************************
    >
    > The method is trying to find an element in an xml document that matches
    > a given name.
    >
    > Can anyone give me tips on how to go about returning a value from deep
    > recursion to the first recursion of that particular method, so it can
    > return that value to the method that called it.
    >
    > And also quite importantly how to stop any recursions after a match has
    > been found. EG a target element is found and returned, but how can i
    > stop the recursion going on to check elements under this element.
    >
    > So far i have only achieved the two by creating a global variable and
    > flag, the variable is initialised when the match is found, and flag is
    > set to true so no more recursions are done afterwards.


    You need to grab the result of your inner call and check for null. If it isn't null,
    simply return that result. BTW, you're currently ignoring inner results.

    --
    Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
     
    Lee Fesperman, Jan 26, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken
    Replies:
    3
    Views:
    9,852
    John C. Bollinger
    Jun 1, 2004
  2. Chet Nichols III
    Replies:
    3
    Views:
    197
    Eric I.
    Nov 14, 2007
  3. NamSa
    Replies:
    1
    Views:
    479
    Peter J. Holzer
    May 4, 2009
  4. Replies:
    8
    Views:
    747
    John Reye
    Apr 26, 2012
  5. Tim Watts

    TIEHANDLE and deep recursion

    Tim Watts, May 9, 2012, in forum: Perl Misc
    Replies:
    11
    Views:
    866
    Tim Watts
    May 10, 2012
Loading...

Share This Page