Skip to content

Commit 2ba2114

Browse files
committed
#Modification 22
1 parent 5655527 commit 2ba2114

183 files changed

Lines changed: 1308 additions & 5384 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

data_from_eclipse_ide/binaryTree/BT_Problem_01.java renamed to binaryTree/BT_Problem_01.java

File renamed without changes.

binaryTree/BT_Problem_02.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package binaryTree;
2+
import java.util.*;
3+
4+
// Find Reverse Level Order traversal
5+
6+
public class BT_Problem_02 {
7+
8+
Node root;
9+
10+
/* Given a binary tree, print its nodes in reverse level order */
11+
@SuppressWarnings({ "unchecked", "rawtypes" })
12+
void reverseLevelOrder(Node node) {
13+
14+
//Logic for O(n^2) Time Complexity approach
15+
// int h = height(node);
16+
// int i;
17+
// for(i = h; i >= 1; i--)
18+
// printGivenLevel(node, i);
19+
20+
Stack<Node> S = new Stack();
21+
Queue<Node> Q = new LinkedList();
22+
Q.add(node);
23+
24+
// Do something like normal level order traversal order.
25+
// Following are the differences with normal level order traversal:
26+
// 1. Instead of printing a node, we push the node to stack
27+
// 2. Right subtree is visited before left subtree.
28+
while(Q.isEmpty() == false) {
29+
30+
//Dequeue node and make it root
31+
node = Q.peek();
32+
Q.remove();
33+
S.push(node);
34+
35+
//Enqueue right child
36+
if(node.right != null)
37+
Q.add(node.right); //Node RIGHT CHILD IS ENQUEUED BEFORE LEFT
38+
39+
// Enqueue left child
40+
if(node.left != null)
41+
Q.add(node.left);
42+
}
43+
44+
// Now pop all items from stack one by one and print them
45+
while(S.empty() == false) {
46+
node = S.peek();
47+
System.out.print(node.data + " ");
48+
S.pop();
49+
}
50+
51+
}
52+
53+
// void printGivenLevel(Node node, int level) {
54+
// if(node == null)
55+
// return;
56+
// if(level == 1)
57+
// System.out.print(node.data + " ");
58+
// else if(level > 1) {
59+
// printGivenLevel(node.left, level - 1);
60+
// printGivenLevel(node.right, level - 1);
61+
// }
62+
// }
63+
//
64+
// int height(Node node) {
65+
// if(node == null)
66+
// return 0;
67+
// else {
68+
// int lheight = height(node.left);
69+
// int rheight = height(node.right);
70+
//
71+
// if(lheight > rheight)
72+
// return (lheight + 1);
73+
// else
74+
// return (rheight + 1);
75+
// }
76+
// }
77+
//
78+
public static void main(String[] args) {
79+
80+
BT_Problem_02 tree = new BT_Problem_02();
81+
82+
tree.root = new Node(1);
83+
tree.root.left = new Node(2);
84+
tree.root.right = new Node(3);
85+
tree.root.left.left = new Node(4);
86+
tree.root.left.right = new Node(5);
87+
88+
System.out.println("Level Order traversal of binary tree is : ");
89+
90+
tree.reverseLevelOrder(tree.root);
91+
}
92+
}

binaryTree/BT_Problem_03.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package binaryTree;
2+
3+
/* Problem Title :- Find the Height of a tree or Maximum Depth of a tree.
4+
*
5+
* Height of tree :-
6+
* The height of a tree is the number of edges on the longest downward path
7+
* between the root and a leaf.
8+
*/
9+
public class BT_Problem_03 {
10+
11+
Node root;
12+
13+
/*
14+
* Compute the "maxDepth" of a tree --
15+
* the number of nodes along the longest path from the root node
16+
* down to the farthest leaf node
17+
*/
18+
int maxDepth(Node node) {
19+
20+
if(node == null) return 0;
21+
22+
else {
23+
/* compute the depth of each subtree */
24+
int lDepth = maxDepth(node.left);
25+
int rDepth = maxDepth(node.right);
26+
27+
/* use the larger one*/
28+
if(lDepth > rDepth)
29+
return (lDepth + 1);
30+
else
31+
return (rDepth + 1);
32+
}
33+
}
34+
35+
/* Driver program to test above functions */
36+
public static void main(String[] args) {
37+
38+
BT_Problem_03 tree = new BT_Problem_03();
39+
40+
tree.root = new Node(1);
41+
tree.root.left = new Node(2);
42+
tree.root.right = new Node(3);
43+
tree.root.left.left = new Node(4);
44+
tree.root.left.right = new Node(5);
45+
46+
System.out.println("Height of tree is : " + tree.maxDepth(tree.root));
47+
48+
}
49+
50+
}

