machkovskitomche

od bosko 3

Aug 9th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int max(int x,int y){return (x>y)?x:y;}
  4. typedef struct Node{
  5.     int val;
  6.     struct Node* right;
  7.     struct Node* left; 
  8. }Node;
  9. Node* makeNew(int data){
  10.     Node* new = (Node*)malloc(sizeof(Node));
  11.     new->val = data;
  12.     new->left = NULL;
  13.     new->right = NULL;
  14.     return new;
  15. }
  16. Node* Insert(Node* root,int data) {
  17.     if(root == NULL) root = makeNew(data);
  18.     else if(data <= root->val) root->left = Insert(root->left,data);
  19.     else root->right = Insert(root->right,data);
  20.     return root;
  21. }
  22. int Search(Node* root, int data){
  23.     if(root == NULL) return 0;
  24.     if(root->val == data) return 1;
  25.     if(data<root->val) return Search(root->left,data);
  26.     else return Search(root->right,data);
  27. }
  28. int maxSum(Node* root,int temp){
  29.     if(root==NULL) return temp;
  30.     int tempSum = temp + root->val;
  31.     return max(maxSum(root->left,tempSum),maxSum(root->right,tempSum));
  32. }
  33. int findHeight(Node* root){
  34.     if(root==NULL) return 0;
  35.     int l = findHeight(root->left);
  36.     int r = findHeight(root->right);
  37.     return 1 + max(l,r);
  38. }
  39. Node* addRec(Node* root,int data){
  40.     if(root==NULL){
  41.         root = makeNew(data);
  42.         return root;
  43.     }
  44.     if(data<root->val){
  45.         return root->left = addRec(root->left,data);
  46.     }
  47.     else{
  48.         return root->right = addRec(root->right,data);
  49.     }
  50. }
  51. int main(int argc, char const *argv[]){
  52.         Node* root = NULL;
  53.         int i = 30;
  54.         while(i>0){
  55.         root = Insert(root,i);
  56.             i--;   
  57.         }
  58.         int max = findHeight(root);
  59.         printf("MAX = %d\n",max);
  60.         return 0;
  61. }
Add Comment
Please, Sign In to add comment