当前位置:网站首页>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
边栏推荐
猜你喜欢

2022 chemical automation control instrument examination exercises and online simulation examination

杰理之MUSIC 模式获取播放文件的目录【篇】

7-9 超级玛丽

分享insert into select遇到的死锁问题(项目实战)

Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model

What is a data asset? How should data asset management be implemented?

杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】

大势智慧创建倾斜模型和切割单体化

杰理之外挂 4M 的 flash 在 PC 上查看大小只有 1M 的处理方法【篇】

第014-15讲:字符串 (见小甲鱼新版27讲-32讲)| 课后测试题及答案
随机推荐
第020讲:函数:内嵌函数和闭包 | 课后测试题及答案
Redis learning notes
Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver
微软 Edge 浏览器将支持网络测速,内置计算器和单位转换工具
Lesson 026: Dictionary: when the index is not easy to use 2 | after class test questions and answers
6月25日PMI认证考点防疫要求及考场安排
An example of 89 Oracle SQL writing and optimizer defects
LeetCode#20. Valid parentheses
ACM. HJ24 合唱队 ●●
72 results and development suggestions of the latest on-site production system optimization
IDC發布中國數據治理報告 億信華辰第一
牛客 52次月赛 B牛牛的身高 (思维题 模拟题)
Lesson 027: Set: in my world, you are the only after-school test question and answer
牛客 52次月赛 C 说谎的机器 (区间赋值操作由O(n^2)转为O(n)的复杂度)
Lesson 018: function: flexible is powerful after class test questions and answers
Jerry's music mode obtains the directory of playing files [chapter]
DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]
Campus errand management app Shaanxi Gechuang
Laravel+ pagoda planning task
第029讲:文件:一个任务 | 课后测试题及答案