Java 查找树的最大宽度的算法,不使用节点结构
我想知道如何计算一棵树的最大宽度。我没有使用典型的叶/节点结构,而是基于数据库中的数据。我将查找特定“节点”(Person)的所有子节点,以确定对等线的最大宽度:Java 查找树的最大宽度的算法,不使用节点结构,java,algorithm,recursion,groovy,binary-tree,Java,Algorithm,Recursion,Groovy,Binary Tree,我想知道如何计算一棵树的最大宽度。我没有使用典型的叶/节点结构,而是基于数据库中的数据。我将查找特定“节点”(Person)的所有子节点,以确定对等线的最大宽度: 1 / \ 2 3 / | \ \ 4 5 6 7 / \ 8 9 上面那棵树的最大值是4。既然我没有使用传统的左/右方法,而且孩子的数量可以超过2个,我该怎么做 两件事: 这不是家庭作业 下面的代码生成的最大宽度约为3200(对
1
/ \
2 3
/ | \ \
4 5 6 7
/ \
8 9
上面那棵树的最大值是4。既然我没有使用传统的左/右方法,而且孩子的数量可以超过2个,我该怎么做
两件事:
- 这不是家庭作业
- 下面的代码生成的最大宽度约为3200(对于我手头的示例,我计算的最大宽度为22)
private int calculateWidth(def org, int h) {
def allContacts = Contact.findAllByOrganization(org)
List<String> headNodes = findHighestNode(org.id, allContacts )
Contact contact = Contact.get(Long.parseLong(headNodes.get(0)))
Person parent = new Person(contact.id, contact.fullName)
int maxWidth = 0;
int width;
int heightOfChart = h;
int i;
for(i = 1; i <= heightOfChart; i++)
{
width = getWidth(parent, i);
if(width > maxWidth)
maxWidth = width;
}
System.out.println("The max width is = " + maxWidth)
return ((NODE_HEIGHT + NODE_OFFSET) * (maxWidth))
}
private int getWidth(Person parent, int level)
{
List<Person> allChildren = getChildren(parent)
if(allChildren.size() == 0)
return 0;
if(level == 1)
return 1;
else if (level > 1) {
int count = 0
for(Person child : allChildren) {
count = count + getWidth(parent, level-1)
}
return count
}
}
private int calculateWidth(定义组织,int h){
def allContacts=Contact.findAllByOrganization(org)
List headNodes=findHighestNode(org.id,allContacts)
Contact Contact=Contact.get(Long.parseLong(headNodes.get(0)))
人员父项=新人员(contact.id,contact.fullName)
int maxWidth=0;
整数宽度;
int heightOfChart=h;
int i;
对于(i=1;i最大宽度)
最大宽度=宽度;
}
System.out.println(“最大宽度为=“+maxWidth”)
返回((节点高度+节点偏移)*(最大宽度))
}
私有int getWidth(个人父级,int级别)
{
List allChildren=getChildren(父级)
if(allChildren.size()==0)
返回0;
如果(级别==1)
返回1;
否则,如果(级别>1){
整数计数=0
个人(儿童:所有儿童){
计数=计数+getWidth(父级,级别1)
}
返回计数
}
}
我没有真正检查您的代码,但我会使用广度优先的搜索方法
一些psuedo代码:
start with list containing just the trees root. call it CurrNodes.
maxWidth = 1;
start with empty list. call it NextNodes.
while(CurrNodes is not empty) {
get all children of nodes in CurrNodes and add them to NextNodes
if number of children is > maxWidth, # of children is the new maxWidth
CurrNodes = NextNodes
NextNodes = empty.
}
解决这个问题的一种方法是使用一个长度为树高的计数器数组,然后对于您搜索的每个级别,您可以在数组中添加节点的计数器,最后您只需要在数组中获得最大值的索引。修改代码时,可能是这样的:
private int calculateWidth(def org, int h) {
def allContacts = Contact.findAllByOrganization(org);
List<String> headNodes = findHighestNode(org.id, allContacts );
Contact contact = Contact.get(Long.parseLong(headNodes.get(0)));
Person parent = new Person(contact.id, contact.fullName)
int maxWidth = 0;
int heightOfChart = h;
int i;
//create the counter array, initialized with 0 values by default
int[] levelWidth = new int[h];
if (parent != null) {
levelWidth[0] = 1;
//I suppose that your "parent" var is the root of your tree.
fillWidth(parent, 1, levelWidth);
}
for(int width : levelWidth) {
maxWidth = Math.max(maxWidth, width);
}
return maxWidth;
}
private void fillWidth(Person parent, int level, int[] levelWidth) {
List<Person> allChildren = getChildren(parent);
if (allChildren != null && !allChildren.isEmptty())
levelWidth[level] += allChildren.size();
for(Person child : allChildren) {
fillWidth(parent, level + 1, levelWidth)
}
}
}
private int calculateWidth(定义组织,int h){
def allContacts=Contact.findAllByOrganization(org);
List headNodes=findHighestNode(org.id,allContacts);
Contact-Contact=Contact.get(Long.parseLong(headNodes.get(0));
人员父项=新人员(contact.id,contact.fullName)
int maxWidth=0;
int heightOfChart=h;
int i;
//创建计数器数组,默认情况下使用0值初始化
int[]levelWidth=新int[h];
如果(父项!=null){
levelWidth[0]=1;
//我假设您的“父”var是树的根。
fillWidth(父级,1,levelWidth);
}
用于(整型宽度:levelWidth){
maxWidth=Math.max(maxWidth,width);
}
返回最大宽度;
}
私有void fillWidth(个人父级、int级别、int[]级别宽度){
列出所有子项=获取子项(父项);
if(allChildren!=null&&!allChildren.isempty())
levelWidth[level]+=allChildren.size();
个人(儿童:所有儿童){
fillWidth(父级,标高+1,标高宽度)
}
}
}
请注意,每个节点可以有两个以上子节点的二叉树不是二叉树。它只是一棵树。另外,我认为你所说的宽度通常被称为高度或深度。最后,我认为不可能计算任意树的最大值。实际上,等等,你是指树的最大高度/深度,还是指任何节点的最大子节点数?(编辑:或maba的定义,或其他定义?)在您的示例中,宽度是指4 5 6 7
?例如,某个深度上的最大宽度?是-从h高度开始,我会检查每一行的宽度(因此第1行=1,第2行=2,第3行=4,第4行=2),最大宽度为4。我猜我的答案出现得晚。这几乎就是我所做的-将发布我的答案供将来使用(在清理之后)