Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2fab9ef

Browse files
committed
BF traversal added
1 parent 9e563d9 commit 2fab9ef

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/main/java/com/rampatra/common/BinaryNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
public class BinaryNode<E extends Comparable<E>> {
1111

1212
public E value;
13-
public BinaryNode left;
14-
public BinaryNode right;
13+
public BinaryNode<E> left;
14+
public BinaryNode<E> right;
1515

16-
public BinaryNode(E value, BinaryNode left, BinaryNode right) {
16+
public BinaryNode(E value, BinaryNode<E> left, BinaryNode<E> right) {
1717
this.value = value;
1818
this.left = left;
1919
this.right = right;

src/main/java/com/rampatra/trees/BFSUsingQueue.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.rampatra.common.LinkedQueue;
66
import com.rampatra.common.Queue;
77

8+
import java.util.LinkedList;
89
import java.util.NoSuchElementException;
910

1011
import static java.lang.System.out;
@@ -20,13 +21,16 @@ public class BFSUsingQueue {
2021

2122
/**
2223
* Breadth first traversal (Level-order traversal using Queue).
24+
*
25+
* @param node a tree node with left and right references and a value of type {@code E}
26+
* @param <E> the type of value that the {@code node} holds
2327
*/
2428
public static <E extends Comparable<E>> void breadthFirstTraversalUsingQueue(BinaryNode<E> node) {
2529
Queue<BinaryNode<E>> queue = new LinkedQueue<>();
2630
breadthFirstTraversalUsingQueue(node, queue);
2731
}
2832

29-
public static <E extends Comparable<E>> void breadthFirstTraversalUsingQueue(BinaryNode<E> node,
33+
private static <E extends Comparable<E>> void breadthFirstTraversalUsingQueue(BinaryNode<E> node,
3034
Queue<BinaryNode<E>> queue) {
3135

3236
if (node != null) {
@@ -42,12 +46,33 @@ public static <E extends Comparable<E>> void breadthFirstTraversalUsingQueue(Bin
4246
}
4347
}
4448

45-
public static <E extends Comparable<E>> void printValue(BinaryNode<E> node) {
49+
private static <E extends Comparable<E>> void printValue(BinaryNode<E> node) {
4650
if (node == null) return;
4751

4852
out.print(node.value);
4953
}
5054

55+
/**
56+
* Level order traversal using queue but iteratively.
57+
*
58+
* @param root the root node from where the traversal should start
59+
* @param <E> type of the {@code value} that {@code BinaryNode} holds
60+
*/
61+
public static <E extends Comparable<E>> void breadthFirstTraversalUsingQueueIterative(BinaryNode<E> root) {
62+
if (root == null) return;
63+
64+
Queue<BinaryNode<E>> q = new LinkedQueue<>();
65+
q.add(root);
66+
67+
while (!q.isEmpty()) {
68+
BinaryNode<E> node = q.remove();
69+
out.print(node.value);
70+
71+
if (node.left != null) q.add(node.left);
72+
if (node.right != null) q.add(node.right);
73+
}
74+
}
75+
5176
public static void main(String a[]) {
5277
BinaryTree<Integer> bt = new BinaryTree<>();
5378
bt.put(6);
@@ -57,5 +82,7 @@ public static void main(String a[]) {
5782
bt.put(8);
5883
bt.put(9);
5984
breadthFirstTraversalUsingQueue(bt.root);
85+
System.out.println();
86+
breadthFirstTraversalUsingQueueIterative(bt.root);
6087
}
6188
}

0 commit comments

Comments
 (0)