数组树找到父母和他们的父母以及到根的最短路径

发布于 2024-12-04 19:56:45 字数 934 浏览 0 评论 0原文

我想找到从孩子到他的父母,到祖父母,最后到根的最短路线。

例如,输入 0 0 0 1 2 表示:

input[1] parent is 0 (route = 1)
input[2] also has parent 0 (route = 1) 
input[3] has parent 1 which has parent 0 (route = 2)
input[4] has parent 2 which has parent 0 (route = 2)

到目前为止的代码:

创建名为 targetNodes 的数组,其中包含 0 0 0 1 2

System.out.print( "0 " );

for ( int x = 1; x < arrayLength; x++ )
{

  int depth = 1;
  int parent = 0;

  while ( targetNodes[x] != 0 ) 
  {
      depth++;
      targetNodes[x] = targetNodes[ targetNodes[x] ] ;
  }   

  // output shortest path from node to root for every node
  System.out.print( depth + " " );

}

System.out.print("\n");

我的示例有效,输入:0 0 0 1 2,它打印:0 1 1 2 2, 但对于输入:0 0 1 2 1 4,它会打印出:0 1 2 2 2 2,而正确的输出是:0 1 2 3 2 3

不确定我做错了什么,我猜这是逻辑

I want to find the shortest route from a child, to his parent, to grandparent and in the end the root.

For example, input 0 0 0 1 2, means that:

input[1] parent is 0 (route = 1)
input[2] also has parent 0 (route = 1) 
input[3] has parent 1 which has parent 0 (route = 2)
input[4] has parent 2 which has parent 0 (route = 2)

Code so far:

Array is created called targetNodes which contains 0 0 0 1 2,

System.out.print( "0 " );

for ( int x = 1; x < arrayLength; x++ )
{

  int depth = 1;
  int parent = 0;

  while ( targetNodes[x] != 0 ) 
  {
      depth++;
      targetNodes[x] = targetNodes[ targetNodes[x] ] ;
  }   

  // output shortest path from node to root for every node
  System.out.print( depth + " " );

}

System.out.print("\n");

My example works and with input: 0 0 0 1 2 it prints: 0 1 1 2 2,
but for input: 0 0 1 2 1 4 it prints out: 0 1 2 2 2 2 when the correct output would be: 0 1 2 3 2 3

Not sure what I am doing wrong, I would guess it's the logic

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹梦到西洲 2024-12-11 19:56:45

其实很简单。最困难的部分是转换单元测试的数据,以便可以有效地输入它们。

package so7455242;

import static org.junit.Assert.*;

import org.junit.Test;

import com.google.common.primitives.Ints;

public class DistanceFinder {

  private static int[] findPathLengths(int[] parent) {
    int[] distances = new int[parent.length];
    for (int i = 0; i < parent.length; i++) {
      int distance = 0;
      for (int node = i; node != 0; node = parent[node]) {
        distance++;
      }
      distances[i] = distance;
    }
    return distances;
  }

  private static int[] toIntArray(String s) {
    String[] words = s.split(" ");
    int[] ints = new int[words.length];
    for (int i = 0; i < ints.length; i++) {
      ints[i] = Integer.parseInt(words[i]);
    }
    return ints;
  }

  private static void testcase(String expected, String input) {
    int[] nodeDefinitions = toIntArray(input);
    int[] pathLengths = findPathLengths(nodeDefinitions);
    String actual = Ints.join(" ", pathLengths);
    assertEquals(expected, actual);
  }

  @Test
  public void test() {
    testcase("0 1 1 2 2", "0 0 0 1 2");
    testcase("0 1 2 3 2 3", "0 0 1 2 1 4");
  }

}

It's actually pretty simple. The hardest part was to convert the data for the unit tests, so that they can be efficiently typed.

package so7455242;

import static org.junit.Assert.*;

import org.junit.Test;

import com.google.common.primitives.Ints;

public class DistanceFinder {

  private static int[] findPathLengths(int[] parent) {
    int[] distances = new int[parent.length];
    for (int i = 0; i < parent.length; i++) {
      int distance = 0;
      for (int node = i; node != 0; node = parent[node]) {
        distance++;
      }
      distances[i] = distance;
    }
    return distances;
  }

  private static int[] toIntArray(String s) {
    String[] words = s.split(" ");
    int[] ints = new int[words.length];
    for (int i = 0; i < ints.length; i++) {
      ints[i] = Integer.parseInt(words[i]);
    }
    return ints;
  }

  private static void testcase(String expected, String input) {
    int[] nodeDefinitions = toIntArray(input);
    int[] pathLengths = findPathLengths(nodeDefinitions);
    String actual = Ints.join(" ", pathLengths);
    assertEquals(expected, actual);
  }

  @Test
  public void test() {
    testcase("0 1 1 2 2", "0 0 0 1 2");
    testcase("0 1 2 3 2 3", "0 0 1 2 1 4");
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文