diff --git a/.DS_Store b/.DS_Store index e8fab94..f1e630b 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/BinaryTrees/heightofTree.java b/BinaryTrees/heightofTree.java new file mode 100644 index 0000000..1eb8c79 --- /dev/null +++ b/BinaryTrees/heightofTree.java @@ -0,0 +1,110 @@ +package BinaryTrees; + +import Stacks.reverseAStack; + +public class heightofTree { + public static class Node{ + int data; + Node left; + Node right; + + public Node(int data) + { + this.data = data; + left=null; + right=null; + } + } + public static int height(Node root) //max depth/height of tree + { + if(root==null) + { + return 0; + } + int leftHeight = height(root.left); + int righHeight = height(root.right); + return Math.max(leftHeight, righHeight)+1; + } + public static int count(Node root) //count of nodes + { + if(root==null) + { + return 0; + } + int leftC = count(root.left); + int righC = count(root.right); + return (leftC + righC)+1; + } + public static int sum(Node root) //sum of nodes - recursive + { + if(root==null) + { + return 0; + } + int leftS = sum(root.left); + int righS = sum(root.right); + return (leftS + righS)+root.data; + } + public static int maxDiam(Node root) + { + if(root == null) + { + return 0; + } + int leftDiam = maxDiam(root.left); + int leftHeight = height(root.left); + int rightDiam = maxDiam(root.right); + int righHeight = height(root.right); + + int selfDiam = leftHeight+righHeight+1; + + return Math.max(leftDiam, Math.max(rightDiam, selfDiam)); + } + + public static class Info{ + int daim; + int ht; + + public Info(int daim, int ht) + { + this.daim = daim; + this.ht = ht; + } + } + public static Info diamter(Node root) + { + if(root == null) + { + return new Info(0, 0); + } + Info left = diamter(root.left); + Info right = diamter(root.right); + + int maxDi = Math.max(left.daim, Math.max(right.daim, left.ht+right.ht+1)); + int maxH = Math.max(left.ht, right.ht)+1; + + return new Info(maxDi, maxH); + + } + public static void main(String[] args) { + Node root = new Node(1); + root.left = new Node (2) ; + root.right = new Node (3) ; + root. left. left = new Node (4) ; + root.left.right = new Node (5) ; + root.right. left = new Node (6) ; + root.right.right = new Node(7) ; + int h = height(root); + System.out.println(h); + int c = count(root); + System.out.println(c); + int s = sum(root); + System.out.println(s); + int d = maxDiam(root); + System.out.println(d); + int di = diamter(root).daim; + System.out.println(di); + int hi = diamter(root).ht; + System.out.println(hi); + } +} diff --git a/BinaryTrees/kthLevel.java b/BinaryTrees/kthLevel.java new file mode 100644 index 0000000..7d614ae --- /dev/null +++ b/BinaryTrees/kthLevel.java @@ -0,0 +1,42 @@ +package BinaryTrees; +import java.util.*; +public class kthLevel { + public static class Node{ + int data; + Node left; + Node right; + + public Node(int data) + { + this.data = data; + left=null; + right=null; + } + } + static List arr = new ArrayList<>(); + public static void kThLevel(Node root, int level, int target) + { + if(root == null) + { + return; + } + if(level == target) + { + arr.add(root.data); + return; + } + kThLevel(root.left, level+1, target); + kThLevel(root.right, level+1, target); + } + public static void main(String[] args) { + Node root = new Node(1); + root.left = new Node (2) ; + root.right = new Node (3) ; + root. left. left = new Node (4) ; + root.left.right = new Node (5) ; + root.right. left = new Node (6) ; + root.right.right = new Node(7) ; + kThLevel(root, 0, 2); + System.out.println(arr); + } +} diff --git a/BinaryTrees/preorder.java b/BinaryTrees/preorder.java new file mode 100644 index 0000000..2925b2d --- /dev/null +++ b/BinaryTrees/preorder.java @@ -0,0 +1,118 @@ +package BinaryTrees; + +import java.util.*; + +public class preorder { + public static class Node{ + int data; + Node left; + Node right; + + public Node(int data) + { + this.data = data; + left=null; + right=null; + } + } + public static class binaryTree{ + static int idx = -1; + public static Node preorder(int[] nodes) + { + idx++; + if(nodes[idx]==-1) + { + return null; + } + Node newNode = new Node(nodes[idx]); + newNode.left = preorder(nodes); + newNode.right = preorder(nodes); + return newNode; + } + public static void pre(Node root) // PREORDER o(n) + { + if(root == null) + { + //System.out.println("-1"); //if I do this then I get the answer in the form of the input provided + return; + } + System.out.print(root.data+" "); //root + pre(root.left); //left + pre(root.right); //right + } + public static void in(Node root) //INORDER + { + if(root==null) + { + //System.out.print("-1 "); + return; + } + in(root.left); //left + System.out.print(root.data+" "); //root + in(root.right); //right + } + public static void post(Node root) //POSTORDER + { + if(root==null) + { + // System.out.print("-1 "); + return; + } + post(root.left); //left + post(root.right); //right + System.out.print(root.data+" "); //root + } + public static void level(Node root) + { + if(root == null) //the tree is empty + { + return; + } + + Queue qu = new LinkedList<>(); + qu.add(root); //adding the root element + qu.add(null); //adding the first line break after level 1 + + while(!qu.isEmpty()) + { + Node currNode = qu.remove(); + if(currNode == null) + { + System.out.println(); + if(qu.isEmpty()) + { + break; + } + else + { + qu.add(null); + } + } + else + { + System.out.print(currNode.data+" "); + if(currNode.left != null) + { + qu.add(currNode.left); + } + if(currNode.right != null) + { + qu.add(currNode.right); + } + } + } + } + } + public static void main(String[] args) { + int[] nodes = {1,2,4,-1,-1,5,-1,-1,3,-1,6,-1,-1}; + binaryTree bt = new binaryTree(); + Node root = bt.preorder(nodes); + bt.pre(root); + System.out.println(); + bt.in(root); + System.out.println(); + bt.post(root); + System.out.println(); + bt.level(root); + } +} diff --git a/BinaryTrees/preorderBuild.java b/BinaryTrees/preorderBuild.java new file mode 100644 index 0000000..3c9e39e --- /dev/null +++ b/BinaryTrees/preorderBuild.java @@ -0,0 +1,39 @@ +package BinaryTrees; +import java.util.*; + +public class preorderBuild { //o(n) + public static class Node{ + int data; + Node left; + Node right; + + public Node(int data) + { + this.data = data; + left=null; + right=null; + } + } + + public static class binaryTree{ + static int idx = -1; + public static Node preorder(int[] nodes) + { + idx++; + if(nodes[idx]==-1) + { + return null; + } + Node newNode = new Node(nodes[idx]); + newNode.left = preorder(nodes); + newNode.right = preorder(nodes); + return newNode; + } + } + public static void main(String[] args) { + int[] nodes = {1,2,4,-1,-1,5,-1,-1,3,-1,6,-1,-1}; + binaryTree bt = new binaryTree(); + Node root = bt.preorder(nodes); + System.out.println(root.data); + } +} diff --git a/BinaryTrees/topView.java b/BinaryTrees/topView.java new file mode 100644 index 0000000..33a6d2b --- /dev/null +++ b/BinaryTrees/topView.java @@ -0,0 +1,107 @@ +package BinaryTrees; +import java.util.*; +public class topView { + public static class Node{ + int data; + Node left; + Node right; + + public Node(int data) + { + this.data = data; + left=null; + right=null; + } + } + + public static class Info //bundling node and horizontal distance together + { + Node node; + int horiD; + + public Info(Node node, int horiD) + { + this.node = node; + this.horiD = horiD; + } + } + public static void top(Node root) + { + Queue q = new LinkedList<>(); + HashMap hs = new HashMap<>(); + + int min = Integer.MAX_VALUE; + int max = Integer.MIN_VALUE; + q.add(new Info(root, 0)); //adding the root node and its horizontal Distance as root and 0 + q.add(null); + + while(!q.isEmpty()) + { + Info curr = q.remove(); + if(curr == null) //if current is null + { + if(q.isEmpty()) //if after that the queue becomes null means we have completed all our steps + { + break; + } + else // else add back the null + { + q.add(null); + } + } + else //if current is not null + { + if(!hs.containsKey(curr.horiD)) //check if the hashmap already has the horiDistance of the current node - if not then proceed inside + { + hs.put(curr.horiD, curr.node); //add the horizontal distance and the current node + } + if(curr.node.left != null) // if the value on the left of the node is not null + { + q.add(new Info(curr.node.left, curr.horiD-1)); // add the node and its distance -1 (packaged in Info) to the queue + min = Math.min(curr.horiD-1, min); //check for minimum as it wont affect the max as going left is always -1 + } + if(curr.node.right != null) + { + q.add(new Info(curr.node.right, curr.horiD+1)); //same as above: just opposite + max = Math.max(curr.horiD+1, max); + } + } + } + System.out.println("TOP VIEW"); + for(int i= min; i<=max; i++) //go from min to max and print the order in which we will see the tree from the top + { + System.out.print(hs.get(i).data+" "); //hs.get(i) gives node hence hs.get(i).data gives value + } + System.out.println(); + System.out.println("RIGHT VIEW"); // all positive hori distance + for(int i= 0; i<=max; i++) //go from 0 to max and print the order in which we will see the tree from the right + { + System.out.print(hs.get(i).data+" "); //hs.get(i) gives node hence hs.get(i).data gives value + } + System.out.println(); + System.out.println("LEFT VIEW"); //all negative hori distance + for(int i= 0; i>=min; i--) //go from 0 to min and print the order in which we will see the tree from the left + { + System.out.print(hs.get(i).data+" "); //hs.get(i) gives node hence hs.get(i).data gives value + } + System.out.println(); + System.out.println("BOTTOM VIEW"); // reverse of Top View + for(int i= max; i>=min; i--) //go from max to min and print the order in which we will see the tree from the bottom + { + System.out.print(hs.get(i).data+" "); //hs.get(i) gives node hence hs.get(i).data gives value + } + System.out.println(); + + } + public static void main(String[] args) { + Node root = new Node(1); + root.left = new Node (2) ; + root.right = new Node (3) ; + root. left. left = new Node (4) ; + root.left.right = new Node (5) ; + root.right. left = new Node (6) ; + root.right.right = new Node(7) ; + + top(root); + } +}