Skip to content

Commit 7c19038

Browse files
committed
#Modification 23
1 parent 8831f7c commit 7c19038

5 files changed

Lines changed: 266 additions & 14 deletions

File tree

binaryTree/BT_Problem_12.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package binaryTree;
2+
23
import java.util.*;
4+
import java.util.Map.Entry;
35

46
/*
57
* Problem Title :- Write a Java program to find Bottom View of Tree.
68
*/
7-
public class BT_Problem_12 {
89

10+
class Tree{
911
Node root;
1012

11-
public BT_Problem_12() {}
13+
public Tree() {}
1214

13-
public BT_Problem_12(Node node){
15+
public Tree(Node node){
1416
root = node;
1517
}
1618

17-
private void BottomView() {
19+
void bottomView() {
1820

1921
if(root == null)
2022
return;
@@ -54,11 +56,30 @@ private void BottomView() {
5456
Set<Entry<Integer, Integer>> set = map.entrySet();
5557

5658
Iterator<Entry<Integer, Integer>> iterator = set.iterator();
59+
60+
while(iterator.hasNext()) {
61+
Map.Entry<Integer, Integer> me = iterator.next();
62+
System.out.print(me.getValue() + " ");
63+
}
5764
}
58-
65+
}
66+
67+
public class BT_Problem_12 {
5968

6069
public static void main(String[] args) {
70+
Node root = new Node(20);
71+
root.left = new Node(8);
72+
root.right = new Node(22);
73+
root.left.left = new Node(5);
74+
root.left.right = new Node(3);
75+
root.right.left = new Node(4);
76+
root.right.right = new Node(25);
77+
root.left.right.left = new Node(10);
78+
root.left.right.right = new Node(14);
6179

80+
Tree tree = new Tree(root);
81+
System.out.println("Bottom view of the given binary tree: ");
82+
tree.bottomView();
6283
}
6384

6485
}

binaryTree/BT_Problem_13.java

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,77 @@
11
package binaryTree;
2+
import java.util.*;
3+
4+
/*
5+
* Problem Title :- Zig-Zag traversal of a binary tree
6+
*/
7+
8+
class BinaryTree{
9+
10+
Node root;
11+
12+
void printZigZagTraversal() {
13+
if(root == null)
14+
return;
15+
16+
Stack<Node> currentLevel = new Stack<>();
17+
Stack<Node> nextLevel = new Stack<>();
18+
19+
currentLevel.push(root);
20+
boolean leftToRight = true;;
21+
22+
while(!currentLevel.isEmpty()) {
23+
Node node = currentLevel.pop();
24+
25+
System.out.print(node.data + " ");
26+
27+
if(leftToRight) {
28+
if(node.left != null) {
29+
nextLevel.push(node.left);
30+
}
31+
32+
if(node.left != null) {
33+
nextLevel.push(node.left);
34+
}
35+
}
36+
37+
else {
38+
if(node.right != null) {
39+
nextLevel.push(node.right);
40+
}
41+
if(node.left != null) {
42+
nextLevel.push(node.left);
43+
}
44+
}
45+
46+
if(currentLevel.isEmpty()) {
47+
leftToRight = !leftToRight;
48+
Stack<Node> temp = currentLevel;
49+
currentLevel = nextLevel;
50+
nextLevel = temp;
51+
}
52+
}
53+
}
54+
}
255

356
public class BT_Problem_13 {
457

558
public static void main(String[] args) {
6-
// TODO Auto-generated method stub
7-
59+
60+
BinaryTree tree = new BinaryTree();
61+
62+
tree.root = new Node(1);
63+
tree.root.left = new Node(8);
64+
tree.root.right = new Node(21);
65+
tree.root.left.left = new Node(2);
66+
tree.root.left.right = new Node(3);
67+
tree.root.right.left = new Node(7);
68+
tree.root.right.right = new Node(6);
69+
tree.root.left.right.left = new Node(5);
70+
tree.root.left.right.right = new Node(4);
71+
72+
System.out.println("ZigZag Order traversal tree is ");
73+
tree.printZigZagTraversal();
74+
875
}
976

1077
}

binaryTree/BT_Problem_14.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
11
package binaryTree;
2-
2+
/*
3+
* Problem Title :- Check if a tree is balanced or not
4+
*/
35
public class BT_Problem_14 {
46

7+
Node root;
8+
9+
boolean isBalanced(Node node) {
10+
int lh;
11+
int rh;
12+
13+
if(node == null)
14+
return true;
15+
16+
lh = height(node.left);
17+
rh = height(node.right);
18+
19+
if(Math.abs(lh - rh) <= 1 && isBalanced(node.left) && isBalanced(node.right)) {
20+
return true;
21+
}
22+
return false;
23+
}
24+
25+
int height(Node node) {
26+
if(node == null)
27+
return 0;
28+
29+
return 1 + Math.max(height(node.left), height(node.right));
30+
}
31+
532
public static void main(String[] args) {
6-
// TODO Auto-generated method stub
733

34+
BT_Problem_14 tree = new BT_Problem_14();
35+
36+
tree.root = new Node(1);
37+
tree.root.left = new Node(2);
38+
tree.root.right = new Node(3);
39+
tree.root.left.left = new Node(4);
40+
tree.root.left.right = new Node(5);
41+
tree.root.left.left.left = new Node(8);
42+
43+
if(tree.isBalanced(tree.root))
44+
System.out.println("Tree is balanced");
45+
else
46+
System.out.println("Tree is not balanced");
847
}
9-
1048
}

binaryTree/BT_Problem_15.java

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,66 @@
11
package binaryTree;
2-
2+
import java.util.*;
3+
import java.util.Map.Entry;
4+
/*
5+
* Problem Title :- Diagnol Traversal of a Binary tree
6+
*/
37
public class BT_Problem_15 {
48

9+
static class Node{
10+
int data;
11+
Node left, right;
12+
13+
Node(int data){
14+
this.data = data;
15+
left = null;
16+
right = null;
17+
}
18+
}
19+
20+
static void diagonalPrintUtil(Node root, int d, HashMap<Integer, Vector<Integer>> diagonalPrint) {
21+
if(root == null)
22+
return;
23+
24+
Vector<Integer> k = diagonalPrint.get(d);
25+
26+
if(k == null) {
27+
k = new Vector<>();
28+
k.add(root.data);
29+
}
30+
31+
else {
32+
k.add(root.data);
33+
}
34+
35+
diagonalPrint.put(d,k);
36+
diagonalPrintUtil(root.left, d+1, diagonalPrint);
37+
diagonalPrintUtil(root.right, d, diagonalPrint);
38+
}
39+
40+
static void diagonalPrint(Node root) {
41+
HashMap<Integer, Vector<Integer>> diagonalPrint = new HashMap<>();
42+
43+
diagonalPrintUtil(root, 0, diagonalPrint);
44+
45+
System.out.println("Diagonal Traversal of Binary Tree");
46+
47+
for(Entry<Integer, Vector<Integer>> entry : diagonalPrint.entrySet())
48+
System.out.println(entry.getValue());
49+
}
50+
551
public static void main(String[] args) {
6-
52+
Node root = new Node(8);
53+
54+
root.left = new Node(3);
55+
root.right = new Node(10);
56+
root.left.left = new Node(1);
57+
root.left.right = new Node(6);
58+
root.right.right = new Node(14);
59+
root.right.right.left = new Node(13);
60+
root.left.right.left = new Node(4);
61+
root.left.right.right = new Node(7);
62+
63+
diagonalPrint(root);
764
}
865

966
}

binaryTree/BT_Problem_16.java

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,78 @@
11
package binaryTree;
2-
2+
/*
3+
* Problem Title :- Boundary traversal of a Binary tree
4+
*/
35
public class BT_Problem_16 {
46

7+
Node root;
8+
// printing leaf nodes
9+
void printLeaves(Node node) {
10+
if(node == null)
11+
return;
12+
13+
printLeaves(node.left);
14+
// print if it is a leaf node
15+
if(node.left == null && node.right == null)
16+
System.out.print(node.data + " ");
17+
printLeaves(node.right);
18+
}
19+
20+
void printBoundaryLeft(Node node) {
21+
if(node == null)
22+
return;
23+
if(node.left != null) {
24+
System.out.print(node.data + " ");
25+
printBoundaryLeft(node.left);
26+
}
27+
28+
else if(node.right != null) {
29+
System.out.print(node.data + " ");
30+
printBoundaryLeft(node.right);
31+
}
32+
}
33+
34+
void printBoundaryRight(Node node) {
35+
if(node == null)
36+
return;
37+
if(node.left != null) {
38+
System.out.print(node.data + " ");
39+
printBoundaryRight(node.left);
40+
}
41+
42+
else if(node.right != null) {
43+
System.out.print(node.data + " ");
44+
printBoundaryRight(node.right);
45+
}
46+
}
47+
48+
void printBoundary(Node node) {
49+
if(node == null)
50+
return;
51+
52+
System.out.print(node.data + " ");
53+
54+
printBoundaryLeft(node.left);
55+
56+
printLeaves(node.left);
57+
printLeaves(node.right);
58+
59+
printBoundaryRight(node.right);
60+
}
61+
562
public static void main(String[] args) {
6-
// TODO Auto-generated method stub
63+
BT_Problem_16 tree = new BT_Problem_16();
64+
65+
tree.root = new Node(1);
66+
tree.root.left = new Node(2);
67+
tree.root.left.left = new Node(4);
68+
tree.root.left.right = new Node(5);
69+
tree.root.left.left.left = new Node(8);
70+
tree.root.left.right.left = new Node(10);
71+
tree.root.left.right.right = new Node(14);
72+
tree.root.right = new Node(3);
73+
tree.root.right.right = new Node(22);
74+
75+
tree.printBoundary(tree.root);
776

877
}
978

0 commit comments

Comments
 (0)