我对代码还很陌生 blocks.I 在这个错误上花了太多时间。有人能指出我在这段代码中的错误吗
I am fairly new to code blocks.I have spent too much time on this error .Can someone point out what is my mistake in this code
我是代码块的新手。我试图找到 BST 的高度并使用 max()
函数。但是在编译 CodeBlock 时抛出 error.I am using Windows machine.I know linking maths.h manually 将解决 issue.I know how to link math.h 手动使用 gcc 但我不知道 gcc 等同于 windows.Basically 我想知道除了 linking 之外还有其他解决方案吗?如果手动 linking 是唯一的选项离开然后如何在 CodeBlocks.Thank 上 Windows 你!!
undefined reference to max
我很确定逻辑上没有错误,所有导入语句也是正确的。但是还是没有输出!!这是代码:
#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);
}
- 您没有声明
max
函数。
在 findHeight
函数中,您执行了 if(root=NULL)
,本应是 if(root==NULL)
。这样做:
int findHeight(struct BstNode * root){
if(root==NULL){
return -1;
} else {
return max(findHeight(root->left),findHeight(root->right))+1;
}
}
不要忘记打印 findHeight
的值。
我是代码块的新手。我试图找到 BST 的高度并使用 max()
函数。但是在编译 CodeBlock 时抛出 error.I am using Windows machine.I know linking maths.h manually 将解决 issue.I know how to link math.h 手动使用 gcc 但我不知道 gcc 等同于 windows.Basically 我想知道除了 linking 之外还有其他解决方案吗?如果手动 linking 是唯一的选项离开然后如何在 CodeBlocks.Thank 上 Windows 你!!
undefined reference to max
我很确定逻辑上没有错误,所有导入语句也是正确的。但是还是没有输出!!这是代码:
#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);
}
- 您没有声明
max
函数。 在
findHeight
函数中,您执行了if(root=NULL)
,本应是if(root==NULL)
。这样做:int findHeight(struct BstNode * root){ if(root==NULL){ return -1; } else { return max(findHeight(root->left),findHeight(root->right))+1; } }
不要忘记打印
findHeight
的值。