【发布时间】:2015-05-23 19:12:31
【问题描述】:
嗯,我正在尝试从给定的输入创建一个不平衡的二叉搜索树,作为一个(未排序的)整数序列。
我的方法是递归地为每个单独的节点找到正确的位置,然后为它分配内存并为其定义数据。
但是我无法有效地调试程序,因为尽管已经对其进行了适当的审查,但我似乎无法确定问题所在。
对于如下输入:
所以对于输入:
11
15 6 4 8 5 3 1 10 13 2 11
预期的输出应该是后序遍历和中序遍历,但奇怪的是没有打印任何内容(除了我在中间给出的换行符)。
更新:
1.[2015 年 3 月 20 日]-考虑到警告,删除了 malloc 类型转换。
2. [2015 年 3 月 21 日] 做了一些更改,虽然现在,代码只给出以下输出:
11 13
11 13
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define arrmax 100
/***Pointer-based BST implementation, developed by Abhineet Saxena***/
/***The data-type declaration***/
typedef struct node{
int data;
struct node* parent;
struct node* leftChild;
struct node* rightChild;
}Node;
typedef struct tree
{
Node* root;
int size;
}BSTree;
/***Method prototypes***/
/*Method to create a tree*/
Node* createTree(int[],int,int);
//Changed from void to Node*: void createNode(Node*,int);
Node* createNode(Node*,int);
void inOrder(Node* root);
void postOrder(Node *root);
int main(void) {
BSTree* bs_tree;
//Incorrect here: bs_tree=(BSTree*)malloc(sizeof(BSTree));
bs_tree=malloc(sizeof(BSTree));
bs_tree->root=NULL;
bs_tree->size=0;
/****Taking the input****/
int num_elem,iterv;
scanf("%d\n",&num_elem);
//Incorrect here: int *arr=(int*)malloc(sizeof(int)*(num_elem));
int *arr=malloc(sizeof(int)*(num_elem));
for(iterv=0;iterv<num_elem;iterv++)
{
scanf("%d",&arr[iterv]);
}
bs_tree->root=createTree(arr,0,num_elem-1);
postOrder(bs_tree->root);
printf("\n");
inOrder(bs_tree->root);
return 0;
}
Node* createTree(int marr[],int left,int right)
{
int iterv;
Node* root;
root=NULL;
for(iterv=left;iterv<=right;iterv++)
{
//Made changes here: Old:-createNode(root,marr[iterv]);
root=createNode(root,marr[iterv]);
}
return root;
}
Node* createNode(Node* root,int key)
{
if(root==NULL)
{
root=malloc(sizeof(Node));
//printf("Used malloc here for key: %d\n",key);
root->data=key;
root->leftChild=NULL;
root->rightChild=NULL;
root->parent=NULL;
//Changed here: [old]return;
return root;
}
else
{
if(key<root->data)
{
//Added code here[Old: createNode(root->leftChild,key);]
if(root->leftChild!=NULL)
createNode(root->leftChild,key);
else
{
root->leftChild=createNode(root->leftChild,key);
return root;
}
}
else
//Added Code here: [old codeline:-] createNode(root->rightChild,key);
{
if(root->rightChild!=NULL)
createNode(root->rightChild,key);
else
{
root->rightChild=createNode(root->rightChild,key);
return root;
}
}
}
}
void inOrder(Node* bst_tree)
{
if(bst_tree!=NULL)
{
inOrder(bst_tree->leftChild);
printf("%d ",bst_tree->data);
inOrder(bst_tree->rightChild);
}
else
return;
}
void postOrder(Node* bst_tree)
{
if(bst_tree!=NULL)
{
postOrder(bst_tree->leftChild);
postOrder(bst_tree->rightChild);
printf("%d ",bst_tree->data);
}
else
return;
}
【问题讨论】:
-
标准警告:请do not cast
malloc()和C中的家人的返回值。 -
Taking due consideration of the warning,removed the malloc typecast.我猜你错过了很多其他地方。 :-) -
我相信现在一定没问题。我正在尝试这里介绍的方法,将恢复工作修改(以及实现细节)。
-
合并了更改,但仍然无法正常工作,需要帮助。 @Sourav Ghosh 介意指出我哪里可能出错了吗?
-
您可以从
inOrder()和postOrder()中删除else return;。它在那里没有任何作用。
标签: c pointers malloc pass-by-value