-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementation.java
More file actions
176 lines (166 loc) · 4.56 KB
/
Implementation.java
File metadata and controls
176 lines (166 loc) · 4.56 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.util.LinkedList;
import java.util.Queue;
public class Implementation {
public int size(Node root){
if(root==null){
return 0;
}
return 1+size(root.left)+size(root.right);
}
public int maxValue(Node root){
if(root==null){
return Integer.MIN_VALUE;
}
return Math.max(root.val, Math.max(maxValue(root.left), maxValue(root.right)));
}
public int MinValue(Node root){
if(root==null)
{
return Integer.MAX_VALUE;
}
return Math.min(root.val, Math.min(MinValue(root.left), MinValue(root.right)));
}
public int totalSum(Node root){
if(root==null){
return 0;
}
return root.val+totalSum(root.left)+totalSum(root.right);
}
public int height(Node root){
if(root==null){
return 0;
}
if(root.left==null && root.right==null){
return 0;
}
int left=1+height(root.left);
int right=1+height(root.right);
return Math.max(left,right);
}
public int productOfAllNodes(Node root){
if(root==null){
return 1;
}
return root.val*productOfAllNodes(root.left)*productOfAllNodes(root.right);
}
public void printTreeUsingQueue(Node root)
{
if(root==null)
{
return ;
}
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
Node curNode=queue.poll();
System.out.print(curNode.val+" ");
if(curNode.left!=null)
{
queue.add(curNode.left);
}
if(curNode.right!=null)
{
queue.add(curNode.right);
}
}
}
public void printNode(Node root)
{
if(root==null)
{
return ;
}
System.out.print(root.val);
System.out.print("-> ");
if(root.left!=null)
{
System.out.print(root.left.val);
}
else
{
System.out.print("null");
}
System.out.print(", ");
if(root.right!=null)
{
System.out.print(root.right.val);
}
else
{
System.out.print("null");
}
System.out.println();
printNode(root.left);
printNode(root.right);
}
public void inOrderTraversal(Node root){
if(root==null){
return ;
}
inOrderTraversal(root.left);
System.out.print(root.val+" ");
inOrderTraversal(root.right);
}
public void preOrderTraversal(Node root){
if(root==null){
return ;
}
System.out.print(root.val+" ");
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
public void postOrderTraversal(Node root){
if(root==null){
return ;
}
postOrderTraversal(root.left);
postOrderTraversal(root.right);
System.out.print(root.val+" ");
}
public void printElement(Node root,int level,int levelToPrint){
if(root==null){
return ;
}
if(level==levelToPrint){
System.out.print(root.val+" ");
return ;
}
printElement(root.left,level+1,levelToPrint);
printElement(root.right,level+1,levelToPrint);
}
public static void main(String [] args){
Node a =new Node(1);
Node b =new Node(2);
Node c =new Node(3);
a.left = b;
a.right = c;
Node d=new Node(4);
Node e=new Node(5);
b.left=d;
b.right=e;
Node f=new Node(6);
c.right=f;
Implementation impl = new Implementation();
impl.printNode(a);
System.out.println("Size of tree: "+impl.size(a));
System.out.println("Max value in tree: "+impl.maxValue(a));
System.out.println("Min value in tree: "+impl.MinValue(a));
System.out.println("Total Sum of all the tree is "+impl.totalSum(a));
System.out.println("Height of tree is : "+impl.height(a));
System.out.println("Product of all nodes in tree is : "+impl.productOfAllNodes(a));
System.out.print("Inorder Traversal: ");
impl.inOrderTraversal(a);
System.out.println();
System.out.print("Preorder Traversal: ");
impl.preOrderTraversal(a);
System.out.println();
System.out.print("Postorder Traversal: ");
impl.postOrderTraversal(a);
System.out.println();
System.out.print("Print elements at level 1: ");
impl.printElement(a, 0, 1);
System.out.println();
System.out.print("Print tree using queue: ");
impl.printTreeUsingQueue(a);
}
}