From 4b1cabfd61603ddc1ee4e410e9e28db63ce459b5 Mon Sep 17 00:00:00 2001 From: Atharv Joundal <158443883+Atharv2433@users.noreply.github.com> Date: Sat, 28 Sep 2024 22:48:38 +0530 Subject: [PATCH] Add files via upload --- Node_3.java | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Node_3.java diff --git a/Node_3.java b/Node_3.java new file mode 100644 index 0000000..68651fa --- /dev/null +++ b/Node_3.java @@ -0,0 +1,66 @@ +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Queue; + +public class Node_3{ + int data; + Node_3 left, right; + + Node_2(int val) { + data = val; + left = right = null; + } + public static class Info{ + Node_3 node; + int hd; + + Info(int hd,Node_3 data){ + hd = hd; + node = data; + } + } + public static void top_view(Node_3 root){ + if(root == null){ + return; + } + Queue q = new LinkedList<>(); + HashMap mpp = new HashMap<>(); + + q.add(new Indo(0,root)); + q.add(null); + + while(!q.isEmpty()){ + Info curr = q.remove(); + if(curr == null){ + System.out.println(); + if(q.isEmpty()){ + break; + }else{ + q.add(null); + } + } + if(!mpp.containskey(curr.hd)){ + mpp.put(curr.hd,curr.data); + } + if(curr.left != null){ + q.add(curr.hd-1,curr.left); + } + if(curr.right != null){ + q.add(curr.hd+1,curr.right); + } + } + } + public static void main(String[] args) { + Node_2 root = new Node_2(1); + root.left = new Node_2(2); + root.right = new Node_2(3); + root.left.left = new Node_2(4); + root.left.right = new Node_2(5); + + Node_2 subroot = new Node_2(2); + subroot.left = new Node_2(4); + subroot.right = new Node_2(5); + + System.out.println(is_subtree(root, subroot)); + } +}