Created
October 5, 2021 17:31
-
-
Save devil-cyber/69f125ec3a0743ad14da64b1cd2f5c12 to your computer and use it in GitHub Desktop.
Binary Tree implementation in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<math.h> | |
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) | |
#define MIN(X,Y) ((X) > (Y) ? (Y) : (X)) | |
struct Node{ | |
int data; | |
struct Node *left; | |
struct Node *right; | |
}; | |
struct Node *NewNode(int data){ | |
struct Node *node = (struct Node*)malloc(sizeof(struct Node)); | |
node->data = data; | |
node->left = NULL; | |
node->right = NULL; | |
}; | |
struct Node *Tree(){ | |
struct Node *root; | |
root = NewNode(10); | |
root->left = NewNode(30); | |
root->right = NewNode(20); | |
root->right->left = NewNode(45); | |
root->left->right = NewNode(23); | |
return root; | |
} | |
void inOrder(struct Node *root){ | |
if(root==NULL){ | |
return; | |
} | |
inOrder(root->left); | |
printf("%d ",root->data); | |
inOrder(root->right); | |
} | |
int height(struct Node *root){ | |
if(root==NULL){ | |
return 0; | |
} | |
return 1 + MAX(height(root->left),height(root->right)); | |
} | |
int main() | |
{ | |
struct Node *root; | |
root = Tree(); | |
inOrder(root); | |
printf("\n"); | |
printf("The height of the Tree is: %d\n",height(root)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment