Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created July 5, 2015 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juanplopes/386557a11ad616548173 to your computer and use it in GitHub Desktop.
Save juanplopes/386557a11ad616548173 to your computer and use it in GitHub Desktop.
#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