Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7be0e8

Browse files
committed
hhh
1 parent c5dc6bf commit e7be0e8

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package DataAndAlgoL.Chpt6TreeAndBinaryTrees;
2+
//Problem-1 Give an algorithm for finding maximum element in binary tree.
3+
4+
public class FindMaxTreeNode {
5+
class TreeNode {
6+
int val;
7+
TreeNode left;
8+
TreeNode right;
9+
TreeNode() {}
10+
TreeNode(int val) { this.val = val; }
11+
TreeNode(int val, TreeNode left, TreeNode right) {
12+
this.val = val;
13+
this.left = left;
14+
this.right = right;
15+
}
16+
}
17+
public static void main(String[] args) {
18+
19+
}
20+
21+
public static int maxBinaryTreeNode(TreeNode root){
22+
int maxVal= Integer.MIN_VALUE;
23+
if(root != null){
24+
int rightMax= maxBinaryTreeNode(root.right);
25+
int leftMax= maxBinaryTreeNode(root.left);
26+
27+
if(leftMax > rightMax){
28+
maxVal=leftMax;
29+
}else{
30+
maxVal=rightMax;
31+
}
32+
33+
if(root.val > maxVal){
34+
maxVal=root.val;
35+
}
36+
}
37+
38+
return maxVal;
39+
}
40+
41+
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package DataAndAlgoL.Chpt6TreeAndBinaryTrees;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class FindMaxTreeNodeBFS {
7+
8+
class TreeNode {
9+
int val;
10+
TreeNode left;
11+
TreeNode right;
12+
TreeNode() {}
13+
TreeNode(int val) { this.val = val; }
14+
TreeNode(int val, TreeNode left, TreeNode right) {
15+
this.val = val;
16+
this.left = left;
17+
this.right = right;
18+
}
19+
}
20+
21+
public static void main(String[] args) {
22+
23+
}
24+
25+
public int maxBinaryTreeBFS(TreeNode root){
26+
if(root == null){
27+
return Integer.MIN_VALUE;
28+
}
29+
int max= Integer.MAX_VALUE;
30+
31+
Queue<TreeNode> q= new LinkedList<>(); //Create queue to have Treenodes
32+
q.offer(root); //queue starts with node inside
33+
while(!q.isEmpty()){
34+
TreeNode temp= q.poll(); // gets 1st node from queue to explore its descendents
35+
if(temp.val > max){ // check all nodes to see who has highest val until both right and left nodes next pointer points to null
36+
max= temp.val;
37+
}
38+
// BFS IS LEVEL ORDER LEFT TO RIGHT SO LEFT IS FIRST THEN RIGHT BY QUEUE RULES (FIFO)
39+
if(temp != null){
40+
if(temp.left != null){ //push left tree node to queue to explore its descendent nodes
41+
q.offer(temp.left);
42+
}
43+
if(temp.right != null){ //push right tree node to queue to explore its descendent nodes
44+
q.offer(temp.right);
45+
}
46+
}
47+
}
48+
49+
return max;
50+
}
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package DataAndAlgoL.Chpt6TreeAndBinaryTrees;
2+
3+
public class SearchinElementBinaryTree {
4+
class TreeNode {
5+
int val;
6+
TreeNode left;
7+
TreeNode right;
8+
TreeNode() {}
9+
TreeNode(int val) { this.val = val; }
10+
TreeNode(int val, TreeNode left, TreeNode right) {
11+
this.val = val;
12+
this.left = left;
13+
this.right = right;
14+
}
15+
}
16+
17+
public static void main(String[] args) {
18+
19+
}
20+
//SEARCHING USING RECURSION
21+
public static boolean findElementTreeNode(TreeNode root, int data){
22+
if(root == null){
23+
return false;
24+
}
25+
26+
if(root.val == data){
27+
return true;
28+
}
29+
30+
return findElementTreeNode(root.right, data) || findElementTreeNode(root.left, data);
31+
}
32+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package DataAndAlgoL.Chpt6TreeAndBinaryTrees;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class SearchingElementBTBFS {
7+
class TreeNode {
8+
int val;
9+
TreeNode left;
10+
TreeNode right;
11+
TreeNode() {}
12+
TreeNode(int val) { this.val = val; }
13+
TreeNode(int val, TreeNode left, TreeNode right) {
14+
this.val = val;
15+
this.left = left;
16+
this.right = right;
17+
}
18+
19+
public static void main(String[] args) {
20+
21+
}
22+
23+
public static boolean SearchinElementInBTBFS(TreeNode root, int data){
24+
if(root ==null){
25+
return false;
26+
}
27+
28+
Queue<TreeNode> q= new LinkedList<>();
29+
q.offer(root);
30+
31+
while(root !=null){
32+
TreeNode temp= q.poll();
33+
if(temp.val == data){
34+
return true;
35+
}
36+
37+
if(temp != null){
38+
if(temp.val > data){
39+
q.offer(temp.right);
40+
}
41+
42+
if(temp.val < data){
43+
q.offer(temp.left);
44+
}
45+
}
46+
}
47+
48+
return false;
49+
50+
}
51+
}
52+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /