Created
July 5, 2015 01:22
-
-
Save juanplopes/386557a11ad616548173 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdio.h" | |
typedef struct tree tree; | |
struct tree{ | |
int value; | |
tree *left ; | |
tree *right; | |
}; | |
tree *tree_first_bigger(tree *t) { | |
if (t->left != NULL) | |
return tree_first_bigger(t->left); | |
return t; | |
} | |
tree *find_first_bigger(tree *t, int x){ | |
if(t == NULL) | |
return NULL; | |
if(t->value > x) { | |
tree *answer = find_first_bigger(t->left, x); | |
if (answer != NULL) return answer; | |
return t; | |
} else if(t->value < x) { | |
return find_first_bigger(t->right, x); | |
} else if(t->value == x) { | |
if (t->right != NULL) | |
return tree_first_bigger(t->right); | |
return NULL; | |
} | |
} | |
void setT(tree *t, int value, tree* left, tree* right) { | |
t->value = value; | |
t->left = left; | |
t->right = right; | |
} | |
int main() { | |
tree T, J, U, O, P, M, N; | |
setT(&T, 'T', &J, &U); | |
setT(&J, 'J', NULL, &O); | |
setT(&U, 'U', NULL, NULL); | |
setT(&O, 'O', &M, &P); | |
setT(&P, 'P', NULL, NULL); | |
setT(&M, 'M', NULL, &N); | |
setT(&N, 'N', NULL, NULL); | |
printf("%c\n", (char)find_first_bigger(&T, 'T')->value); | |
printf("%c\n", (char)find_first_bigger(&T, 'N')->value); | |
printf("%c\n", (char)find_first_bigger(&T, 'P')->value); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment