Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Queue;
- public class BFS {
- public void levelOrderQueue(Node root) {
- Queue<Node> qund = new LinkedList<Node>();
- if (root == null)
- return;
- qund.add(root);
- while (!qund.isEmpty()) {
- Node nde = (Node) qund.remove();
- System.out.print(" " + nde.data);
- if (nde.left != null)
- qund.add(nde.left);
- if (nde.down != null)
- qund.add(nde.down);
- if (nde.right != null)
- qund.add(nde.right);
- }
- }
- public static void main(String[] args) throws java.lang.Exception {
- Node root=new Node(1);
- root.left=new Node(2);
- root.down=new Node(3);
- root.right=new Node(4);
- root.left.left=new Node(5);
- root.left.down=new Node(6);
- root.right.down=new Node(7);
- root.right.right=new Node(8);
- root.left.left.left=new Node(9);
- root.left.left.down=new Node(10);
- root.right.down.down=new Node(11);
- root.right.down.right=new Node(12);
- BFS i = new BFS();
- System.out.println("BFS:");
- i.levelOrderQueue(root);
- }
- static class Node {
- int data;
- Node left;
- Node right;
- Node down;
- public Node(int data) {
- this.data = data;
- this.left = null;
- this.right = null;
- this.down = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement