Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- struct bstnode {
- int item;
- struct bstnode* left;
- struct bstnode* right;
- };
- struct bst {
- struct bstnode* root;
- };
- int binsert(struct bstnode **root, int info){
- if(!*root){
- *root = malloc(sizeof(struct bstnode));
- (*root)->item = info;
- (*root)->left = NULL;
- (*root)->right = NULL;
- } else
- return (info > (*root)->item) ? \
- binsert(&((*root)->right), info) : \
- binsert(&(*root)->left, info);
- return 0;
- }
- int sum_greater(struct bst * t, int num){
- struct bstnode *ptr = t->root;
- if(!ptr)
- return 0;
- struct bst right_tree = { t->root->right };
- struct bst left_tree = { t->root->left };
- if(ptr->item > num)
- return ptr->item + \
- sum_greater(&left_tree, num) + \
- sum_greater(&right_tree, num);
- else // ptr->item =< num
- return sum_greater(&right_tree, num);
- }
- void inordre(struct bstnode *root){
- if(root){
- inordre(root->left);
- printf("%d\n", root->item);
- inordre(root->right);
- }
- }
- int main(){
- struct bstnode *root = NULL;
- int val[] = {100, 50, 200, 25, 75, 150, 250, 15, 35, 65, 85, 120, 170, 230, 300};
- int i;
- for(i = 0; i < sizeof(val)/sizeof(int); i++)
- binsert(&root, val[i]);
- inordre(root);
- struct bst t = {root};
- printf("sum:%d\n", sum_greater(&t, 26));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement