当前位置:网站首页>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
边栏推荐
- IDC releases China Data Governance Report Yixin Huachen No. 1
- Cannot re-register id: PommeFFACompetition-v0问题解决
- Jerry's problem of opening the near end of four channel call [chapter]
- 80- paging query, not only writing
- es 按条件查询数据总条数
- RuntimeError: CUDA error: CUBLAS_ STATUS_ EXECUTION_ FAILED when calling `cublasSgemmStridedBatched( ha
- ICML2022 | 利用虚拟节点促进图结构学习
- 第027讲:集合:在我的世界里,你就是唯一 | 课后测试题及答案
- When the AUX1 or aux2 channel is used in Jerry's aux mode, the program will reset the problem [chapter]
- ACM. Hj24 chorus ●●
猜你喜欢

不同网络结构的特征也能进行对比学习?蚂蚁&美团&南大&阿里提出跨架构自监督视频表示学习方法CACL,性能SOTA!
![Jerry's dynamic switching EQ document [chapter]](/img/2d/9a0245b87fb05ea61dbfc7922ea766.png)
Jerry's dynamic switching EQ document [chapter]

300. longest increasing subsequence ●●

Lesson 018: function: flexible is powerful after class test questions and answers

TC397 Flash

Capital and share increase of avita technology under Chang'an is settled: Ningde times will hold about 24%!

数据库总结:mysql在开发过程中常见的问题及优化

Research hotspot - Official publicity! The release time of JCR zoning and impact factors will be determined in 2022!

(duc/ddc) digital up mixing / quadrature down mixing principle and MATLAB simulation

The necessary materials for the soft test have been released. All the soft test materials for the whole subject have been prepared for you!
随机推荐
80- paging query, not only writing
Jerry's dynamic switching EQ document [chapter]
Research hotspot - Official publicity! The release time of JCR zoning and impact factors will be determined in 2022!
The problem that Jericho can't play the prompt tone in the music mode using the interrupt mode [chapter]
HarmonyOS应用开发培训第二次
第032讲:异常处理:你不可能总是对的 | 课后测试题及答案
Redis核心技术与实战:学习总结目录
kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动
第021讲:函数:lambda表达式 | 课后测试题及答案
Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model
88- widely circulated parameter optimization, honey or poison?
校园跑腿管理端APP—陕西格创
软考必备资料大放送,全科目软考资料都给你备好了!
数据库总结:mysql在开发过程中常见的问题及优化
Android kotlin SP DP to PX
5分钟快速上线Web应用和API(Vercel)
杰理之列免晶振一拖八烧录升级【篇】
杰理之开启四声道通话近端卡顿问题【篇】
Campus errand management app Shaanxi Gechuang
Ten thousand words long text | use RBAC to restrict access to kubernetes resources