Skip to content

Commit 98ef327

Browse files
committed
Level Order Traversal(2 methods)
1 parent 12af048 commit 98ef327

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.util.Queue;
2+
import java.util.LinkedList;
3+
4+
/* Class to represent Tree node */
5+
class Node {
6+
int data;
7+
Node left, right;
8+
9+
public Node(int item) {
10+
data = item;
11+
left = null;
12+
right = null;
13+
}
14+
}
15+
16+
/* Class to print Level Order Traversal */
17+
class BinaryTree {
18+
19+
Node root;
20+
21+
/* Given a binary tree. Print its nodes in level order
22+
using array for implementing queue */
23+
void printLevelOrder()
24+
{
25+
Queue<Node> queue = new LinkedList<Node>();
26+
queue.add(root);
27+
while (!queue.isEmpty())
28+
{
29+
30+
/* poll() removes the present head.
31+
For more information on poll() visit
32+
http://www.tutorialspoint.com/java/util/linkedlist_poll.htm */
33+
Node tempNode = queue.poll();
34+
System.out.print(tempNode.data + " ");
35+
36+
/*Enqueue left child */
37+
if (tempNode.left != null) {
38+
queue.add(tempNode.left);
39+
}
40+
41+
/*Enqueue right child */
42+
if (tempNode.right != null) {
43+
queue.add(tempNode.right);
44+
}
45+
}
46+
}
47+
48+
public static void main(String args[])
49+
{
50+
/* creating a binary tree and entering
51+
the nodes */
52+
BinaryTree tree_level = new BinaryTree();
53+
tree_level.root = new Node(1);
54+
tree_level.root.left = new Node(2);
55+
tree_level.root.right = new Node(3);
56+
tree_level.root.left.left = new Node(4);
57+
tree_level.root.left.right = new Node(5);
58+
59+
System.out.println("Level order traversal of binary tree is - ");
60+
tree_level.printLevelOrder();
61+
}
62+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
class Node
2+
{
3+
int data;
4+
Node left, right;
5+
public Node(int item)
6+
{
7+
data = item;
8+
left = right = null;
9+
}
10+
}
11+
12+
class BinaryTree
13+
{
14+
// Root of the Binary Tree
15+
Node root;
16+
17+
public BinaryTree()
18+
{
19+
root = null;
20+
}
21+
22+
/* function to print level order traversal of tree*/
23+
void printLevelOrder()
24+
{
25+
int h = height(root);
26+
int i;
27+
for (i=1; i<=h; i++)
28+
printGivenLevel(root, i);
29+
}
30+
31+
/* Compute the "height" of a tree -- the number of
32+
nodes along the longest path from the root node
33+
down to the farthest leaf node.*/
34+
int height(Node root)
35+
{
36+
if (root == null)
37+
return 0;
38+
else
39+
{
40+
/* compute height of each subtree */
41+
int lheight = height(root.left);
42+
int rheight = height(root.right);
43+
44+
/* use the larger one */
45+
if (lheight > rheight)
46+
return(lheight+1);
47+
else return(rheight+1);
48+
}
49+
}
50+
51+
/* Print nodes at the given level */
52+
void printGivenLevel (Node root ,int level)
53+
{
54+
if (root == null)
55+
return;
56+
if (level == 1)
57+
System.out.print(root.data + " ");
58+
else if (level > 1)
59+
{
60+
printGivenLevel(root.left, level-1);
61+
printGivenLevel(root.right, level-1);
62+
}
63+
}
64+
65+
/* Driver program to test above functions */
66+
public static void main(String args[])
67+
{
68+
BinaryTree tree = new BinaryTree();
69+
tree.root= new Node(1);
70+
tree.root.left= new Node(2);
71+
tree.root.right= new Node(3);
72+
tree.root.left.left= new Node(4);
73+
tree.root.left.right= new Node(5);
74+
75+
System.out.println("Level order traversal of binary tree is ");
76+
tree.printLevelOrder();
77+
}
78+
}

0 commit comments

Comments
 (0)