6-12 Binary search tree operation set sample runtime error/VS2019 prompts that wntdll is not loaded

You can first check whether there is a problem with memory management operations, such as deleting a pointer that has been deleted again. It may not be obvious, and has been deleted in an inconspicuous place. In short, this is the source of the problem.

There is no problem with debugging in DEV, but since PTA is still unable to pass, I can only reluctantly copy the code to VS2019 to see.

This is the code before the initial error search.

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    
    
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

void PreorderTraversal( BinTree BT ); /* 先序遍历,由裁判实现,细节不表 */
void InorderTraversal( BinTree BT );  /* 中序遍历,由裁判实现,细节不表 */

BinTree Insert( BinTree BST, ElementType X );
BinTree Delete( BinTree BST, ElementType X );
Position Find( BinTree BST, ElementType X );
Position FindMin( BinTree BST );
Position FindMax( BinTree BST );

int main()
{
    
    
    BinTree BST, MinP, MaxP, Tmp;
    ElementType X;
    int N, i;

    BST = NULL;
    scanf("%d", &N);
    for ( i=0; i<N; i++ ) {
    
    
        scanf("%d", &X);
        BST = Insert(BST, X);
    }
    printf("Preorder:"); PreorderTraversal(BST); printf("\n");
    MinP = FindMin(BST);
    MaxP = FindMax(BST);
    scanf("%d", &N);
    for( i=0; i<N; i++ ) {
    
    
        scanf("%d", &X);
        Tmp = Find(BST, X);
        if (Tmp == NULL) printf("%d is not found\n", X);
        else {
    
    
            printf("%d is found\n", Tmp->Data);
            if (Tmp==MinP) printf("%d is the smallest key\n", Tmp->Data);
            if (Tmp==MaxP) printf("%d is the largest key\n", Tmp->Data);
        }
    }
    scanf("%d", &N);
    for( i=0; i<N; i++ ) {
    
    
        scanf("%d", &X);
        BST = Delete(BST, X);
    }
    printf("Inorder:"); InorderTraversal(BST); printf("\n");

    return 0;
}





BinTree Insert( BinTree BST, ElementType X ){
    
    
	
	if(BST== NULL){
    
    
		//新建结点
		
		BST= (BinTree)(malloc(sizeof(struct TNode))); 
		BST->Data= X;
		BST->Left= NULL;
		BST->Right= NULL;
		return BST;
	} 
	if(X<BST->Data){
    
    
		BST->Left= Insert(BST->Left, X);
	}
	else{
    
    

		BST->Right= Insert(BST->Right, X);
	} 
	
	return BST;
	
	
}


//寻找最小值
Position FindMin( BinTree BST ){
    
    
	if(BST== NULL) return NULL;
	if(BST->Left==NULL){
    
    
		return BST;
	} 
	else
		return FindMin(BST->Left);

} 

//寻找最大值 
Position FindMax( BinTree BST ){
    
    
		if(BST== NULL) return NULL;
		if(BST->Right==NULL){
    
    
		return BST;
	} 
	else
		return FindMax(BST->Right);	
}
 
 
//寻找指定值
Position Find( BinTree BST, ElementType X ){
    
    		
		if(BST==NULL) return NULL;//没有找到 
		if(X== BST->Data) return BST; 
		else if(X<BST->Data) return Find(BST->Left, X);//向左找 
		else return Find(BST->Right, X);//向右找 
} 


//删除指定结点
BinTree Delete( BinTree BST, ElementType X ){
    
    
	//搜索移动指针过程 
	//无法找到
	if(BST== NULL) {
    
    
		printf("Not Found\n");
		return NULL; 
	} 
	//中途过程 
	else if(X<BST->Data){
    
    
		BST->Left= Delete(BST->Left, X);
	} 
	else if(X>BST->Data){
    
    
		BST->Right= Delete(BST->Right,X);		
	} 
	//找到 
	else{
    
    
		BinTree temp;
		temp= BST;
		if(BST->Left==NULL){
    
    
			temp= BST;
			BST= BST->Right;
			free(temp);
		}
		else if(BST->Right==NULL){
    
    
			temp= BST;
			BST= BST->Left;
			free(temp);
		}
		else{
    
    
			//左右双全 将结点的值替换 删除右子树最小的 
			BinTree rightMin= FindMin(BST->Right);
			BST->Data= rightMin->Data;
			BST->Right= Delete(BST->Right, rightMin->Data);
			free(rightMin);
		} 
		
		
	} 
	return BST;
} 

//中序遍历
void InorderTraversal(BinTree BST){
    
    
	if(BST==NULL){
    
    
		return; //终止 
	}
	else {
    
    
	
		InorderTraversal(BST->Left);
		printf("%d",BST->Data);
		InorderTraversal(BST->Right);
	}
} 


//前序遍历
void PreorderTraversal(BinTree BST){
    
    
	if(BST==NULL){
    
    
		return; //终止 
	}
	else {
    
    
		printf("%d",BST->Data);
		PreorderTraversal(BST->Left);
		PreorderTraversal(BST->Right);
	}
} 


A breakpoint was triggered in VS2019, promptInsert picture description here

Checking the related blogs, you will find that this is probably a problem related to memory management. For this code, plus my estimate of my C programming ability, considering that several other points have been passed, the problem is almost impossible. Out in the Delete module.

//删除指定结点
BinTree Delete(BinTree BST, ElementType X) {
    
    
	//搜索移动指针过程 
	//无法找到
	if (BST == NULL) {
    
    
		printf("Not Found\n");
		return NULL;
	}
	//中途过程 
	else if (X < BST->Data) {
    
    
		BST->Left = Delete(BST->Left, X);
	}
	else if (X > BST->Data) {
    
    
		BST->Right = Delete(BST->Right, X);
	}
	//找到 
	else {
    
    
		BinTree temp;
		temp = BST;
		if (BST->Left == NULL) {
    
    
			temp = BST;
			BST = BST->Right;
			free(temp);
		}
		else if (BST->Right == NULL) {
    
    
			temp = BST;
			BST = BST->Left;
			free(temp);
		}
		else {
    
    
			//左右双全 将结点的值替换 删除右子树最小的 
			BinTree rightMin = FindMin(BST->Right);
			BST->Data = rightMin->Data;
			BST->Right = Delete(BST->Right, rightMin->Data);
			free(rightMin);
		}


	}
	return BST;
}

I tried to comment out all operations related to freeing memory, because PTA does not require real deletion. In this case, the problem of runtime errors is solved.

Look at the code carefully, it should be a problem with this piece of code

else {
    
    
			//左右双全 将结点的值替换 删除右子树最小的 
			BinTree rightMin = FindMin(BST->Right);
			BST->Data = rightMin->Data;
			BST->Right = Delete(BST->Right, rightMin->Data);
			free(rightMin);
		}

Because rightMin must be a leaf, the pointer of rightMin has been deleted in Delete before free, and an error is reported when free is performed again.

Guess you like

Origin blog.csdn.net/roswellnotfound/article/details/108957645