I am new to codeblocks. I was trying to find height of a BST and used max()
function. But while compiling CodeBlock throws error.I am using Windows machine.I know linking maths.h manually will solve the issue.I know how to link math.h manually using gcc but i dont know gcc equivalent for windows.Basically i want to know is there any other solution besides linking??If manually linking is the only option left then how to do it on Windows on CodeBlocks.Thank you!!
undefined reference to max
I am pretty sure there is no error in logic all import statements are correct too. But still no output!! Here is the code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//Definition of Node for Binary search tree
struct BstNode {
int data;
struct BstNode* left;
struct BstNode* right;
};
struct BstNode* GetNewNode(int);
struct BstNode* Insert(struct BstNode*,int );
int findHeight(struct BstNode *);
//struct BstNode* findMin(struct BstNode* root)
// Function to create a new Node in heap
struct BstNode* GetNewNode(int data) {
struct BstNode* newNode = (struct BstNode*)malloc(sizeof(struct BstNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
// To insert data in BST, returns address of root node
struct BstNode* Insert(struct BstNode* root,int data) {//pass by value therefore * used and reurn stmt.otherwise pbarg ** no return
if(root == NULL) { // empty tree
root = GetNewNode(data);
}
// if data to be inserted is lesser, insert in left subtree.
else if(data <= root->data) {
root->left = Insert(root->left,data);
}
// else, insert in right subtree.
else {
root->right = Insert(root->right,data);
}
return root;
}
int findHeight(struct BstNode * root){
if(root=NULL){
return -1;
}
else{
return max(findHeight(root->left),findHeight(root->right))+1;
}
};
//BstNode* findMin(BstNode* root)
int main() {
int a,b;
struct BstNode* root = NULL; // Creating an empty tree
/*Code to test the logic*/
root = Insert(root,15);
root = Insert(root,10);
root = Insert(root,20);
root = Insert(root,25);
root = Insert(root,8);
root = Insert(root,12);
findHeight(root);
}