简体   繁体   English

在bfs树中查找节点的高度

[英]Find height of a node in bfs tree

I want to find the height of a tree node in bfs but it's not working out, it's giving me the correct height for first 5 nodes but the last two nodes are not correct. 我想在bfs中找到树节点的高度,但它没有用,它给了我前5个节点的正确高度,但最后两个节点不正确。 Here is the code 这是代码

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bfs;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class BreadthFirstSearchExample
{ 

 private Queue<Node> queue;
 static ArrayList<Node> nodes=new ArrayList<Node>();
 static class Node
 {
  int data;
  boolean visited;
  int parent;
  int height;

  Node(int data){
   this.data=data;


  }
 }

 public BreadthFirstSearchExample()
 {
  queue = new LinkedList<Node>();
 }

 // find neighbors of node using adjacency matrix
 // if adjacency_matrix[i][j]==1, then nodes at index i and index j are connected
 public ArrayList<Node> findNeighbours(int adjacency_matrix[][],Node x)
 {
  int nodeIndex=-1;

  ArrayList<Node> neighbours=new ArrayList<Node>();
  for (int i = 0; i < nodes.size(); i++) {
   if(nodes.get(i).equals(x))
   {
    nodeIndex=i;
    break;
   }
  }

  if(nodeIndex!=-1)
  {
   for (int j = 0; j < adjacency_matrix[nodeIndex].length; j++) {
    if(adjacency_matrix[nodeIndex][j]==1)
    {
     neighbours.add(nodes.get(j));
    }
   }
  }
  return neighbours;
 }

 public  void bfs(int adjacency_matrix[][], Node node)
 {

  queue.add(node);
  node.visited=true;


  int nf =0;

  while (!queue.isEmpty())
  {
   nf = queue.size();
   Node element=queue.remove();

   System.out.print(element.data + "\t");


    System.out.print("Height" + element.height + "\t");

   ArrayList<Node> neighbours=findNeighbours(adjacency_matrix,element);
   for (int i = 0; i < neighbours.size(); i++) {
    Node n=neighbours.get(i);
    int mf = neighbours.size();

    if(n!=null && !n.visited)
    {

     n.parent=element.data;
     queue.add(n);
     n.visited=true;   
    }
    n.height++;
   element.height = n.height;
   }

  }
 }

 public static void main(String arg[])
 {

   Node node40 =new Node(40);
   Node node10 =new Node(10);
   Node node20 =new Node(20);
   Node node30 =new Node(30);
   Node node60 =new Node(60);
   Node node50 =new Node(50);
   Node node70 =new Node(70);

   nodes.add(node40);
   nodes.add(node10);
   nodes.add(node20);
   nodes.add(node30);
   nodes.add(node60);
   nodes.add(node50);
   nodes.add(node70);
   int adjacency_matrix[][] = {
     {0,1,1,0,0,0,0},  // Node 1: 40
     {1,0,0,1,0,0,0},  // Node 2 :10
     {1,0,0,1,1,1,0},  // Node 3: 20
     {0,1,1,0,1,0,0},  // Node 4: 30
     {0,0,1,1,0,0,1},  // Node 5: 60
     {0,0,1,0,0,0,1},  // Node 6: 50
     {0,0,0,0,1,1,0},  // Node 7: 70
   };
   System.out.println("The BFS traversal of the graph is ");
   BreadthFirstSearchExample bfsExample = new BreadthFirstSearchExample();
   bfsExample.bfs(adjacency_matrix, node40);

 }
}

The output of the code should be 代码的输出应该是

40 Height0 10 Height1 20 Height1 30 Height2 60 Height2 50 Height2 70 Height3 40高度0 10高度1 20高度1 30高度2 60高度2 50高度2 70高度3

but instead it is giving me the output 但它却给了我输出

40 Height0 10 Height1 20 Height1 30 Height2 60 Height2 50 Height1 70 Height2 40高度0 10高度1 20高度1 30高度2 60高度2 50高度1 70高度2

How can I solve this? 我怎么解决这个问题?

The for -loop of your bfs method has to look like this: 你的bfs方法的for -loop必须如下所示:

for (int i = 0; i < neighbours.size(); i++) {
  Node n = neighbours.get(i);
  int mf = neighbours.size();

  if (n != null && !n.visited) {
    n.parent = element.data;
    queue.add(n);
    n.visited = true;
    n.height = element.height + 1;    // <- this is right
  }

  // This was wrong
  //n.height++;                    
  //element.height = n.height;
}

Explanation: 说明:

The changed line sets the height of the visited node ("n") to the height of the parent node ("element") + 1, but only on the first visit (so inside the if statement). 更改的行将受访节点的高度(“n”)设置为父节点的高度(“元素”)+ 1,但仅在第一次访问时(因此在if语句内)。 This is what you want, ie the height. 这就是你想要的,即高度。

What you did before was increment the height of a node each time you saw it as a neighbor, independently of whether it was visited before or not. 您之前所做的是每次将节点视为邻居时增加节点的高度,而不管之前是否访问过节点。 And then you change the height of the parent node to that, but never see it because it isn't printed again. 然后将父节点的高度更改为该节点,但永远不会看到它,因为它不会再次打印。

Just change this part from 只需更改此部分即可

if(n!=null && !n.visited)
{
    n.parent=element.data;
    queue.add(n);
    n.visited=true;
}
n.height++;
element.height = n.height;

to

if(n!=null && !n.visited)
{
    n.parent=element.data;
    queue.add(n);
    n.visited=true;
    n.height=element.height+1;
}
//n.height++;
//element.height = n.height;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM