-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasics.java
More file actions
89 lines (79 loc) · 2.26 KB
/
Basics.java
File metadata and controls
89 lines (79 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package Trees;
/**
* A tree is one of the data structures that represents hierarchical data.
* Every tree has a node, it has a left subtree and a right subtree.
* 1) Leaf Node: No children
* 2) Internal Node: !leaf and !root
* 3) Ancestor Node: Parent and Parent's parent
* 4) Descendant Node: Child and Child's child
* 5) Size: No of nodes in the tree
* 6) Edges: Size - 1
* 7) Level: The depth of the tree is known as level.
* 8) Height: Level - 1
*/
public class Basics {
/*
* Function to find the height of a binary tree.
* Check the base case of a leaf node.
*/
public static int height(Node root) {
if (root == null)
return 0;
if (root.left == null && root.right == null)
return 0;
return 1 + Math.max(height(root.left), height(root.right));
}
/*
* Function to find the size of a binary tree.
*/
public static int size(Node root) {
if (root == null)
return 0;
return 1 + size(root.left) + size(root.right);
}
/*
* Sum of the value of all the nodes of a binary tree.
*/
public static int sum(Node root) {
if (root == null)
return 0;
return root.val + sum(root.left) + sum(root.right);
}
/*
* Product of the value of all the nodes of a binary tree.
*/
public static int product(Node root) {
if (root == null)
return 1;
return root.val * sum(root.left) * sum(root.right);
}
/*
* Function to find the max value of a binary tree.
*/
public static int max(Node root) {
if (root == null)
return Integer.MIN_VALUE;
return Math.max(root.val, Math.max(max(root.left), max(root.right)));
}
public static void main(String[] args) {
// Creating a Binary Tree .
Node root = new Node(10);
Node a = new Node(20);
Node b = new Node(30);
root.left = a;
root.right = b;
Node c = new Node(40);
Node d = new Node(50);
a.left = c;
a.right = d;
Node e = new Node(60);
Node f = new Node(70);
b.right = e;
e.right = f;
System.out.println(max(root));
System.out.println(size(root));
System.out.println(sum(root));
System.out.println(height(root));
System.out.println(product(root));
}
}