简体   繁体   English

广度优先树

[英]Breadth-first tree

I seem to be having issues structuring a breadth-first tree. 我似乎在构造广度优先树时遇到问题。

In the code below, I have a node that is inserted through a loop in another class. 在下面的代码中,我有一个节点通过另一个类中的循环插入。

The structure of the tree is supposed to be as so: 树的结构应该是这样的:

    A
   / \
  B   C
 /\   /\
D E  F  G

Now for the code: 现在查看代码:

My code structures the left side correctly, whereas the right side adds the left side as well. 我的代码正确构造了左侧,而右侧也添加了左侧。 I understand where in the code this happens, but is there a way to prevent this from happening? 我知道代码在哪里发生,但是有办法防止这种情况发生吗?

public Node familyTree;

public void breadthFirst(Node newNode){
    familyTree = breadthFirst(familyTree,newNode);

}

public Node breadthFirst(Node T, Node newNode){
    if(T == null){
        T = newNode;
        return T;            
    }
    if(T.left == null){
        newNode.height = T.height + 1;            
        T.left = newNode;
        return T;
    }
    else if(T.right == null){
        newNode.height = T.height + 1;    
        T.right = newNode;
        return T;
    }
    else{            
         T.left = breadthFirst(T.left, newNode);
         T.right = breadthFirst(T.right, newNode); <-- this is the corporate           
    }
    return T;

}

if you are using recursive, definitely the implementation is a "depth-first-search", for breadth-first-search, you use a queue or a FIFO data structure 如果使用递归,则肯定是“深度优先”的实现,对于广度优先的搜索,则使用队列或FIFO数据结构

pseudo-code 伪码

public Node breadthFirst(Node T, Node searchNode){
  Queue queue = new Queue();
  queue.queue(T);

  while (!queue.isEmpty()) {
    Node curNode = queue.dequeue();
    if (curNode == null) continue;

    if (curNode.value().equals(searchNode.value()) {
      return curNode;
    }

    queue.queue(curNode.left);
    queue.queue(curNode.right);
  } 

  return null; //or throw exception not found
}

I think a breadth-first tree resemble a complete binary tree , so you can employ Array to store it rather than link list. 我认为breadth-first tree类似于complete binary tree ,因此您可以使用Array来存储它而不是链接列表。 And about complete binary tree if the parent number is n then the left number=2*n+1 right=2*n+2. 关于complete binary tree如果父代号是nleft number=2*n+1 right=2*n+2.


for example: use array nodes[the amount of node] and the 0th Node is A (number begin zero) when the number n of Node is even like C( n=2 ) then nodes[(n-2)/2].right = nth node else odd like B then nodes[(n-1)/2].left = nth node 例如:使用阵列nodes[the amount of node]0th Node是A (number begin zero)时,数n节点的是even像C( n=2 ),那么节点[(N-2)/ 2]。 right = nth node否则像B一样odd然后是nodes [(n-1)/ 2]。left= nth node

What you are missing is using the height of the left and right node to determine which side the new node should be a child of when you reach the else statement. 您缺少的是使用左右节点的高度确定到达else语句时新节点应在哪一侧成为子节点。 Currently, you're adding it to both sides regardless of where the node should be placed. 当前,无论要将节点放置在何处,都将其添加到两侧。

As an aside, it looks like you might be keeping track of depth of the tree in height attribute, rather than the height. 顺便说一句,您似乎可能在height属性中而不是在height中跟踪树的深度。 This stackoverflow post does a good job of explaining the difference. 这个stackoverflow帖子很好地解释了差异。

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

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