0

I am attempting to traverse an api recursively to produce an end result of the type:

     1
    / \  \  \
   /   \  \  \
  /     \  \  \
 2       3  0  7
 \     /        \
   4   5   6     10
  /       / \
 7       8   9

The issue I am finding is that I understand that I can use something similar to http://rosettacode.org/wiki/Tree_traversal#Java But fail to understand how to recursively make a NEW call on the api?

The API accepts the following input: http://example.com/1/children

To return:

{
    "name": "1",
    "children": [{
        "name": "2"
    }, {
        "name": "3"
    }]

}

How would I then continue to make a call on 2 to find it's children and then do the same for 3 once all the children for 2 are found?

Sample code:

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://example.com/1/children");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}
Anish B
  • 39
  • 1
  • 8
  • Could you post the code you are using the make the call with `1`? The trick then is to loop over the results and call the same method again but with each result instead of `1`. – Tunaki Feb 09 '16 at 22:47
  • Certainly added some response. I would receive inputLine but I do not know where to go from there. – Anish B Feb 09 '16 at 22:50
  • You are trying to make a web crawler? Look up depth-first or breadth-first search. However, that may be difficult unless you have a precompiled list of addresses you are trying to access, in which case each URL may have more than just 2 direct links, no? – OneCricketeer Feb 09 '16 at 22:54
  • I am trying to produce a hierarchical structure which will be used at a later point on my code. The initial call could product 0 or more results so I understand that. However, how can I recursively make the NEXT call? – Anish B Feb 09 '16 at 22:57
  • You make a new URLConnection object and get its inputstream... But you need to get the next url itself, which seems to be the problem you are having. I would think about it like a filesystem structure rather than a tree as you have shown. What does the content of these webpages look like? How would you know when to stop traversing a "subtree"? – OneCricketeer Feb 09 '16 at 22:59
  • Once you have found a branch which does not have any children, you would consider it a leaf and therefore would stop there. I will add an example output on my original question to provide some aid – Anish B Feb 09 '16 at 23:01
  • I'm linking you to a canonical answer about recursion that goes really in-depth about that question. It explains well how to think and tackle such problems. It doesn't address your question particularly: it is more general and applies to all recursive problems, such as this one. – Tunaki Feb 09 '16 at 23:10
  • @Tunaki There can be more than 2 children per node and therefore I don't believe the answer you have provided would help. If however you think that the same rule can be applied for that scenario then I will continue researching into Binary Trees. – Anish B Feb 09 '16 at 23:21
  • What you want is to loop over the result and call the recursive method for each element in the loop. The concept is the same. – Tunaki Feb 09 '16 at 23:28
  • 1
    Like I said *Look up depth-first or breadth-first search* - Basically covers the general algorithm for getting a list of all children, then recursing each one – OneCricketeer Feb 09 '16 at 23:32

0 Answers0