@@ -91,7 +91,23 @@ int countNodes(struct node * root){
91
91
if (root==NULL )
92
92
return 0 ;
93
93
else
94
- return 1 + count (root->left ) + count (root->right );
94
+ return 1 + countNodes (root->left ) + countNodes (root->right );
95
+ }
96
+
97
+
98
+ // This function calclulates the height of the binary search tree.
99
+ int height (struct node *root){
100
+ if (root==NULL )
101
+ return 0 ;
102
+ return 1 + max (height (root->left ),height (root->right ));
103
+ }
104
+
105
+
106
+ // This function calculates the diameter of the binary search tree.
107
+ int diameter (struct node *root){
108
+ if (root == NULL )
109
+ return 0 ;
110
+ return max (1 + height (root->left ) + height (root->right ), max (diameter (root->left ),diameter (root->right )));
95
111
}
96
112
97
113
int main () {
@@ -108,15 +124,23 @@ int main() {
108
124
create (bst, 12 );
109
125
create (bst, 21 );
110
126
// Inorder permutate from left -> root -> right
127
+ cout << " The Inorder traversal of the tree is : " ;
111
128
inorder (bst);
112
129
cout << endl;
113
130
// Postorder permutate from left -> right -> root
131
+ cout << " The Postorder traversal of the tree is : " ;
114
132
postorder (bst);
115
133
cout << endl;
116
134
// Preorder permutate from root -> left -> right
135
+ cout << " The Preorder traversal of the tree is : " ;
117
136
preorder (bst);
118
137
cout << endl;
119
138
// Count total number of nodes in the tree
120
- cout << countNodes (root);
139
+ cout << " The number of nodes in the tree : " ;
140
+ cout << countNodes (bst) << endl;
141
+ cout << " The height of the tree is : " ;
142
+ cout << height (bst) << endl;
143
+ cout << " The diameter of the tree is : " ;
144
+ cout << diameter (bst) << endl;
121
145
return 0 ;
122
146
}
0 commit comments