binaryTree/BT_Problem_04.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package binaryTree;
2+
/* Problem Title :- Find the Diameter of a tree or Width of tree.
3+
*
4+
* Diameter of a tree :-
5+
* The Diameter of a tree is the number of nodes on the longest path
6+
* between two end nodes.
7+
*/
8+
9+
// Class to print the Diameter.
10+
public class BT_Problem_04 {
11+
12+
Node root;
13+
14+
// Method to calculate the diameter and return it to main
15+
int diameter(Node root) {
16+
// base case if tree is empty
17+
if(root == null)
18+
return 0;
19+
20+
// get the height of left and right sub-trees
21+
int lheight = height(root.left);
22+
int rheight = height(root.right);
23+
24+
// get the diameter of left and right sub-trees
25+
int ldiameter = diameter(root.left);
26+
int rdiameter = diameter(root.right);
27+
28+
return Math.max(lheight + rheight + 1, Math.max(ldiameter, rdiameter));
29+
}
30+
31+
// A wrapper over diameter(Node root)
32+
int diameter() {return diameter(root);}
33+
34+
/*
35+
* The function Compute the "height" of a tree.
36+
* Height is the number of nodes along the longest path
37+
* from the root node to the farthest leaf node.
38+
*/
39+
static int height(Node node) {
40+
if(node == null)
41+
return 0;
42+
// If tree is not empty then height = 1 + max of left height and right heights.
43+
return (1 + Math.max(height(node.left), height(node.left)));
44+
}
45+
46+
// Driver Code
47+
public static void main(String[] args) {
48+
49+
BT_Problem_04 tree = new BT_Problem_04();
50+
51+
tree.root = new Node(1);
52+
tree.root.left = new Node(2);
53+
tree.root.right = new Node(3);
54+
tree.root.left.left = new Node(4);
55+
tree.root.left.right = new Node(5);
56+
57+
// Function Call
58+
System.out.println("The diameter of given bianry tree is : " + tree.diameter());
59+
}
60+
61+
}

binaryTree/BT_Problem_05.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package binaryTree;
2+
3+
/* Problem Title :- Find Mirror of a tree.
4+
*
5+
* Mirror of a tree :-
6+
* The Diameter of a tree is the number of nodes on the longest path
7+
* between two end nodes.
8+
*/
9+
public class BT_Problem_05 {
10+
11+
/*
12+
* A binary tree node has data,
13+
* pointer to left child
14+
* & a pointer to right child
15+
*/
16+
static class node{
17+
int val;
18+
node left;
19+
node right;
20+
}
21+
22+
/*
23+
* Helper function that allocates a new node with the given data
24+
* & null left and right pointers
25+
*/
26+
static node createNode(int val) {
27+
node newNode = new node();
28+
newNode.val = val;
29+
newNode.left = null;
30+
newNode.right = null;
31+
return newNode;
32+
}
33+
34+
/*
35+
* Helper function to print
36+
* In-order Traversal
37+
*/
38+
static void inorder(node root) {
39+
if(root == null)
40+
return;
41+
inorder(root.left);
42+
System.out.println(root.val);
43+
inorder(root.right);
44+
}
45+
46+
/*
47+
* mirror-i-f-y function takes two trees,
48+
* original tree and a mirror tree
49+
* It recurses on both the trees.
50+
* but when original tree recurses on left,
51+
* mirror tree recurses on right and vice-versa
52+
*/
53+
static node mirrorify(node root) {
54+
if(root == null)
55+
return null;
56+
57+
// Create new mirror node from original tree node
58+
node mirror = createNode(root.val);
59+
mirror.right = mirrorify(root.left);
60+
mirror.left = mirrorify(root.right);
61+
return mirror;
62+
}
63+
64+
// Driver Code
65+
public static void main(String[] args) {
66+
67+
node tree = createNode(5);
68+
tree.left = createNode(5);
69+
tree.right = createNode(5);
70+
tree.left.right = createNode(5);
71+
tree.left.left = createNode(5);
72+
73+
// print in-order traversal of the original input tree
74+
System.out.print("\n Inorderr of original tree: ");
75+
inorder(tree);
76+
node mirror = null;
77+
mirror = mirrorify(tree);
78+
79+
// print in-order traversal of the mirror tree
80+
System.out.print("\n Inorderr of mirror tree: ");
81+
inorder(mirror);
82+
}
83+
84+
}

binaryTree/BT_Problem_06_a.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package binaryTree;
2+
import java.util.*;
3+
/*
4+
* Problem Title :- In-order Traversal of a tree both using Recursion
5+
*/
6+
// Class to print the in-order traversal
7+
public class BT_Problem_06_a {
8+
9+
// Root of Binary Tree
10+
Node root;
11+
12+
/*
13+
* Given a binary tree,
14+
* print its nodes in in-order
15+
*/
16+
void inorder() {
17+
18+
if(root == null) return;
19+
20+
Stack<Node> s = new Stack<>();
21+
Node curr = root;
22+
23+
// traverse the tree
24+
while(curr != null || s.size() > 0) {
25+
/* Reach the left most Node of the current Node */
26+
while(curr != null) {
27+
/* place pointer to a tree node on
28+
* the stack before traversing
29+
* the node,s left subtree */
30+
s.push(curr);
31+
curr = curr.left;
32+
}
33+
34+
/*Current must be NULL at this point */
35+
curr = s.pop();
36+
37+
System.out.print(curr.data + " ");
38+
39+
/* we have visited the node and its left subtree.
40+
* Now, its right subtree's turn */
41+
curr = curr.right;
42+
}
43+
}
44+
45+
// Driver method
46+
public static void main(String[] args) {
47+
48+
// creating a binary tree and entering the nodes
49+
BT_Problem_06_a tree = new BT_Problem_06_a();
50+
51+
tree.root = new Node(1);
52+
tree.root.left = new Node(2);
53+
tree.root.right = new Node(3);
54+
tree.root.left.left = new Node(4);
55+
tree.root.left.right = new Node(5);
56+
57+
tree.inorder();
58+
59+
}
60+
61+
}
62+

0 commit comments

Comments
 (0)