Skip to content

Commit ef1aa79

Browse files
Merge pull request #103 from mpokryva/level-order
Added level order traversal, and more nodes in main method
2 parents 956e6e5 + bb6f827 commit ef1aa79

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

data_structures/Trees/TreeTraversal.java

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
1-
/**
2-
*
3-
* @author Varun Upadhyay (https://github.com/varunu28)
4-
*
5-
*/
61
import java.util.LinkedList;
72

3+
/**
4+
*
5+
* @author Varun Upadhyay (https://github.com/varunu28)
6+
*
7+
*/
8+
9+
810
// Driver Program
911
public class TreeTraversal {
1012
public static void main(String[] args) {
1113
Node tree = new Node(5);
1214
tree.insert(3);
15+
tree.insert(2);
1316
tree.insert(7);
14-
tree.insert(1);
15-
tree.insert(9);
16-
17-
// Prints 1 3 5 7 9
18-
tree.printInOrder();
19-
System.out.println();
17+
tree.insert(4);
18+
tree.insert(6);
19+
tree.insert(8);
2020

21-
// Prints 5 3 1 7 9
21+
// Prints 5 3 2 4 7 6 8
22+
System.out.println("Pre order traversal:");
2223
tree.printPreOrder();
2324
System.out.println();
24-
25-
// Prints 1 3 9 7 5
25+
// Prints 2 3 4 5 6 7 8
26+
System.out.println("In order traversal:");
27+
tree.printInOrder();
28+
System.out.println();
29+
// Prints 2 4 3 6 8 7 5
30+
System.out.println("Post order traversal:");
2631
tree.printPostOrder();
2732
System.out.println();
28-
29-
// Add a couple more nodes for print level test
30-
// Print 5 3 7 1 9
33+
// Prints 5 3 7 2 4 6 8
34+
System.out.println("Level order traversal:");
3135
tree.printLevelOrder();
3236
System.out.println();
3337
}
3438
}
3539

3640
/**
37-
* The Node class which initializes a Node of a tree
38-
* Consists of all 3 traversal methods: printInOrder, printPostOrder & printPreOrder
39-
* printInOrder: LEFT -> ROOT -> RIGHT
40-
* printPreOrder: ROOT -> LEFT -> RIGHT
41-
* printPostOrder: LEFT -> RIGHT -> ROOT
42-
* printLevelOrder: ROOT -> ROOT's CHILDREN -> ROOT's CHILDREN's CHILDREN -> etc
43-
*/
41+
* The Node class which initializes a Node of a tree
42+
* Consists of all 3 traversal methods: printInOrder, printPostOrder & printPreOrder
43+
* printInOrder: LEFT -> ROOT -> RIGHT
44+
* printPreOrder: ROOT -> LEFT -> RIGHT
45+
* printPostOrder: LEFT -> RIGHT -> ROOT
46+
* printLevelOrder: Prints by level (starting at root), from left to right.
47+
*/
4448
class Node {
4549
Node left, right;
4650
int data;
@@ -98,19 +102,23 @@ public void printPostOrder() {
98102
System.out.print(data + " ");
99103
}
100104

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

0 commit comments

Comments
 (0)