Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node {
  5.     int data;
  6.     struct node* left;
  7.     struct node* right;
  8. };
  9.  
  10.  int sum1=0,sum2=0;
  11. void inorder(struct node* root){
  12.     if(root == NULL) return;
  13.     inorder(root->left);
  14.     printf("%d ->", root->data);
  15.     inorder(root->right);
  16. }
  17.  
  18. void preorder(struct node* root){
  19.     if(root == NULL) return;
  20.     printf("%d ->", root->data);
  21.     preorder(root->left);
  22.     preorder(root->right);
  23. }
  24.  
  25. void postorder(struct node* root) {
  26.     if(root == NULL) return;
  27.     postorder(root->left);
  28.     postorder(root->right);
  29.     printf("%d ->", root->data);
  30. }
  31.  
  32.  
  33. struct node* createNode(value){
  34.     struct node* newNode = malloc(sizeof(struct node));
  35.     newNode->data = value;
  36.     newNode->left = NULL;
  37.     newNode->right = NULL;
  38.  
  39.     return newNode;
  40. }
  41.  
  42. struct node* insertLeft(struct node *root, int value) {
  43.     root->left = createNode(value);
  44.     return root->left;
  45. }
  46.  
  47.  
  48. struct node* insertRight(struct node *root, int value){
  49.     root->right = createNode(value);
  50.     return root->right;
  51. }
  52. int sum11(struct node *root)
  53. {
  54.      if(root==NULL) return;
  55.        else
  56.     {
  57.     if(root->data%2==0) sum1+=root->data;
  58.     sum11(root->left);
  59.     sum11(root->right);
  60.     }
  61.     return (sum1);
  62. }
  63. int sum22(struct node *root)
  64. {
  65.      if(root==NULL) return;
  66.        else
  67.     {
  68.     if(root->data%2!=0) sum2+=root->data;
  69.     sum22(root->left);
  70.     sum22(root->right);
  71.     }
  72.     return (sum2);
  73. }
  74. void inser(struct node *root)
  75. {
  76.     if(root==NULL) return;
  77.     else
  78.     {
  79.         if(root->data<0) root->data=0;
  80.     }
  81.     printf("->%d",root->data);
  82.     inser(root->left);
  83.     inser(root->right);
  84. }
  85.  
  86.  
  87. int main(){
  88.     struct node* root = createNode(1);
  89.     insertLeft(root, 12);
  90.     insertRight(root, 9);
  91.  
  92.     insertLeft(root->left, -1);
  93.     insertRight(root->left, 6);
  94.  
  95.     printf("Inorder traversal \n");
  96.     inorder(root);
  97.  
  98.     printf("\nPreorder traversal \n");
  99.     preorder(root);
  100.  
  101.     printf("\nPostorder traversal \n");
  102.     postorder(root);
  103.    int e=sum11(root);
  104.    int d=sum22(root);
  105.    printf("\n%d %d\n",e,d);
  106.    inser(root);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement