我一直在试图看看为什么printf在打印文件输入后没有破坏循环。 .c文件是BST,我现在正在测试是否已经构建了一个树,但似乎无法退出printf循环。我需要printf循环来获得代码的正确输出。有关此错误发生原因的任何建议。显示完整的代码和输出。
#include "bst.h"
#include <stdio.h>
#include <stdlib.h>
///Author: Joe W
//arbitrary list of temp nodes
TreeNode *new_node, *root, *tmp, *parent;
int elemArray[100], i1, i2, i0;
/*
Insert a new node into the tree by referencing the root and using recursion
*/
TreeNode* getN(int dataElem) {
TreeNode* temp;
temp = malloc(sizeof(TreeNode*));
temp-> left = NULL;
temp-> right = NULL;
temp->data = dataElem;
return temp;
}
TreeNode* addNodeToTree(TreeNode *root, int data) {
TreeNode* newNode = malloc(sizeof(TreeNode*));
if (root == NULL)
{
// thingy too. Creating a function too,
// for you to look at.
root = newNode;
return root;
}
else
{
TreeNode* parent = malloc(sizeof(TreeNode*));
TreeNode* current = malloc(sizeof(TreeNode*));
parent = current = root;
// This loop will actually help us, find the `parent`,
// of the `newNode`(which is to be inserted)
while (current != NULL)
{
parent = current;
if (current->data > data)
current = current->left;
else if (current->data < data)
current = current->right;
}
// Now once, we have found, the node, under which the
// newNode will be placed, now we have to check, whether
// newNode will become a `left child/right child` of the
// parent.
newNode = getN(data);
if (parent->data > data)
parent->left = newNode;
else if (parent->data < data)
parent->right = newNode;
return root;
}
}
void build_Tree(TreeNode** root, const int elements[], const int count) {
//TreeNode* node = malloc(sizeof(TreeNode*));
//node->left = node ->right = NULL;
for ( i0 = 0; i0 < count; ++i0 ){
*root = addNodeToTree(*root, elements[count]);
}
}
编译好。文件接受用户输入并开始显示代码后,错误开始。用printf语句。
int main( int argc, char *argv[] ) {
//Throw error is *Argv[] is not an integer
//assuming it was an integer
int cnt = atoi( argv[1] );
printf( "number is %d\n", cnt );
//
printf("Enter %i integer values to place in tree:\n", cnt);
for ( i1 = 0; i1 < cnt; ++i1) {
scanf( "%d", &elemArray[i1] );
}
//first ouput "INput Values"
printf( " Input Values:\n " );
for ( i2 = 0; i2 < cnt; ++i2) {
printf( "%d\n", elemArray[i2] );
printf("building tree0\n");
}
printf("building tree");
TreeNode** root = malloc(sizeof(TreeNode*));
printf("building tree");
build_Tree(root, elemArray, cnt );
printf("Tree Built");
printf( "Preorder:\n ");
//traverse
//TreeNode* tempN = malloc(sizeof(TreeNode*));
//tempN->data= 5;
traverse( *root, PREORDER); //pass the pointer of root to traverse the tree
//traverse a single node
printf( "Inorder:\n ");
printf( "Postorder:\n ");
//Build tree with each element
return 0;
}
void traverse( const TreeNode* root, const TraversalType type ) {
if ( type == PREORDER) {
if (root != NULL)
{
printf("%d", root->data);
traverse( root->left, PREORDER);
traverse( root-> right, PREORDER);
}
}
}
/**
void insertNode(TreeNode** root, TreeNode* new_node) {
if (new_node-> data < *root-> data) {
if (*root-> left == NULL)
*root-> left == new_node;
else
insert(*root->left, new_node);
}
if (new_node->data > *root->data) {
if(*root-> right ==NULL)
*root->right = new_node;
else
insert(*root->right, new_node);
}
}
**/
//question1: what is the
这是我开始运行代码的地方
jw8453@idaho:~/Courses/CS243/Homework/5$ make
gcc -ggdb -c bst.c
gcc -ggdb -o bst bst.o -lm
jw8453@idaho:~/Courses/CS243/Homework/5$ ./bst 5
number is 5
Enter 5 integer values to place in tree:
1
2
3
4
5
Input Values:
1
building tree0
2
building tree0
3
building tree0
4
building tree0
5
building tree0
this isnt supposed to be in the printf loop
的延续
答案 0 :(得分:1)
这是错误的:
TreeNode* newNode = malloc(sizeof(TreeNode*));
代替
TreeNode* newNode = malloc(sizeof(TreeNode));
或
TreeNode* newNode = malloc(sizeof(*newNode));
下面:
TreeNode* parent = malloc(sizeof(TreeNode*));
TreeNode* current = malloc(sizeof(TreeNode*));
parent = current = root;
您有内存泄漏,为parent
和current
预留了空间,然后将这些变量分配到另一个地址,同样适用于:
TreeNode* newNode = malloc(sizeof(TreeNode*));
...
newNode = getN(data);
我建议
void setN(TreeNode *node, int dataElem) {
node->left = NULL;
node->right = NULL;
node->data = dataElem;
}
...
setN(newNode, data);
而不是
TreeNode* getN(int dataElem) {
TreeNode* temp;
temp = malloc(sizeof(TreeNode*));
temp-> left = NULL;
temp-> right = NULL;
temp->data = dataElem;
return temp;
}
...
newNode = getN(data);