Data structure | PTA function question 04-tree 7 operation set of binary search tree

In this paper, five common operations of a given binary search tree are required.

Function interface definition:

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 );

The BinTree structure is defined as follows:

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

The Insert function inserts X into the binary search tree BST and returns the root node pointer of the result tree;
Function Delete deletes X from the binary search tree BST and returns the root node pointer of the result tree; if X is not in the tree, print a line of Not Found and returns the root node pointer of the original tree;
The Find function finds X in the binary search tree BST, and returns the pointer of the node; if not, it returns a null pointer;
The FindMin function returns the pointer of the smallest element node in the binary search tree BST;
The FindMax function returns a pointer to the largest meta node in the binary search tree BST.

Sample referee test procedure:

#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 ); /* First order traversal, implemented by the referee, details are not shown */
void InorderTraversal( BinTree BT );  /* Middle order traversal, implemented by referees, details not shown */

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;
}
/* Your code will be embedded here */

Input example:

10
5 8 6 2 4 1 0 10 9 7
5
6 3 10 0 5
5
5 7 0 10 3

Output example:

Preorder: 5 2 1 0 4 8 6 7 10 9
6 is found
3 is not found
10 is found
10 is the largest key
0 is found
0 is the smallest key
5 is found
Not Found
Inorder: 1 2 4 6 8 9

By code:

/* Insert X into the binary search tree BST and return the root node pointer of the result tree */
BinTree Insert( BinTree BST, ElementType X ){
	if( !BST ){
		BST = (BinTree)malloc(sizeof(struct TNode));
		BST->Data = X;
		BST->Left = BST->Right = NULL;
	}else{
		if( X < BST->Data )
			BST->Left = Insert( BST->Left, X );
		else if( X > BST->Data )
			BST->Right = Insert( BST->Right, X ); 
	}
	return BST;
}

/* Delete X from the binary search tree BST and return the root node pointer of the result tree; if X is not in the tree, print a line of Not Found and return the root node pointer of the original tree */ 
BinTree Delete( BinTree BST, ElementType X ){
	Position Tmp;
	if( !BST )
		printf("Not Found\n");	//Remember \n 
	else if( X < BST->Data )
		BST->Left = Delete( BST->Left, X );
	else if( X > BST->Data )
		BST->Right = Delete( BST->Right, X );
	else{
		if( BST->Left && BST->Right ){
			Tmp = FindMin( BST->Right );
			BST->Data = Tmp->Data;
			BST->Right = Delete(BST->Right, BST->Data);
		}else{
			Tmp = BST;
			if( !BST->Left )
				BST = BST->Right;
			else if( !BST->Right )
				BST = BST->Left;
			free( Tmp );
		}
	}
	return BST;
}

/* Find X in the binary search tree BST, return the pointer of the node; if not, return the null pointer */
Position Find( BinTree BST, ElementType X ){
	while( BST ){
		if( X > BST->Data )
			BST = BST->Right;	//X is larger than the value of this node. Go to the right subtree 
		else if( X < BST->Data )
			BST = BST->Left;	//X is smaller than the value of this node. Look for the left subtree 
		else
			return BST;		//It's X -- it's found 
	}
	return NULL;	//Search failed 
}

/* Return the pointer of the smallest element node in the BST of binary search tree (recursive writing method) */ 
Position FindMin( BinTree BST ){	 
	if( !BST ) //Empty tree 
		return NULL;
	else if( !BST->Left )
		return BST;	//Find the leftmost node (the leftmost node is the minimum value) 
	else 
		return FindMin( BST->Left );	//Continue to the left branch 
}

/* Return the pointer of the largest meta node in the binary search tree BST (non recursive writing method)  */ 
Position FindMax( BinTree BST ){
	if( !BST ) //Empty tree 
		return NULL;	
	else {
		while(	BST->Right ){	//Search to the right node until NULL is encountered (no value is assigned to BST when NULL is encountered here) 
			BST = BST->Right;
		}
	}
	return BST;	//Continue to right branch search 
}

Evaluation results:

Posted by s_ainley87 on Tue, 12 Nov 2019 08:39:57 -0800