Skip to content

Commit df310be

Browse files
Merge pull request #195 from ayaanfaiz/master
Added Trees Basic Programs.
2 parents d77ce80 + 98ef327 commit df310be

File tree

3 files changed

+245
-0
lines changed

3 files changed

+245
-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+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Java program to print top view of Binary tree
2+
import java.util.*;
3+
4+
// Class for a tree node
5+
class TreeNode
6+
{
7+
// Members
8+
int key;
9+
TreeNode left, right;
10+
11+
// Constructor
12+
public TreeNode(int key)
13+
{
14+
this.key = key;
15+
left = right = null;
16+
}
17+
}
18+
19+
// A class to represent a queue item. The queue is used to do Level
20+
// order traversal. Every Queue item contains node and horizontal
21+
// distance of node from root
22+
class QItem
23+
{
24+
TreeNode node;
25+
int hd;
26+
public QItem(TreeNode n, int h)
27+
{
28+
node = n;
29+
hd = h;
30+
}
31+
}
32+
33+
// Class for a Binary Tree
34+
class Tree
35+
{
36+
TreeNode root;
37+
38+
// Constructors
39+
public Tree() { root = null; }
40+
public Tree(TreeNode n) { root = n; }
41+
42+
// This method prints nodes in top view of binary tree
43+
public void printTopView()
44+
{
45+
// base case
46+
if (root == null) { return; }
47+
48+
// Creates an empty hashset
49+
HashSet<Integer> set = new HashSet<>();
50+
51+
// Create a queue and add root to it
52+
Queue<QItem> Q = new LinkedList<QItem>();
53+
Q.add(new QItem(root, 0)); // Horizontal distance of root is 0
54+
55+
// Standard BFS or level order traversal loop
56+
while (!Q.isEmpty())
57+
{
58+
// Remove the front item and get its details
59+
QItem qi = Q.remove();
60+
int hd = qi.hd;
61+
TreeNode n = qi.node;
62+
63+
// If this is the first node at its horizontal distance,
64+
// then this node is in top view
65+
if (!set.contains(hd))
66+
{
67+
set.add(hd);
68+
System.out.print(n.key + " ");
69+
}
70+
71+
// Enqueue left and right children of current node
72+
if (n.left != null)
73+
Q.add(new QItem(n.left, hd-1));
74+
if (n.right != null)
75+
Q.add(new QItem(n.right, hd+1));
76+
}
77+
}
78+
}
79+
80+
// Driver class to test above methods
81+
public class Main
82+
{
83+
public static void main(String[] args)
84+
{
85+
/* Create following Binary Tree
86+
1
87+
/ \
88+
2 3
89+
\
90+
4
91+
\
92+
5
93+
\
94+
6*/
95+
TreeNode root = new TreeNode(1);
96+
root.left = new TreeNode(2);
97+
root.right = new TreeNode(3);
98+
root.left.right = new TreeNode(4);
99+
root.left.right.right = new TreeNode(5);
100+
root.left.right.right.right = new TreeNode(6);
101+
Tree t = new Tree(root);
102+
System.out.println("Following are nodes in top view of Binary Tree");
103+
t.printTopView();
104+
}
105+
}

0 commit comments

Comments
 (0)