欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > 数据结构(陈越,何钦铭) 第四讲 树(中)

数据结构(陈越,何钦铭) 第四讲 树(中)

2025/2/26 19:31:52 来源:https://blog.csdn.net/qq_44845100/article/details/145862824  浏览:    关键词:数据结构(陈越,何钦铭) 第四讲 树(中)

4.1 二叉搜索树

4.1.1 二叉搜索树及查找

在这里插入图片描述

Position Find(ElementTyoe X,BinTree BST){if(!BST){return NULL;}if(X>BST->Data){return Find(X,BST->Right)}else if(X<BST->Data){return Find(X,BST->Left)}else{return BST;}
} Position IterFind(ElementType X,BinTree BST){while(BST){if(X>BST->Data){BST=BST->Right;}else if(X<BST->Data){BST=BST->Left;}else{return BST;}}
}Position FindMin(BinTree BST){if(!BST){return NULL;}else if(!BST->Left){return BST;}else{return FindMin(BST->Left);}
}Position FindMax(BinTree BST){if(BST){while(BST->Right){BST=BST->Right;}}return BST;
}

4.1.2 二叉搜索树的插入

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

4.1.3 二叉搜索树的删除

BinTree Delete(ElementType X,BinTree BST){Position Tmp;if(!BST){printf("要删除的元素未找到");}else if(X<BST->Data){BST->Left=Delete(X,BST->Left);}else if(X>BST->Data){BST->Right=Delete(X,BST->Right);}else{if(BST->Left&&BST->Right){Tmp=FindMin(BST->Right);BST->Data=Tmp->Data;BST->Right=Delete(BST->Data,BST->Right);}else{Tmp=BST;if(!BST->Left){BST=BST->Right;}else if(!BST->Right){BST=BST->Left;}free(Tmp);}}return BST;
}

4.2 平衡二叉树

4.2.1 什么是平衡二叉树

在这里插入图片描述

4.2.2 平衡二叉树的调整

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Position Find(ElementTyoe X,BinTree BST){if(!BST){return NULL;}if(X>BST->Data){return Find(X,BST->Right)}else if(X<BST->Data){return Find(X,BST->Left)}else{return BST;}
} Position IterFind(ElementType X,BinTree BST){while(BST){if(X>BST->Data){BST=BST->Right;}else if(X<BST->Data){BST=BST->Left;}else{return BST;}}
}Position FindMin(BinTree BST){if(!BST){return NULL;}else if(!BST->Left){return BST;}else{return FindMin(BST->Left);}
}Position FindMax(BinTree BST){if(BST){while(BST->Right){BST=BST->Right;}}return BST;
}BinTree Insert(ElementType X,BinTree BST){if(!BST){BST=(BinTree)malloc(sizeof(struct TreeNode));BST->Data=X;BST->Left=BST->Right=NULL;}else{if(X<BST->Data){BST->Left=Insert(X,BST->Left);}else if(X>BST->Data){BST->Right=Insert(X,BST->Right);}}return BST;
}BinTree Delete(ElementType X,BinTree BST){Position Tmp;if(!BST){printf("要删除的元素未找到");}else if(X<BST->Data){BST->Left=Delete(X,BST->Left);}else if(X>BST->Data){BST->Right=Delete(X,BST->Right);}else{if(BST->Left&&BST->Right){Tmp=FindMin(BST->Right);BST->Data=Tmp->Data;BST->Right=Delete(BST->Data,BST->Right);}else{Tmp=BST;if(!BST->Left){BST=BST->Right;}else if(!BST->Right){BST=BST->Left;}free(Tmp);}}return BST;
}

小白专场

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>typedef struct TreeNode *Tree;
struct TreeNode{int v;Tree Left,Right;int flag;
};Tree NewNode(int V){Tree T=(Tree)malloc(sizeof(struct TreeNode));T->v=V;T->Left=T->Right=NULL;T->flag=0;return T;
}Tree Insert(Tree T,int V){if(!T){T=NewNode(V);}else{if(V>T->v){T->Right=Insert(T->Right,V);}else{T->Left=Insert(T->Left,V);}}return T;
}Tree MakeTree(int N){Tree T;int i,V;scanf("%d",&V);T=NewNode(V);for(i=1;i<N;i++){scanf("%d",&V);T=Insert(T,V);}return T;
}int check(Tree T,int V){if(T->flag){if(V<T->v){return check(T->Left,V);}else if(V>T->v){return check(T->Right,V);}else{return 0;}}else{if(V==T->v){T->flag=1;return 1;}else{return 0;}}
}int Judge(Tree T,int N){int i,V,flag=0;scanf("%d",&V);if(V!=T->v){flag=1;}else{T->flag=1;}for(i=1;i<N;i++){scanf("%d",&V);if((!flag)&&(!check(T,V))){flag=1;}}if(flag){return 0;}else{return 1;}
}void ResetT(Tree T){if(T->Left){ResetT(T->Left);}if(T->Right){ResetT(T->Right);}T->flag=0;
}void FreeTree(Tree T){if(T->Left){FreeTree(T->Left);}if(T->Right){FreeTree(T->Right);}free(T);
}int main(){int N,L,i;Tree T;scanf("%d",&N);while(N){scanf("%d",&L);T=MakeTree(N);for(i=0;i<L;i++){if(Judge(T,N)){printf("Yes\n");	}else{printf("No\n");}ResetT(T);}FreeTree(T);scanf("%d",&N);}return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词