当前位置:网站首页>6-1 operation set of binary search tree

6-1 operation set of binary search tree

2022-06-22 21:55:00 White -

6-1 Operation set of binary search tree

This problem requires the implementation of a given binary search tree 5 Common operations .

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 );
among BinTree The structure is defined as follows :

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
function Insert take X Insert binary search tree BST And return the root node pointer of the result tree ;
function Delete take X Search the tree from the binary BST Delete in , And return the root node pointer of the result tree ; If X Not in the tree , Then print a line Not Found And return the root node pointer of the original tree ;
function Find Search the tree in the binary BST Find X, Return the pointer of the node ; Returns a null pointer if not found ;
function FindMin Back to binary search tree BST Pointer to the smallest element node in ;
function FindMax Back to binary search tree BST Pointer to the largest meta node in .
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 ); /* The first sequence traversal , By the referee , Details don't show /
void InorderTraversal( BinTree BT ); /
In the sequence traversal , By the referee , Details don't show */

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 */
sample input :
10
5 8 6 2 4 1 0 10 9 7
5
6 3 10 0 5
5
5 7 0 10 3
sample output :
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

Code :

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

}
Position Find( BinTree BST, ElementType X )
{
    
    if(BST==NULL)
        return NULL;
    else
    {
    
        if(X>BST->Data)
            return Find(BST->Right,X);
        else if(X<BST->Data)
            return Find(BST->Left,X);
        else
            return BST;
    }
}
BinTree Delete( BinTree BST, ElementType X )
{
    
    if(BST==NULL)
    {
    
        printf("Not Found\n");
        return BST;
    }
    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==NULL&&BST->Right==NULL)
        {
    
            free(BST);
            BST=NULL;
        }
        else if(BST->Left==NULL&&BST->Right!=NULL)
        {
    
            BinTree temp=BST->Right;
            free(BST);
            BST=temp;
        }
        else if(BST->Left!=NULL&&BST->Right==NULL)
        {
    
            BinTree temp=BST->Left;
            free(BST);
            BST=temp;
        }
        else if(BST->Left!=NULL&&BST->Right!=NULL)
        {
    
            BinTree Max;
            Max=FindMax(BST->Left);
            BST->Data=Max->Data;
            BST->Left=Delete(BST->Left,BST->Data);
        }
        }
    }
    return BST;
}
Position FindMin( BinTree BST )
{
    
    if(BST==NULL)
        return NULL;
    else
    {
    
        if(BST->Left==NULL)
            return BST;
        else
            return FindMin(BST->Left);
    }
}
Position FindMax( BinTree BST )
{
    
    if(BST==NULL)
        return NULL;
    else
    {
    
        if(BST->Right==NULL)
            return BST;
        else
            return FindMax(BST->Right);
    }
}

202206201634 One

原网站

版权声明
本文为[White -]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222023313810.html