Merge pull request #103 from mpokryva/level-order
Added level order traversal, and more nodes in main method
This commit is contained in:
commit
ef1aa79bd5
@ -1,46 +1,50 @@
|
|||||||
/**
|
|
||||||
*
|
|
||||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
// Driver Program
|
// Driver Program
|
||||||
public class TreeTraversal {
|
public class TreeTraversal {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Node tree = new Node(5);
|
Node tree = new Node(5);
|
||||||
tree.insert(3);
|
tree.insert(3);
|
||||||
|
tree.insert(2);
|
||||||
tree.insert(7);
|
tree.insert(7);
|
||||||
tree.insert(1);
|
tree.insert(4);
|
||||||
tree.insert(9);
|
tree.insert(6);
|
||||||
|
tree.insert(8);
|
||||||
|
|
||||||
// Prints 1 3 5 7 9
|
// Prints 5 3 2 4 7 6 8
|
||||||
tree.printInOrder();
|
System.out.println("Pre order traversal:");
|
||||||
System.out.println();
|
|
||||||
|
|
||||||
// Prints 5 3 1 7 9
|
|
||||||
tree.printPreOrder();
|
tree.printPreOrder();
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
// Prints 2 3 4 5 6 7 8
|
||||||
// Prints 1 3 9 7 5
|
System.out.println("In order traversal:");
|
||||||
|
tree.printInOrder();
|
||||||
|
System.out.println();
|
||||||
|
// Prints 2 4 3 6 8 7 5
|
||||||
|
System.out.println("Post order traversal:");
|
||||||
tree.printPostOrder();
|
tree.printPostOrder();
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
// Prints 5 3 7 2 4 6 8
|
||||||
// Add a couple more nodes for print level test
|
System.out.println("Level order traversal:");
|
||||||
// Print 5 3 7 1 9
|
|
||||||
tree.printLevelOrder();
|
tree.printLevelOrder();
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Node class which initializes a Node of a tree
|
* The Node class which initializes a Node of a tree
|
||||||
* Consists of all 3 traversal methods: printInOrder, printPostOrder & printPreOrder
|
* Consists of all 3 traversal methods: printInOrder, printPostOrder & printPreOrder
|
||||||
* printInOrder: LEFT -> ROOT -> RIGHT
|
* printInOrder: LEFT -> ROOT -> RIGHT
|
||||||
* printPreOrder: ROOT -> LEFT -> RIGHT
|
* printPreOrder: ROOT -> LEFT -> RIGHT
|
||||||
* printPostOrder: LEFT -> RIGHT -> ROOT
|
* printPostOrder: LEFT -> RIGHT -> ROOT
|
||||||
* printLevelOrder: ROOT -> ROOT's CHILDREN -> ROOT's CHILDREN's CHILDREN -> etc
|
* printLevelOrder: Prints by level (starting at root), from left to right.
|
||||||
*/
|
*/
|
||||||
class Node {
|
class Node {
|
||||||
Node left, right;
|
Node left, right;
|
||||||
int data;
|
int data;
|
||||||
@ -98,19 +102,23 @@ class Node {
|
|||||||
System.out.print(data + " ");
|
System.out.print(data + " ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* O(n) time algorithm.
|
||||||
|
* Uses O(n) space to store nodes in a queue to aid in traversal.
|
||||||
|
*/
|
||||||
public void printLevelOrder() {
|
public void printLevelOrder() {
|
||||||
LinkedList<Node> queue = new LinkedList<>();
|
LinkedList<Node> queue = new LinkedList<>();
|
||||||
queue.add(this);
|
queue.add(this);
|
||||||
while(!queue.isEmpty()) {
|
while (queue.size() > 0) {
|
||||||
Node n = queue.poll();
|
Node head = queue.remove();
|
||||||
System.out.print(n.data + " ");
|
System.out.print(head.data + " ");
|
||||||
if (n.left != null) {
|
// Add children of recently-printed node to queue, if they exist.
|
||||||
queue.add(n.left);
|
if (head.left != null) {
|
||||||
|
queue.add(head.left);
|
||||||
}
|
}
|
||||||
if (n.right != null) {
|
if (head.right != null) {
|
||||||
queue.add(n.right);
|
queue.add(head.right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user