Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int max(int x,int y){return (x>y)?x:y;}
- typedef struct Node{
- int val;
- struct Node* right;
- struct Node* left;
- }Node;
- Node* makeNew(int data){
- Node* new = (Node*)malloc(sizeof(Node));
- new->val = data;
- new->left = NULL;
- new->right = NULL;
- return new;
- }
- Node* Insert(Node* root,int data) {
- if(root == NULL) root = makeNew(data);
- else if(data <= root->val) root->left = Insert(root->left,data);
- else root->right = Insert(root->right,data);
- return root;
- }
- int Search(Node* root, int data){
- if(root == NULL) return 0;
- if(root->val == data) return 1;
- if(data<root->val) return Search(root->left,data);
- else return Search(root->right,data);
- }
- int maxSum(Node* root,int temp){
- if(root==NULL) return temp;
- int tempSum = temp + root->val;
- return max(maxSum(root->left,tempSum),maxSum(root->right,tempSum));
- }
- int findHeight(Node* root){
- if(root==NULL) return 0;
- int l = findHeight(root->left);
- int r = findHeight(root->right);
- return 1 + max(l,r);
- }
- Node* addRec(Node* root,int data){
- if(root==NULL){
- root = makeNew(data);
- return root;
- }
- if(data<root->val){
- return root->left = addRec(root->left,data);
- }
- else{
- return root->right = addRec(root->right,data);
- }
- }
- int main(int argc, char const *argv[]){
- Node* root = NULL;
- int i = 30;
- while(i>0){
- root = Insert(root,i);
- i--;
- }
- int max = findHeight(root);
- printf("MAX = %d\n",max);
- return 0;
- }
Add Comment
Please, Sign In to add comment