Skip to content

Commit 6823278

Browse files
committed
Added comments to level order traversal method, and samples in main method.
1 parent 1e52ba3 commit 6823278

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

data_structures/Trees/TreeTraversal.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import java.util.LinkedList;
22

33
/**
4-
*
5-
* @author Varun Upadhyay (https://github.com/varunu28)
6-
*
7-
*/
4+
*
5+
* @author Varun Upadhyay (https://github.com/varunu28)
6+
*
7+
*/
88

99
// Driver Program
1010
public class TreeTraversal {
@@ -17,31 +17,33 @@ public static void main(String[] args) {
1717
tree.insert(6);
1818
tree.insert(8);
1919

20+
// Prints 5 3 2 4 7 6 8
2021
System.out.println("Pre order traversal:");
2122
tree.printPreOrder();
2223
System.out.println();
23-
24+
// Prints 2 3 4 5 6 7 8
2425
System.out.println("In order traversal:");
2526
tree.printInOrder();
2627
System.out.println();
27-
28+
// Prints 2 4 3 6 8 7 5
2829
System.out.println("Post order traversal:");
2930
tree.printPostOrder();
3031
System.out.println();
31-
32+
// Prints 5 3 7 2 4 6 8
3233
System.out.println("Level order traversal:");
3334
tree.printLevelOrder();
3435
System.out.println();
3536
}
3637
}
3738

3839
/**
39-
* The Node class which initializes a Node of a tree
40-
* Consists of all 3 traversal methods: printInOrder, printPostOrder & printPreOrder
41-
* printInOrder: LEFT -> ROOT -> RIGHT
42-
* printPreOrder: ROOT -> LEFT -> RIGHT
43-
* printPostOrder: LEFT -> RIGHT -> ROOT
44-
*/
40+
* The Node class which initializes a Node of a tree
41+
* Consists of all 3 traversal methods: printInOrder, printPostOrder & printPreOrder
42+
* printInOrder: LEFT -> ROOT -> RIGHT
43+
* printPreOrder: ROOT -> LEFT -> RIGHT
44+
* printPostOrder: LEFT -> RIGHT -> ROOT
45+
* printLevelOrder: Prints by level (starting at root), from left to right.
46+
*/
4547
class Node {
4648
Node left, right;
4749
int data;
@@ -99,18 +101,23 @@ public void printPostOrder() {
99101
System.out.print(data + " ");
100102
}
101103

104+
/**
105+
* O(n) time algorithm.
106+
* Uses O(n) space to store nodes in a queue to aid in traversal.
107+
*/
102108
public void printLevelOrder() {
103-
LinkedList<Node> queue = new LinkedList<>();
104-
queue.add(this);
105-
while (queue.size() > 0) {
106-
Node head = queue.remove();
107-
System.out.print(head.data + " ");
108-
if (head.left != null) {
109-
queue.add(head.left);
110-
}
111-
if (head.right != null) {
112-
queue.add(head.right);
113-
}
114-
}
115-
}
109+
LinkedList<Node> queue = new LinkedList<>();
110+
queue.add(this);
111+
while (queue.size() > 0) {
112+
Node head = queue.remove();
113+
System.out.print(head.data + " ");
114+
// Add children of recently-printed node to queue, if they exist.
115+
if (head.left != null) {
116+
queue.add(head.left);
117+
}
118+
if (head.right != null) {
119+
queue.add(head.right);
120+
}
121+
}
122+
}
116123
}

0 commit comments

Comments
 (0)