Showing posts with label sum of diagonal. Show all posts
Showing posts with label sum of diagonal. Show all posts

Saturday, 26 January 2019

Print Diagonal of Binary tree and their sum

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

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...