@@ -11,7 +11,24 @@ public static void main(String[] args) {
1111
1212 // tree.inorder();
1313 // tree.preorder();
14- tree .postorder ();
14+ // tree.postorder();
15+
16+ // int count = tree.countNode(BinaryTree.root);
17+ // System.out.println(count);
18+
19+ int sumOfNodes = tree .sumOfNodes (BinaryTree .root );
20+ System .out .print ("Sum of the nodes: " + sumOfNodes );
21+
22+ System .out .println ();
23+
24+ int heightOfTheTree = tree .heightOfTheTree (BinaryTree .root );
25+ System .out .println ("Height of the Tree is : " + heightOfTheTree );
26+
27+ System .out .println ();
28+
29+ int diameter = tree .CalculateDiameter (BinaryTree .root );
30+ System .out .println ("Diameter of the tree is : " + diameter );
31+
1532 System .out .println ();
1633
1734 }
@@ -27,8 +44,9 @@ public Node(int data) {
2744 }
2845 }
2946
30- public static class BinaryTree {
47+ public static class BinaryTree {
3148 static Node root ;
49+ static int AnsOfDiamiter = 0 ;
3250
3351 public static void insertData (int data ) {
3452 root = insertRec (root , data );
@@ -61,32 +79,90 @@ public static void inorderRec(Node root) {
6179
6280 }
6381
64- //pre order
65- public static void preorder (){
82+ // pre order
83+ public static void preorder () {
6684 preorderRec (root );
6785 }
68- public static void preorderRec (Node root ){
69- if (root == null ){
86+
87+ public static void preorderRec (Node root ) {
88+ if (root == null ) {
7089 return ;
7190 }
7291 System .out .print (root .data + " " );
7392 preorderRec (root .left );
7493 preorderRec (root .right );
7594 }
7695
77- //post order
78- public static void postorder (){
96+ // post order
97+ public static void postorder () {
7998 postorderRec (root );
8099 }
81100
82- public static void postorderRec (Node root ){
83- if (root == null ){
101+ public static void postorderRec (Node root ) {
102+ if (root == null ) {
84103 return ;
85104 }
86105 postorderRec (root .left );
87106 postorderRec (root .right );
88107 System .out .println (root .data + " " );
89108
90109 }
110+
111+ // count the nodes
112+ public static int countNode (Node root ) {
113+ if (root == null ) {
114+ return 0 ;
115+ }
116+
117+ int left = countNode (root .left );
118+ int right = countNode (root .right );
119+
120+ return 1 + left + right ;
121+ }
122+
123+ // sun of nodes
124+
125+ public static int sumOfNodes (Node root ) {
126+ if (root == null ) {
127+ return 0 ;
128+ }
129+ int left = sumOfNodes (root .left );
130+ int right = sumOfNodes (root .right );
131+
132+ return left + right + root .data ;
133+ }
134+
135+ // height of the tree
136+
137+ public static int heightOfTheTree (Node root ) {
138+ if (root == null ) {
139+ return 0 ;
140+ }
141+ int left = heightOfTheTree (root .left );
142+ int right = heightOfTheTree (root .right );
143+
144+ return Math .max (left , right ) + 1 ;
145+ }
146+
147+ // diameter of a binary tree
148+ public static int CalculateDiameter (Node root ){
149+ diamiter (root );
150+ return AnsOfDiamiter ;
151+ }
152+ public static int diamiter (Node root ){
153+
154+ if (root == null ){
155+ return 0 ;
156+ }
157+
158+ int diam1 = diamiter (root .left );
159+ int diam2 = diamiter (root .right );
160+
161+ int finaldiam = diam1 + diam2 + 1 ;
162+ AnsOfDiamiter = Math .max (AnsOfDiamiter , finaldiam );
163+ return 1 + Math .max (diam1 , diam2 );
164+
165+ }
166+
91167 }
92168}
0 commit comments