Resolved Why my recursive function calling itself again after returning the value ?

ram_rocks

Active member
Joined
Jun 14, 2021
Messages
27
Programming Experience
1-3
The problem is after the node.parentNode is null it's returning but it's going back to the GetPath function and executing and returning it. so what should I do? Due to this my program is running for a very long time
C#:
public static List<string> GetPath(XmlNode node, List<string> parentList)
        {
            if (node.ParentNode == null)
            {
                return parentList;
            }
            else
            {
                if (node.FirstChild.Name == "SHORT-NAME")
                {
                    parentList.Add("/" + node.FirstChild.InnerText);
                }

                parentList = GetPath(node.ParentNode, parentList); //RECURSIVE
                return parentList;
            }
        }
 
Last edited by a moderator:
The way to avoid doing all the rework performed by the original code in post #1 is to make use of a data structure to keep track of nodes that you've already seen. So the best approach in my view is to wrap the code in a class where the class keeps track of the nodes previously seen.

So basically something like the following pseudo code:
C#:
class PathFinder
{
    Dictionary<XmlNode, string> _paths = new Dictionary<XmlNode, string>();
  
    Stack<XmlNode> WalkToRoot(XmlNode node)
    {
        var walk = new Stack<XmlNode>();
        while (node != null)
        {
            walk.Push(node);
            node = node.ParentNode;
        }
        return walk;
    }
  
    string GetNodePath(XmlNode node)
    {
        if (!_paths.ContainsKey(node))
            _paths.Add(node, $"/{node.FirstChild.InnerText}"); 
        return _paths[node];
    }
  
    public IEnumerable<string> GetPaths(XmlNode startNode)
    {
        var walkToRoot = WalkToRoot(startNode);     
        while (walkToRoot.TryPop(out XmlNode node))
        {
            if (node.FirstChild.Name == "SHORT-NAME")
                yield return GetNodePath(node);
        }
    }
}
 
Last edited:
Wise choice to use an appropriate collection Skydiver. Good advice falls on deaf ears it seems!

Long live Json. ? XML is horrible to work with. It's overly convoluted to work with. I only ever use it when working with html code. Other than that, if I had an option to choose Json, I would every time.
 
The way to avoid doing all the rework performed by the original code in post #1 is to make use of a data structure to keep track of nodes that you've already seen. So the best approach in my view is to wrap the code in a class where the class keeps track of the nodes previously seen.

So basically something like the following pseudo code:
C#:
class PathFinder
{
    Dictionary<XmlNode, string> _paths = new Dictionary<XmlNode, string>();
 
    Stack<XmlNode> WalkToRoot(XmlNode node)
    {
        var walk = new Stack<XmlNode>();
        while (node != null)
        {
            walk.Push(node);
            node = node.ParentNode;
        }
        return walk;
    }
 
    string GetNodePath(XmlNode node)
    {
        if (!_paths.ContainsKey(node))
            _paths.Add(node, $"/{node.FirstChild.InnerText}");
        return _paths[node];
    }
 
    public IEnumerable<string> GetPaths(XmlNode startNode)
    {
        var walkToRoot = WalkToRoot(startNode);  
        while (walkToRoot.TryPop(out XmlNode node))
        {
            if (node.FirstChild.Name == "SHORT-NAME")
                yield return GetNodePath(node);
        }
    }
}

Hi Skydriver, Yeah I tried the above pseudo code with slight modification and it returns out path fast compare to my code in #1 post
 
Back
Top Bottom