Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Queue;
- public class T_BFS {
- public void levelOrderQueue(Node root) {
- Queue<Node> q = new LinkedList<Node>();
- if (root == null)
- return;
- q.add(root);
- while (!q.isEmpty()) {
- Node n = (Node) q.remove();
- System.out.print(" " + n.data);
- if (n.left != null)
- q.add(n.left);
- if (n.right != null)
- q.add(n.right);
- }
- }
- public static void main(String[] args) throws java.lang.Exception {
- Node root = new Node("Динка и Стоян");
- root.left = new Node("Николета и Динко");
- root.right = new Node("Никола и Стоян");
- T_BFS i = new T_BFS();
- System.out.println("Breadth First Search : ");
- i.levelOrderQueue(root);
- }
- }
- class Node {
- String data;
- Node left;
- Node right;
- public Node(String data) {
- this.data = data;
- this.left = null;
- this.right = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement