-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathsizeOfBinaryTree.java
112 lines (88 loc) Β· 2.59 KB
/
sizeOfBinaryTree.java
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
package Lecture20;
import java.util.Scanner;
public class sizeOfBinaryTree {
private class Node {
int data;
Node left;
Node right;
Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
private Node root;
private int size = 0;
public sizeOfBinaryTree() {
Scanner sc = new Scanner(System.in);
this.root = takeTreeInput(sc, null, false);
}
private Node takeTreeInput(Scanner sc, Node parent, boolean isLeftorRight) {
if (parent == null) {
System.out.print("Enter data for root node: ");
} else {
if (isLeftorRight) {
System.out.print("Enter data for left child of " + parent.data + ": ");
} else {
System.out.print("Enter data for right child of: " + parent.data + ": ");
}
}
int data = sc.nextInt();
Node node = new Node(data, null, null);
this.size++;
boolean choice = false;
System.out.print("Do you have left child for " + node.data + " -> true/false: ");
choice = sc.nextBoolean();
if (choice) {
node.left = this.takeTreeInput(sc, node, true);
}
choice = false;
System.out.print("Do you have right child for " + node.data + " -> true/false: ");
choice = sc.nextBoolean();
if (choice) {
node.right = this.takeTreeInput(sc, node, false);
}
return node;
}
public int size() {
return this.size(this.root);
}
private int size(Node node) {
if (node == null){
return 0;
}
int lsize = size(node.left);
int rsize = size(node.right);
int ans = lsize + rsize + 1;
return ans;
}
public static void main(String[] args) {
sizeOfBinaryTree tree = new sizeOfBinaryTree();
int treeSize = tree.size();
System.out.println("=============\n");
System.out.println("size of tree is: " + treeSize);
}
}
/* output:
Enter data for root node: 20
Do you have left child for 20 -> true/false: true
Enter data for left child of 20: 10
Do you have left child for 10 -> true/false: true
Enter data for left child of 10: 28
Do you have left child for 28 -> true/false: false
Do you have right child for 28 -> true/false: false
Do you have right child for 10 -> true/false: true
Enter data for right child of: 10: 16
Do you have left child for 16 -> true/false: false
Do you have right child for 16 -> true/false: false
Do you have right child for 20 -> true/false: true
Enter data for right child of: 20: 30
Do you have left child for 30 -> true/false:
true
Enter data for left child of 30: 42
Do you have left child for 42 -> true/false: false
Do you have right child for 42 -> true/false: false
Do you have right child for 30 -> true/false: false
=============
size of tree is: 6
*/