In this program, we are going to share a C program to find the common ancestor and print the path. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C program to find the common ancestor and print the path.
We have designed this program for beginners for learning purpose. Copy below c program and execute it with c compiler to see the output of the program.
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
#include <stdio.h> #include <stdlib.h> struct btnode { int value; struct btnode *l; struct btnode *r; }; typedef struct btnode N; N* new(int); int count; void create(); void preorder(N *t); void ancestor(N *t); int search(N *t, int, int); void path(int, int, int); N *root = NULL; void main() { int choice; create(); while (1) { printf("Enter the choice\n"); printf("1-Display : 2-path : 3-Exit\n"); scanf("%d", &choice); switch (choice) { case 1: printf("preorder display of tree elements\n"); preorder(root); printf("\n"); break; case 2: ancestor(root); break; case 3: exit(0); default: printf("Enter the right choice\n"); } } } /* creating temporary node */ N* new(int data) { N* temp = (N*)malloc(sizeof(N)); temp->value = data; temp->l = NULL; temp->r = NULL; return(temp); } /* Creating the binary search tree */ void create() { root = new(10); root->l = new(7); root->r = new(15); root->l->l = new(6); root->l->r = new(8); root->r->l = new(12); root->r->r = new(18); root->r->r->r = new(20); root->l->l->l = new(5); root->l->r->r = new(9); } /* To display the preorder traversal of the tree */ void preorder(N *temp) { printf("%d->", temp->value); if (temp->l != NULL) preorder(temp->l); if (temp->r != NULL) preorder(temp->r); } /* to find common ancestor for given nodes */ void ancestor(N *temp) { int a, b, anc = 0; count = 0; printf("enter two node values to find common ancestor\n"); scanf("%d", &a); scanf("%d", &b); count = search(root, a, b); if (count == 2) { while (temp->value != a && temp->value != b) { if ((temp->value > a)&&(temp->value > b)) { anc = temp->value; temp = temp->l; } else if ((temp->value < a)&&(temp->value < b)) { anc = temp->value; temp = temp->r; } else if ((temp->value > a)&&(temp->value < b)) { anc = temp->value; printf("anc = %d\n", anc); break; } else if ((temp->value < a)&&(temp->value > b)) { anc = temp->value; temp = temp->r; } else { printf("common ancestor = %d\n", anc); break; } } path(anc, a, b); } else printf("enter correct node values & do not enter root value\n"); } /* to find whether given nodes are present in tree or not */ int search(N *temp, int a, int b) { if ((temp->value == a ||temp->value == b)&& (root->value != a&&root->value != b)) { count++; } if (temp->l != NULL) search(temp->l, a, b); if (temp->r != NULL) search(temp->r, a, b); return count; } /* to print the path ancestor to given nodes */ void path(int anc, int c, int b) { N *temp = NULL; int i = 0, a[2]; a[0] = c; a[1] = b; for (;i < 2;i++) { if (anc == root->value) // If ancestor is root { temp = root; while (1) { printf("%d", temp->value); if (a[i] < temp->value) temp = temp->l; else if (a[i] > temp->value) temp = temp->r; else { if (a[i] == temp->value) { break; } } printf("->"); } printf("\n"); } else if (anc < root->value) //If ancestor is less than the root value { temp = root; while (temp != NULL) { if (anc < temp->value) temp = temp->l; else if (anc > temp->value) temp = temp->r; else { while (1) { if (a[i] < temp->value) { printf("%d->", temp->value); temp = temp->l; } else if (a[i] > temp->value) { printf("%d->", temp->value); temp = temp->r; } else { printf("%d\n", temp->value); break; } } } } } else //If ancestor greater than the root value { temp = root; while (temp != NULL) { if (anc > temp->value) temp = temp->r; else if (anc < temp->value) temp = temp->l; else { while (1) { if (a[i] < temp->value) { printf("%d->", temp->value); temp = temp->l; } else if (a[i] > temp->value) { printf("%d->", temp->value); temp = temp->r; } else { printf("%d\n", temp->value); break; } } } } } } } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.