|
| 1 | +#include <stdio.h> |
| 2 | +#include <conio.h> |
| 3 | +#include <malloc.h> |
| 4 | +struct node |
| 5 | +{ |
| 6 | + int data; |
| 7 | + struct node *left; |
| 8 | + struct node *right; |
| 9 | +}; |
| 10 | +struct node *tree; |
| 11 | +void create_tree(struct node *); |
| 12 | +struct node *insertElement(struct node *, int); |
| 13 | +void preorderTraversal(struct node *); |
| 14 | +void inorderTraversal(struct node *); |
| 15 | +void postorderTraversal(struct node *); |
| 16 | +struct node *findSmallestElement(struct node *); |
| 17 | +struct node *findLargestElement(struct node *); |
| 18 | +struct node *deleteElement(struct node *, int); |
| 19 | +struct node *mirrorImage(struct node *); |
| 20 | +int totalNodes(struct node *); |
| 21 | +int totalExternalNodes(struct node *); |
| 22 | +int totalInternalNodes(struct node *); |
| 23 | +int Height(struct node *); |
| 24 | +struct node *deleteTree(struct node *); |
| 25 | + |
| 26 | +int main() |
| 27 | +{ |
| 28 | + int option, val; |
| 29 | + struct node *ptr; |
| 30 | + create_tree(tree); |
| 31 | + clrscr(); |
| 32 | + do |
| 33 | + { |
| 34 | + printf("\n ******MAIN MENU******* \n"); |
| 35 | + printf("\n 1. Insert Element"); |
| 36 | + printf("\n 2. Preorder Traversal"); |
| 37 | + printf("\n 3. Inorder Traversal"); |
| 38 | + printf("\n 4. Postorder Traversal"); |
| 39 | + printf("\n 5. Find the smallest element"); |
| 40 | + printf("\n 6. Find the largest element"); |
| 41 | + printf("\n 7. Delete an element"); |
| 42 | + printf("\n 8. Count the total number of nodes"); |
| 43 | + printf("\n 9. Count the total number of external nodes"); |
| 44 | + printf("\n 10. Count the total number of internal nodes"); |
| 45 | + printf("\n 11. Determine the height of the tree"); |
| 46 | + printf("\n 12. Find the mirror image of the tree"); |
| 47 | + printf("\n 13. Delete the tree"); |
| 48 | + printf("\n 14. Exit"); |
| 49 | + printf("\n\n Enter your option : "); |
| 50 | + scanf("%d", &option); |
| 51 | + switch(option) |
| 52 | + { |
| 53 | + case 1: printf("\n Enter the value of the new node : "); |
| 54 | + scanf("%d", &val); |
| 55 | + tree = insertElement(tree, val); |
| 56 | + break; |
| 57 | + case 2: printf("\n The elements of the tree are : \n"); |
| 58 | + preorderTraversal(tree); |
| 59 | + break; |
| 60 | + case 3: printf("\n The elements of the tree are : \n"); |
| 61 | + inorderTraversal(tree); |
| 62 | + break; |
| 63 | + case 4: printf("\n The elements of the tree are : \n"); |
| 64 | + postorderTraversal(tree); |
| 65 | + break; |
| 66 | + case 5: ptr = findSmallestElement(tree); |
| 67 | + printf("\n Smallest element is :%d",ptr–>data); |
| 68 | + break; |
| 69 | + case 6: ptr = findLargestElement(tree); |
| 70 | + printf("\n Largest element is : %d", ptr–>data); |
| 71 | + break; |
| 72 | + case 7: printf("\n Enter the element to be deleted : "); |
| 73 | + scanf("%d", &val); |
| 74 | + tree = deleteElement(tree, val); |
| 75 | + break; |
| 76 | + case 8: printf("\n Total no. of nodes = %d", totalNodes(tree)); |
| 77 | + break; |
| 78 | + case 9: printf("\n Total no. of external nodes = %d", |
| 79 | + totalExternalNodes(tree)); |
| 80 | + break; |
| 81 | + case 10:printf("\n Total no. of internal nodes = %d", |
| 82 | + totalInternalNodes(tree)); |
| 83 | + break; |
| 84 | + case 11:printf("\n The height of the tree = %d",Height(tree)); |
| 85 | + break; |
| 86 | + case 12:tree = mirrorImage(tree); |
| 87 | + break; |
| 88 | + case 13:tree = deleteTree(tree); |
| 89 | + break; |
| 90 | + } |
| 91 | + }while(option!=14); |
| 92 | + getch(); |
| 93 | + return 0; |
| 94 | +} |
| 95 | +void create_tree(struct node *tree) |
| 96 | +{ |
| 97 | + tree = NULL; |
| 98 | +} |
| 99 | +struct node *insertElement(struct node *tree, int val) |
| 100 | +{ |
| 101 | + struct node *ptr, *nodeptr, *parentptr; |
| 102 | + ptr = (struct node*)malloc(sizeof(struct node)); |
| 103 | + ptr–>data = val; |
| 104 | + ptr–>left = NULL; |
| 105 | + ptr–>right = NULL; |
| 106 | + if(tree==NULL) |
| 107 | + { |
| 108 | + tree=ptr; |
| 109 | + tree–>left=NULL; |
| 110 | + tree–>right=NULL; |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + parentptr=NULL; |
| 115 | + nodeptr=tree; |
| 116 | + while(nodeptr!=NULL) |
| 117 | + { |
| 118 | + parentptr=nodeptr; |
| 119 | + if(val<nodeptr–>data) |
| 120 | + nodeptr=nodeptr–>left; |
| 121 | + else |
| 122 | + nodeptr = nodeptr–>right; |
| 123 | + } |
| 124 | + if(val<parentptr–>data) |
| 125 | + parentptr–>left = ptr; |
| 126 | + else |
| 127 | + parentptr–>right = ptr; |
| 128 | + } |
| 129 | + return tree; |
| 130 | +} |
| 131 | +void preorderTraversal(struct node *tree) |
| 132 | +{ |
| 133 | + if(tree != NULL) |
| 134 | + { |
| 135 | + printf("%d\t", tree–>data); |
| 136 | + preorderTraversal(tree–>left); |
| 137 | + preorderTraversal(tree–>right); |
| 138 | + } |
| 139 | +} |
| 140 | +void inorderTraversal(struct node *tree) |
| 141 | +{ |
| 142 | + if(tree != NULL) |
| 143 | + { |
| 144 | + inorderTraversal(tree->left); |
| 145 | + printf("%d\t", tree->data); |
| 146 | + inorderTraversal(tree->right); |
| 147 | + } |
| 148 | +} |
| 149 | +void postorderTraversal(struct node *tree) |
| 150 | +{ |
| 151 | + if(tree != NULL) |
| 152 | + { |
| 153 | + postorderTraversal(tree->left); |
| 154 | + postorderTraversal(tree->right); |
| 155 | + printf("%d\t", tree->data); |
| 156 | + } |
| 157 | +} |
| 158 | +struct node *findSmallestElement(struct node *tree) |
| 159 | +{ |
| 160 | + if( (tree == NULL) || (tree->left == NULL)) |
| 161 | + return tree; |
| 162 | + else |
| 163 | + return findSmallestElement(tree ->left); |
| 164 | +} |
| 165 | +struct node *findLargestElement(struct node *tree) |
| 166 | +{ |
| 167 | + if( (tree == NULL) || (tree->right == NULL)) |
| 168 | + return tree; |
| 169 | + else |
| 170 | + return findLargestElement(tree->right); |
| 171 | +} |
| 172 | +struct node *deleteElement(struct node *tree, int val) |
| 173 | +{ |
| 174 | + struct node *cur, *parent, *suc, *psuc, *ptr; |
| 175 | + if(tree–>left==NULL) |
| 176 | + { |
| 177 | + printf("\n The tree is empty "); |
| 178 | + return(tree); |
| 179 | + } |
| 180 | + parent = tree; |
| 181 | + cur = tree–>left; |
| 182 | + while(cur!=NULL && val!= cur–>data) |
| 183 | + { |
| 184 | + parent = cur; |
| 185 | + cur = (val<cur–>data)? cur–>left:cur–>right; |
| 186 | + } |
| 187 | + if(cur == NULL) |
| 188 | + { |
| 189 | + printf("\n The value to be deleted is not present in the tree"); |
| 190 | + return(tree); |
| 191 | + } |
| 192 | + if(cur–>left == NULL) |
| 193 | + ptr = cur–>right; |
| 194 | + else if(cur–>right == NULL) |
| 195 | + ptr = cur–>left; |
| 196 | + else |
| 197 | + { |
| 198 | + // Find the in–order successor and its parent |
| 199 | + psuc = cur; |
| 200 | + cur = cur–>left; |
| 201 | + while(suc–>left!=NULL) |
| 202 | + { |
| 203 | + psuc = suc; |
| 204 | + suc = suc–>left; |
| 205 | + } |
| 206 | + if(cur==psuc) |
| 207 | + { // Situation 1 |
| 208 | + suc–>left = cur–>right; |
| 209 | + } |
| 210 | + else |
| 211 | + { // Situation 2 |
| 212 | + suc–>left = cur–>left; |
| 213 | + psuc–>left = suc–>right; |
| 214 | + suc–>right = cur–>right; |
| 215 | + } |
| 216 | + ptr = suc; |
| 217 | + } |
| 218 | + // Attach ptr to the parent node |
| 219 | + if(parent–>left == cur) |
| 220 | + parent–>left=ptr; |
| 221 | + else |
| 222 | + parent–>right=ptr; |
| 223 | + free(cur); |
| 224 | + return tree; |
| 225 | +} |
| 226 | + |
| 227 | +int totalNodes(struct node *tree) |
| 228 | +{ |
| 229 | + if(tree==NULL) |
| 230 | + return 0; |
| 231 | + else |
| 232 | + return(totalNodes(tree–>left) + totalNodes(tree–>right) + 1); |
| 233 | +} |
| 234 | +int totalExternalNodes(struct node *tree) |
| 235 | +{ |
| 236 | + if(tree==NULL) |
| 237 | + return 0; |
| 238 | + else if((tree–>left==NULL) && (tree–>right==NULL)) |
| 239 | + return 1; |
| 240 | + else |
| 241 | + return (totalExternalNodes(tree–>left) + totalExternalNodes(tree–>right)); |
| 242 | + } |
| 243 | + |
| 244 | + int totalInternalNodes(struct node *tree) |
| 245 | + { |
| 246 | + if( (tree==NULL) || ((tree–>left==NULL) && (tree–>right==NULL))) |
| 247 | + return 0; |
| 248 | + else |
| 249 | + return (totalInternalNodes(tree–>left) + totalInternalNodes(tree–>right) + 1); |
| 250 | + } |
| 251 | + |
| 252 | + int Height(struct node *tree) |
| 253 | + { |
| 254 | + int leftheight, rightheight; |
| 255 | + if(tree==NULL) |
| 256 | + return 0; |
| 257 | + else |
| 258 | + { |
| 259 | + leftheight = Height(tree–>left); |
| 260 | + rightheight = Height(tree–>right); |
| 261 | + if(leftheight > rightheight) |
| 262 | + return (leftheight + 1); |
| 263 | + else |
| 264 | + return (rightheight + 1); |
| 265 | + } |
| 266 | +} |
| 267 | +struct node *mirrorImage(struct node *tree) |
| 268 | +{ |
| 269 | + struct node *ptr; |
| 270 | + if(tree!=NULL) |
| 271 | + { |
| 272 | + mirrorImage(tree–>left); |
| 273 | + mirrorImage(tree–>right); |
| 274 | + ptr=tree–>left; |
| 275 | + ptr–>left = ptr–>right; |
| 276 | + tree–>right = ptr; |
| 277 | + } |
| 278 | +} |
| 279 | + |
| 280 | +struct node *deleteTree(struct node *tree) |
| 281 | +{ |
| 282 | + if(tree!=NULL) |
| 283 | + { |
| 284 | + deleteTree(tree–>left); |
| 285 | + deleteTree(tree–>right); |
| 286 | + free(tree); |
| 287 | + } |
| 288 | +} |
0 commit comments