|
1 |
| -/** |
2 |
| - * |
3 |
| - * @author Varun Upadhyay (https://github.com/varunu28) |
4 |
| - * |
5 |
| - */ |
6 | 1 | import java.util.LinkedList;
|
7 | 2 |
|
| 3 | +/** |
| 4 | +* |
| 5 | +* @author Varun Upadhyay (https://github.com/varunu28) |
| 6 | +* |
| 7 | +*/ |
| 8 | + |
| 9 | + |
8 | 10 | // Driver Program
|
9 | 11 | public class TreeTraversal {
|
10 | 12 | public static void main(String[] args) {
|
11 | 13 | Node tree = new Node(5);
|
12 | 14 | tree.insert(3);
|
| 15 | + tree.insert(2); |
13 | 16 | 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); |
20 | 20 |
|
21 |
| - // Prints 5 3 1 7 9 |
| 21 | + // Prints 5 3 2 4 7 6 8 |
| 22 | + System.out.println("Pre order traversal:"); |
22 | 23 | tree.printPreOrder();
|
23 | 24 | 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:"); |
26 | 31 | tree.printPostOrder();
|
27 | 32 | 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:"); |
31 | 35 | tree.printLevelOrder();
|
32 | 36 | System.out.println();
|
33 | 37 | }
|
34 | 38 | }
|
35 | 39 |
|
36 | 40 | /**
|
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 | +*/ |
44 | 48 | class Node {
|
45 | 49 | Node left, right;
|
46 | 50 | int data;
|
@@ -98,19 +102,23 @@ public void printPostOrder() {
|
98 | 102 | System.out.print(data + " ");
|
99 | 103 | }
|
100 | 104 |
|
| 105 | + /** |
| 106 | + * O(n) time algorithm. |
| 107 | + * Uses O(n) space to store nodes in a queue to aid in traversal. |
| 108 | + */ |
101 | 109 | public void printLevelOrder() {
|
102 | 110 | LinkedList<Node> queue = new LinkedList<>();
|
103 | 111 | 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); |
109 | 118 | }
|
110 |
| - if (n.right != null) { |
111 |
| - queue.add(n.right); |
| 119 | + if (head.right != null) { |
| 120 | + queue.add(head.right); |
112 | 121 | }
|
113 | 122 | }
|
114 | 123 | }
|
115 | 124 | }
|
116 |
| - |
|
0 commit comments