Skip to content

Commit 8831f7c

Browse files
committed
#Modification 21
1 parent 2ba2114 commit 8831f7c

14 files changed

Lines changed: 254 additions & 4 deletions

binaryTree/BT_Problem_01.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
// Find Level order traversal of binary tree
55
class Node{
66
int data;
7+
int hd;
78
Node left, right;
89

910
public Node(int item) {
1011
data = item;
11-
left = null;
12-
right = null;
12+
hd = Integer.MAX_VALUE;
13+
left = right = null;
1314
}
1415
}
1516
public class BT_Problem_01 {

binaryTree/BT_Problem_10.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77
*/
88
public class BT_Problem_10 {
99

10+
Node root;
11+
static int max_level = 0;
12+
13+
void leftViewUtil(Node node, int level) {
14+
15+
if(node == null)
16+
return;
17+
18+
if(max_level < level) {
19+
System.out.print(" " + node.data);
20+
max_level = level;
21+
}
22+
23+
24+
}
1025
public static void main(String[] args) {
1126

1227

binaryTree/BT_Problem_11.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,75 @@
11
package binaryTree;
2+
import java.util.*;
3+
import java.util.Map.Entry;
24

5+
/*
6+
* Problem Title :- Write a Java program to find Top View of Tree.
7+
*/
8+
// Class of binary Tree
39
public class BT_Problem_11 {
410

11+
Node root;
12+
13+
public BT_Problem_11() {
14+
root = null;
15+
}
16+
17+
// function should print the topView of the binary tree
18+
private void TopView(Node root) {
19+
class QueueObj{
20+
Node node;
21+
int hd;
22+
QueueObj(Node node, int hd){
23+
this.node = node;
24+
this.hd = hd;
25+
}
26+
}
27+
28+
Queue<QueueObj> q = new LinkedList<>();
29+
Map<Integer, Node> topViewMap = new TreeMap<Integer, Node>();
30+
31+
if(root == null)
32+
return;
33+
else
34+
q.add(new QueueObj(root, 0));
35+
36+
System.out.println("The top view of the tree is : ");
37+
38+
/* count function returns 1 if the container
39+
* contains an element whose key is equivalent to hd,
40+
* or returns zero otherwise. */
41+
while(!q.isEmpty()) {
42+
QueueObj tempNode = q.poll();
43+
44+
if(!topViewMap.containsKey(tempNode.hd))
45+
topViewMap.put(tempNode.hd, tempNode.node);
46+
47+
if(tempNode.node.left != null)
48+
q.add(new QueueObj(tempNode.node.left, tempNode.hd + 1));
49+
50+
if(tempNode.node.left != null)
51+
q.add(new QueueObj(tempNode.node.left, tempNode.hd + 1));
52+
}
53+
54+
for(Entry<Integer, Node> entry : topViewMap.entrySet())
55+
System.out.print(entry.getValue().data);
56+
}
57+
58+
// Driver Program to test & run above functions
559
public static void main(String[] args) {
660

61+
BT_Problem_11 tree = new BT_Problem_11();
62+
63+
tree.root = new Node(1);
64+
tree.root.left = new Node(2);
65+
tree.root.right = new Node(3);
66+
tree.root.left.right = new Node(4);
67+
tree.root.left.right.right = new Node(4);
68+
tree.root.left.right.right.right = new Node(4);
69+
70+
System.out.println("Following are nodes in top view of Binary Tree");
71+
tree.TopView(tree.root);
72+
773
}
874

975
}

binaryTree/BT_Problem_12.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,64 @@
11
package binaryTree;
2+
import java.util.*;
23

4+
/*
5+
* Problem Title :- Write a Java program to find Bottom View of Tree.
6+
*/
37
public class BT_Problem_12 {
48

9+
Node root;
10+
11+
public BT_Problem_12() {}
12+
13+
public BT_Problem_12(Node node){
14+
root = node;
15+
}
16+
17+
private void BottomView() {
18+
19+
if(root == null)
20+
return;
21+
22+
// Initialize a variable 'hd' with 0 for the root element.
23+
int hd = 0;
24+
25+
// TreeMap which stores key value pair sorted on key value
26+
Map<Integer, Integer> map= new TreeMap<>();
27+
28+
// Queue to store tree nodes in level order traversal
29+
Queue<Node> q = new LinkedList<>();
30+
31+
// Assign initialized horizontal distance value to root node and add it to the queue.
32+
root.hd = hd;
33+
q.add(root);
34+
35+
while(!q.isEmpty()) {
36+
Node temp = q.remove();
37+
38+
hd = temp.hd;
39+
40+
map.put(hd, temp.data);
41+
42+
if(temp.left != null) {
43+
temp.left.hd = hd-1;
44+
q.add(temp.left);
45+
}
46+
47+
if(temp.right != null) {
48+
temp.right.hd = hd+1;
49+
q.add(temp.right);
50+
}
51+
52+
}
53+
54+
Set<Entry<Integer, Integer>> set = map.entrySet();
55+
56+
Iterator<Entry<Integer, Integer>> iterator = set.iterator();
57+
}
58+
59+
560
public static void main(String[] args) {
6-
// TODO Auto-generated method stub
7-
61+
862
}
963

1064
}

bitManipulation/BM_01.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package bitManipulation;
2+
3+
/*
4+
* Problem Title :-> Count set bits in an integer
5+
*/
6+
public class BM_01 {
7+
8+
// Function to get no of set bits in binary representation of positive integer n
9+
static int countSetBits(int n) {
10+
int count = 0;
11+
while(n > 0) {
12+
count += n & 1;
13+
n >>= 1;
14+
}
15+
return count;
16+
}
17+
18+
// driver program
19+
public static void main(String[] args) {
20+
int i = 9;
21+
System.out.println(countSetBits(i));
22+
}
23+
24+
}

bitManipulation/BM_02.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package bitManipulation;
2+
3+
public class BM_02 {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
}

bitManipulation/BM_03.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package bitManipulation;
2+
3+
public class BM_03 {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
}

bitManipulation/BM_04.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package bitManipulation;
2+
3+
public class BM_04 {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
}

bitManipulation/BM_05.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package bitManipulation;
2+
3+
public class BM_05 {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
}

bitManipulation/BM_06.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package bitManipulation;
2+
3+
public class BM_06 {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
}

0 commit comments

Comments
 (0)