Java 如何实现广度优先遍历?
这就是我所拥有的。我认为预订单是一样的,并把它与深度第一Java 如何实现广度优先遍历?,java,breadth-first-search,Java,Breadth First Search,这就是我所拥有的。我认为预订单是一样的,并把它与深度第一 import java.util.LinkedList; import java.util.Queue; public class Exercise25_1 { public static void main(String[] args) { BinaryTree tree = new BinaryTree(new Integer[] {10, 5, 15, 12, 4, 8 }); System.out.pri
import java.util.LinkedList;
import java.util.Queue;
public class Exercise25_1 {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree(new Integer[] {10, 5, 15, 12, 4, 8 });
System.out.print("\nInorder: ");
tree.inorder();
System.out.print("\nPreorder: ");
tree.preorder();
System.out.print("\nPostorder: ");
tree.postorder();
//call the breadth method to test it
System.out.print("\nBreadthFirst:");
tree.breadth();
}
}
class BinaryTree {
private TreeNode root;
/** Create a default binary tree */
public BinaryTree() {
}
/** Create a binary tree from an array of objects */
public BinaryTree(Object[] objects) {
for (int i = 0; i < objects.length; i++) {
insert(objects[i]);
}
}
/** Search element o in this binary tree */
public boolean search(Object o) {
return search(o, root);
}
public boolean search(Object o, TreeNode root) {
if (root == null) {
return false;
}
if (root.element.equals(o)) {
return true;
}
else {
return search(o, root.left) || search(o, root.right);
}
}
/** Return the number of nodes in this binary tree */
public int size() {
return size(root);
}
public int size(TreeNode root) {
if (root == null) {
return 0;
}
else {
return 1 + size(root.left) + size(root.right);
}
}
/** Return the depth of this binary tree. Depth is the
* number of the nodes in the longest path of the tree */
public int depth() {
return depth(root);
}
public int depth(TreeNode root) {
if (root == null) {
return 0;
}
else {
return 1 + Math.max(depth(root.left), depth(root.right));
}
}
/** Insert element o into the binary tree
* Return true if the element is inserted successfully */
public boolean insert(Object o) {
if (root == null) {
root = new TreeNode(o); // Create a new root
}
else {
// Locate the parent node
TreeNode parent = null;
TreeNode current = root;
while (current != null) {
if (((Comparable)o).compareTo(current.element) < 0) {
parent = current;
current = current.left;
}
else if (((Comparable)o).compareTo(current.element) > 0) {
parent = current;
current = current.right;
}
else {
return false; // Duplicate node not inserted
}
}
// Create the new node and attach it to the parent node
if (((Comparable)o).compareTo(parent.element) < 0) {
parent.left = new TreeNode(o);
}
else {
parent.right = new TreeNode(o);
}
}
return true; // Element inserted
}
public void breadth() {
breadth(root);
}
// Implement this method to produce a breadth first
// search traversal
public void breadth(TreeNode root){
if (root == null)
return;
System.out.print(root.element + " ");
breadth(root.left);
breadth(root.right);
}
/** Inorder traversal */
public void inorder() {
inorder(root);
}
/** Inorder traversal from a subtree */
private void inorder(TreeNode root) {
if (root == null) {
return;
}
inorder(root.left);
System.out.print(root.element + " ");
inorder(root.right);
}
/** Postorder traversal */
public void postorder() {
postorder(root);
}
/** Postorder traversal from a subtree */
private void postorder(TreeNode root) {
if (root == null) {
return;
}
postorder(root.left);
postorder(root.right);
System.out.print(root.element + " ");
}
/** Preorder traversal */
public void preorder() {
preorder(root);
}
/** Preorder traversal from a subtree */
private void preorder(TreeNode root) {
if (root == null) {
return;
}
System.out.print(root.element + " ");
preorder(root.left);
preorder(root.right);
}
/** Inner class tree node */
private class TreeNode {
Object element;
TreeNode left;
TreeNode right;
public TreeNode(Object o) {
element = o;
}
}
}
import java.util.LinkedList;
导入java.util.Queue;
公开课练习25_1{
公共静态void main(字符串[]args){
BinaryTree=newbinarytree(新整数[]{10,5,15,12,4,8});
系统输出打印(“\n顺序:”);
tree.inoorder();
系统输出打印(“\n优先级:”);
tree.preorder();
系统输出打印(“\nPostorder:”);
tree.postorder();
//调用宽度方法来测试它
系统输出打印(“\nReadthFirst:”);
树的宽度();
}
}
类二叉树{
独活根;
/**创建默认的二叉树*/
公共二叉树(){
}
/**从对象数组创建二叉树*/
公共二进制树(对象[]对象){
for(int i=0;i0,则为else{
父项=当前;
current=current.right;
}
否则{
返回false;//未插入重复节点
}
}
//创建新节点并将其附加到父节点
if(((可比)o).compareTo(父元素)<0){
parent.left=新的树节点(o);
}
否则{
parent.right=新的树节点(o);
}
}
返回true;//插入的元素
}
公共空间宽度(){
宽度(根);
}
//实现此方法以首先生成宽度
//搜索遍历
公共空隙宽度(树根){
if(root==null)
返回;
System.out.print(root.element+“”);
宽度(根。左);
宽度(根,右);
}
/**有序遍历*/
公共无效序(){
顺序(根);
}
/**按顺序从子树进行遍历*/
私有void索引(树节点根){
if(root==null){
返回;
}
顺序(根。左);
System.out.print(root.element+“”);
顺序(root.right);
}
/**后序遍历*/
公众邮购无效(){
后序(根);
}
/**子树的后序遍历*/
专用无效后订单(TreeNode根){
if(root==null){
返回;
}
后序(根,左);
postorder(root.right);
System.out.print(root.element+“”);
}
/**前序遍历*/
公共无效预订单(){
前序(根);
}
/**子树的前序遍历*/
私有void预订单(树节点根){
if(root==null){
返回;
}
System.out.print(root.element+“”);
前序(根,左);
前序(root.right);
}
/**内部类树节点*/
私有类树节点{
对象元素;
左树突;
特雷诺德右翼;
公共树节点(对象o){
元素=o;
}
}
}
您似乎不是在要求实现,因此我将尝试解释该过程
使用队列。将根节点添加到队列中。运行循环,直到队列为空。在循环内部,将第一个元素出列并打印出来。然后将其所有子级添加到队列的后面(通常从左到右)
当队列为空时,应已打印出每个元素
另外,wikipedia上对宽度优先搜索有一个很好的解释:宽度优先是一个队列,深度优先是一个堆栈 对于宽度优先,将所有子级添加到队列中,然后拉动头部并使用同一队列对其执行宽度优先搜索 对于深度优先,将所有子节点添加到堆栈中,然后使用相同的堆栈在该节点上弹出并执行深度优先。宽度优先搜索
Queue<TreeNode> queue = new LinkedList<BinaryTree.TreeNode>() ;
public void breadth(TreeNode root) {
if (root == null)
return;
queue.clear();
queue.add(root);
while(!queue.isEmpty()){
TreeNode node = queue.remove();
System.out.print(node.element + " ");
if(node.left != null) queue.add(node.left);
if(node.right != null) queue.add(node.right);
}
}
Queue Queue=newlinkedlist();
公共空隙宽度(树根){
if(root==null)
返回;
queue.clear();
添加(根);
而(!queue.isEmpty()){
TreeNode节点=queue.remove();
System.out.print(node.element+“”);
如果(node.left!=null)queue.add(node.left);
如果(node.right!=null)queue.add(node.right);
}
}
//遍历
公共空间遍历()
{
if(node==null)
System.out.println(“空树”);
其他的
{
队列q=新的LinkedList();
q、 添加(节点);
while(q.peek()!=null)
{
节点温度=q.remove();
System.out.println(temp.getData());
如果(左侧温度!=null)
q、 添加(左侧温度);
如果(临时正确!=null)
q、 添加(右侧温度);
}
}
}
}这是您编写的代码
//traverse
public void traverse()
{
if(node == null)
System.out.println("Empty tree");
else
{
Queue<Node> q= new LinkedList<Node>();
q.add(node);
while(q.peek() != null)
{
Node temp = q.remove();
System.out.println(temp.getData());
if(temp.left != null)
q.add(temp.left);
if(temp.right != null)
q.add(temp.right);
}
}
}
// search traversal
public void breadth(TreeNode root){
if (root == null)
return;
System.out.print(root.element + " ");
breadth(root.left);
breadth(root.right);
}
public static boolean BFS(ListNode n, int x){
if(n==null){
return false;
}
Queue<ListNode<Integer>> q = new Queue<ListNode<Integer>>();
ListNode<Integer> tmp = new ListNode<Integer>();
q.enqueue(n);
tmp = q.dequeue();
if(tmp.val == x){
return true;
}
while(tmp != null){
for(ListNode<Integer> child: n.getChildren()){
if(child.val == x){
return true;
}
q.enqueue(child);
}
tmp = q.dequeue();
}
return false;
}
public void breadthFirstSearch(Node root, Consumer<String> c) {
List<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
Node n = queue.remove(0);
c.accept(n.value);
if (n.left != null)
queue.add(n.left);
if (n.right != null)
queue.add(n.right);
}
}
public static class Node {
String value;
Node left;
Node right;
public Node(final String value, final Node left, final Node right) {
this.value = value;
this.left = left;
this.right = right;
}
}
Queue<TreeNode> queue= new LinkedList<>();
private void breadthWiseTraversal(TreeNode root) {
if(root==null){
return;
}
TreeNode temp = root;
queue.clear();
((LinkedList<TreeNode>) queue).add(temp);
while(!queue.isEmpty()){
TreeNode ref= queue.remove();
System.out.print(ref.data+" ");
if(ref.left!=null) {
((LinkedList<TreeNode>) queue).add(ref.left);
}
if(ref.right!=null) {
((LinkedList<TreeNode>) queue).add(ref.right);
}
}
}