写的一道数据结构树二叉树删除的作业
关于数据结构的正确与否不需要大神考虑,只要帮我看看为什么这道程序执行顺序这么奇怪
代码比较长:请关注main函数里面出现的几个函数就行了,其他不用管
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct aaa {
int key;
char name[20];
char address[100];
} element;
typedef struct node *nodePointer;
typedef struct node {
nodePointer leftChild;
element data;
nodePointer rightChild;
}node;
node* add( nodePointer *q );
void printall( struct node *q );
void searchSmallestAmongLargerNodes( nodePointer search, nodePointer* parentSmall, nodePointer *searchSmall )
{
nodePointer temp = search->rightChild;
if ( temp->rightChild = NULL )
{
*parentSmall = search;
*searchSmall = temp;
}else { *parentSmall = temp;
*searchSmall = temp->rightChild; }
}
/* nodePointer root = NULL; */
int searchParentToInsert( struct node *root, int key, struct node **parentPtr )
{
int found = 0;
struct node *parent = NULL, *search = root;
while ( search != NULL )
{
if ( key == search->data.key )
{
found = 1;
break;
}else {
parent = search;
if ( key < search->data.key )
search = search->leftChild;
else
search = search->rightChild;
}
}
*parentPtr = parent;
return(found);
}
void insertBST( struct node **rootPtr, element x )
{
struct node *ptr, *parent;
int found = 0;
found = searchParentToInsert( *rootPtr, x.key, &parent );
if ( found == 1 )
{
printf( "The key already exists in the tree!\n" );
} else{
ptr = (node *) malloc( sizeof(node) );
ptr->data = x;
ptr->leftChild = ptr->rightChild = NULL;
if ( *rootPtr == NULL )
{
*rootPtr = ptr;
} else {
if ( x.key < parent->data.key )
parent->leftChild = ptr;
else parent->rightChild = ptr;
}
}
}
int deleteBST( struct node **rootPtr, int key )
{
int find1 = 0;
struct node **root, **parent, *search, *parentSmall, *searchSmall;
root = (struct node * * ) malloc( sizeof(struct node*) );
*root = *rootPtr;
parent = (struct node * * ) malloc( sizeof(struct node*) );
search = (struct node * ) malloc( sizeof(struct node) );
searchSmall = (struct node * ) malloc( sizeof(struct node) );
find1 = searchParentToInsert( *root, key, parent );
if ( find1 == 0 )
{
return(0);
} /* NOT FOUND} */
/* case 1. Deletion of a leaf node. */
if ( (search->leftChild == NULL) && (search->rightChild == NULL) )
free( search );
if ( (search->leftChild == NULL) || (search->rightChild == NULL) )
{
{ if ( search->leftChild != NULL )
(*parent) = (*parent)->leftChild;
else
(*parent) = (*parent)->rightChild; }
if ( (search->leftChild != NULL) && (search->rightChild != NULL) )
{
/* find the smallest among the larger than the key */
searchSmallestAmongLargerNodes( search, &parentSmall, &searchSmall );
search->data.key = searchSmall->data.key;
if ( search == parentSmall )
parentSmall->rightChild = searchSmall->rightChild;
else parentSmall->leftChild = searchSmall->rightChild;
free( searchSmall );
}
}
return(0);
}
int main( void )
{
int cc;
nodePointer q;
q = (struct node *) malloc( sizeof(struct node) );
q = add( &q );
cc = deleteBST( &q, 3 );
printall( q );
}
nodePointer add( nodePointer *q )
{
int coun = 0;
while ( 1 )
{
struct node *newnode = NULL;
newnode = (struct node *) malloc( sizeof(struct node) );
scanf( "%d,", &newnode->data.key );
if ( newnode->data.key == -1 )
{
return(*q);
}
newnode->leftChild = NULL;
newnode->rightChild = NULL;
scanf( "%[^,]%[^\n]", newnode->data.name, newnode->data.address );
fflush( stdin );
/* gets_s(newnode->add); */
if ( coun == 0 )
{
*q = newnode;
coun++;
}else { insertBST( q, newnode->data ); }
}
return(*q);
}
void printall( struct node *q ) /* inorder */
{
struct node *temp = q;
if ( temp )
{
printall( temp->leftChild );
printf( "%d %s %s\n", temp->data.key, temp->data.name, temp->data.address );
printall( temp->rightChild );
}
}
我输入了测试代码是
1,asdf,sdf
2,dsf,sdf
3,sdf,sdf
-1
首先看下 nodePointer add(nodePointer *q) 函数(add函数建立二叉树)
循环输入知道输入-1结束
但是当我输入第一行1,asdf,sdf结束的时候,就会main函数从上到下过一下(我用了断点调试)
输入第二行第三行也是这样
输入-1结束输入的时候
cc=deleteBST(&q, 3);这一行直接跳过
(按照顺序执行,这一行应该执行啊)
(deleteBST删除二叉树中的 3 )
请大神输入我的测试代码调试一下
20分等解决问题的大神来拿哦