Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
110 changes: 110 additions & 0 deletions BinaryTrees/heightofTree.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
42 changes: 42 additions & 0 deletions BinaryTrees/kthLevel.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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);
}
}
118 changes: 118 additions & 0 deletions BinaryTrees/preorder.java
Original file line number Diff line number Diff line change
@@ -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<Node> 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);
}
}
39 changes: 39 additions & 0 deletions BinaryTrees/preorderBuild.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading