package tree;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
public class PrintDiagonalBinaryTree {
public static void main(String[] args) {
int[] arr = {10,5,4,7,15,14,17};
Node root = null;
for(int val:arr) {
root = insertNode(root, val);
}
printInorder(root);
System.out.println("\n*****print diagonl****\n");
printDiagnol(root);
}
static void printInorder(Node root) {
if(null!=root) {
printInorder(root.left);
System.out.println(root.value);
printInorder(root.right);
}else {
return;
}
}
static void printDiagnol(Node root) {
Map<Integer, Integer> map = new HashMap<>();
Queue<Node> queue = new LinkedList<>();
queue.add(root);
queue.add(null);
Node p=null;
int count = 0;
while(!queue.isEmpty()) {
p = queue.remove();
if(p==null) {
count = count+1;
queue.add(null);
p = queue.remove();
if(null==p) {
break;
}
}
System.out.println("diagnol::"+count);
while(null!=p) {
System.out.println(p.value);
if(map.containsKey(count)) {
map.put(count, map.get(count)+p.value);
}else {
map.put(count, p.value);
}
if(null!=p.left) {
queue.add(p.left);
}
p = p.right;
}
}
System.out.println("\n***print sum of each diagonal***\n");
map.forEach((k,v)-> {
System.out.println("diagonal ::"+k+" sum is::"+v);
});
}
static Node insertNode(Node root, Integer value) {
Node newNode = new Node(value);
if (root == null) {
return newNode;
} else {
if (value <= root.value) {
root.left = insertNode(root.left, value);
} else {
root.right = insertNode(root.right, value);
}
return root;
}
}
static class Node{
Integer value;
Node left;
Node right;
Node(Integer value){
this.value = value;
left = right = null;
}
}
}
output:
**origianl**
4
5
7
10
14
15
17
*****print diagonl****
diagnol::0
10
15
17
diagnol::1
5
7
diagnol::1
14
diagnol::2
4
***print sum of each diagonal***
diagonal ::0 sum is::42
diagonal ::1 sum is::26
diagonal ::2 sum is::4
Note: the reason behind adding null in queue to trigger end of a diagonal.
main logic: find diagonal using distance start from root d=0 and d=parent+1(for left sub tree) and d= parent(for right sub tree).
Ref: https://www.youtube.com/watch?v=e9ZGxH1y_PE
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
public class PrintDiagonalBinaryTree {
public static void main(String[] args) {
int[] arr = {10,5,4,7,15,14,17};
Node root = null;
for(int val:arr) {
root = insertNode(root, val);
}
printInorder(root);
System.out.println("\n*****print diagonl****\n");
printDiagnol(root);
}
static void printInorder(Node root) {
if(null!=root) {
printInorder(root.left);
System.out.println(root.value);
printInorder(root.right);
}else {
return;
}
}
static void printDiagnol(Node root) {
Map<Integer, Integer> map = new HashMap<>();
Queue<Node> queue = new LinkedList<>();
queue.add(root);
queue.add(null);
Node p=null;
int count = 0;
while(!queue.isEmpty()) {
p = queue.remove();
if(p==null) {
count = count+1;
queue.add(null);
p = queue.remove();
if(null==p) {
break;
}
}
System.out.println("diagnol::"+count);
while(null!=p) {
System.out.println(p.value);
if(map.containsKey(count)) {
map.put(count, map.get(count)+p.value);
}else {
map.put(count, p.value);
}
if(null!=p.left) {
queue.add(p.left);
}
p = p.right;
}
}
System.out.println("\n***print sum of each diagonal***\n");
map.forEach((k,v)-> {
System.out.println("diagonal ::"+k+" sum is::"+v);
});
}
static Node insertNode(Node root, Integer value) {
Node newNode = new Node(value);
if (root == null) {
return newNode;
} else {
if (value <= root.value) {
root.left = insertNode(root.left, value);
} else {
root.right = insertNode(root.right, value);
}
return root;
}
}
static class Node{
Integer value;
Node left;
Node right;
Node(Integer value){
this.value = value;
left = right = null;
}
}
}
output:
**origianl**
4
5
7
10
14
15
17
*****print diagonl****
diagnol::0
10
15
17
diagnol::1
5
7
diagnol::1
14
diagnol::2
4
***print sum of each diagonal***
diagonal ::0 sum is::42
diagonal ::1 sum is::26
diagonal ::2 sum is::4
Note: the reason behind adding null in queue to trigger end of a diagonal.
main logic: find diagonal using distance start from root d=0 and d=parent+1(for left sub tree) and d= parent(for right sub tree).
Ref: https://www.youtube.com/watch?v=e9ZGxH1y_PE
No comments:
Post a Comment