Java 结构并基于输入模式计算树高

Java 结构并基于输入模式计算树高,java,data-structures,tree,Java,Data Structures,Tree,假设我有这个输入 五, 4-1 4 1 说明: 第一行包含节点数您可以从computeHeight()中删除setUpTree()函数,因为您没有在该函数中使用treeHashMap 让我们试着计算时间复杂度 考虑以下树: 我们计算每个节点的深度,为了计算一个节点的深度,我们迭代直到到达顶部。所以 0 we iterate over other nodes {} 1 we iterate over other nodes {0} 2 we iterate over other nodes {1

假设我有这个输入

五,

4-1 4 1

说明:
第一行包含节点数您可以从
computeHeight()
中删除
setUpTree()
函数,因为您没有在该函数中使用
tree
HashMap

让我们试着计算时间复杂度

考虑以下树:

我们计算每个节点的深度,为了计算一个节点的深度,我们迭代直到到达顶部。所以

0 we iterate over other nodes {}
1 we iterate over other nodes {0}
2 we iterate over other nodes {1,0}
3 we iterate over other nodes {2,1,0}
该算法的总体时间复杂度为O(n^2)

我们在做一些重复性的工作吗?

是的,在第三次迭代中,我们知道
2
的深度,当我们为
3
进行迭代时,我们可以使用
2
的深度。一般来说,我们可以说

    depth[node] = 1 + depth[node->parent]
让我们用它来更新我们的算法

算法

      a. Create an auxilary array depth where we store depth of the nodes
      b. Iterate over each node:
          i.  If the depth of parent of node is known then 
              update depth[node]=1+depth[node -> parent] 
          ii. If the depth[node->parent] is not known, then 
              recursively calculate the depth and also update 
              the depth of each node in the path
      c. Height of the tree is the maximum depth among all the nodes
 int  height(HashMap map, int nodeId)
   if(map.get(nodeId).size() == 0)
      // Its the leaf
      return 0;
   int maxHeightsOfSubTress = 0;
   for i = 0 to map.get(nodeId).size()-1
      maxHeightsOfSubTress = max(maxHeightsOfSubTress, height(map,map.get(nodeId).get(i))
   return 1 + maxHeightsOfSubTress

由于每个节点只接触一次,该算法的复杂度为O(n)

编辑

为什么。为什么它是一棵树?既然你没有树,你怎么能给它命名呢 节点类和根与任何其他父类类似。如果它是一个 树

有多种方法可以实现树数据结构,类似地,
node
是树的概念,我们可以用多种方法实现它。一种方法是使用
节点

public class Node {
   int value;
   List<Node> childrens;
}
让我们看看如何使用您给定的树的hashmap表示来编写它

算法

      a. Create an auxilary array depth where we store depth of the nodes
      b. Iterate over each node:
          i.  If the depth of parent of node is known then 
              update depth[node]=1+depth[node -> parent] 
          ii. If the depth[node->parent] is not known, then 
              recursively calculate the depth and also update 
              the depth of each node in the path
      c. Height of the tree is the maximum depth among all the nodes
 int  height(HashMap map, int nodeId)
   if(map.get(nodeId).size() == 0)
      // Its the leaf
      return 0;
   int maxHeightsOfSubTress = 0;
   for i = 0 to map.get(nodeId).size()-1
      maxHeightsOfSubTress = max(maxHeightsOfSubTress, height(map,map.get(nodeId).get(i))
   return 1 + maxHeightsOfSubTress


您可以删除
setUpTree()来自
computeTree
,因为您没有使用它,这增加了时间复杂度。嘿@Doomboi,您的方法是正确的,您的算法应该可以正常工作。我在答案中对你的算法做了一点修改。我想我弄错了。我编写的SetUpTree()方法,从assighemnt提供的建议中我可以理解。他们为我提供了计算树的高度的computeTree方法。但我必须用更好更快的实施来取代它。我将编辑我的问题。非常感谢您的详细阐述。从概念上讲。我理解并能应用你的算法。然而!在我的作业中。我没有节点。它只是一个存储树的映射的数组。我本可以创建一个节点类并根据输入设置树。但由于它是使用数组实现的。继续使用数组可能更好。我得到了上面的一个算法。我试着解释它。现在我只剩下一个hashmap。它存储父索引,也就是父数组中的内容。对于数组列表中的相应子项,在hashmap中下一步要做什么,我将所有父项作为键,树中的所有子项都是表示为ArrayList的值。树可能不是二进制的,因此我的节点可能有两个子节点以上。如果这个实现是一棵树!为什么?为什么它是一棵树,你怎么能命名一棵树,因为你没有一个节点类,根就像任何其他的父。如果它是一棵树。我怎么计算它的高度呢?嘿@Doomboi,我已经在编辑部分回答了这些疑问。如果有帮助,请告诉我好吗?另外,如果您有进一步的疑问,那么让我们通过stackoverflow聊天进行交流。谢谢您的回复。现在我明白了树的结构。我有个问题。如果节点数为奇数,树的高度不是其父节点数+1吗。我在考虑as级别。因为我的hashmap包含了所有的父母和孩子。我所要做的就是返回这个:返回n%2==0?tree.size():tree.size()+1;我在提供的测试文件上测试了它。n=5&&n=10是正确的。但当n=100时,结果是50。正确答案是8。对不起,我不知道怎么在这里聊天!让我们在房间里继续