浙大2020年Mooc数据结构笔记–第三讲 树(中)
〇、前言
- 这几天开始跟着学数据结构,鉴于当初数据结构实在学的太弱,加之这项工作算是为大家之后复习、机试铺路。确实是一个迫切需要做的大规模工作。行胜于言,虽然从第二篇开始,坚持下去。
- 此文部分为自己写,部分参考网上内容。提前说一下哈。期待批评指正。
一、二叉搜索树
二、平衡二叉树
三、平衡二叉树的调整
四、是否为同一棵二叉搜索树
五、课后题
1、04-树4 是否同一棵二叉搜索树 (25分)
输入样例:
4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0
输出样例:
Yes
No
No
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct TreeNode *Tree;
struct TreeNode{
int v;
Tree Left,Right;
int flag; //flag用来标记出现顺序
};
Tree NewNode(int V){
Tree T = (Tree)malloc(sizeof(struct TreeNode));
T->Left = T->Right = NULL;
T->v = V;
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(int 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; //falg:0 表示不一致,1表示一致。
scanf("%d",&V);
if(V!=T->v){
flag = 1;
}else {
T->flag = 1;
}
for(int 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 ) /* 清除T中各结点的flag标记 */
{
if (T->Left) ResetT(T->Left);
if (T->Right) ResetT(T->Right);
T->flag = 0;
}
void FreeTree ( Tree T ) /* 释放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(int i = 0;i<L;i++){
if(Judge(T,N)){
printf("Yes\n");
}else{
printf("No\n");
}
ResetT(T); //清除T中的标记flag
}
FreeTree(T);
scanf("%d",&N);
}
return 0;
}
2、04-树5 Root of AVL Tree (25分)
Sample Input 1:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88
平衡二叉树的调整超详细解释+代码(这是一个超链接,点一下我你就什么都懂了)
#include <stdio.h>
#include <stdlib.h>
//#include <unistd.h>
#include<iostream>
typedef int ElementType;
typedef struct AVLNode* Position;
typedef Position AVLTree; /* AVL树类型 */
struct AVLNode {
ElementType Data; /* 结点数据 */
AVLTree Left; /* 指向左子树 */
AVLTree Right; /* 指向右子树 */
int Height; /* 树高 */
};
int Max(int a, int b)
{
return a > b ? a : b; //比较大小,获取更大的值
}
int GetHeight(AVLTree A) // 获得子树高度
{
int MaxH, HR, HL;
if (A) {
HL = GetHeight(A->Left);
HR = GetHeight(A->Right);
MaxH = (HL > HR) ? HL : HR;
return MaxH + 1; //当前树的高度相当于子树高度最大值+1
}
return -1; //如果当前树的左子树和右子树为空,即为叶子节点,则返回-1
}
AVLTree SingleLeftRotation(AVLTree A)
{
AVLTree B = A->Left;
A->Left = B->Right;
B->Right = A;
A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
B->Height = Max(GetHeight(B->Left), A->Height) + 1;
return B;
}
AVLTree SingleRightRotation(AVLTree A)
{
AVLTree B = A->Right;
A->Right = B->Left;
B->Left = A;
A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
A->Height = Max(GetHeight(B->Right), A->Height) + 1;
return B;
}
AVLTree DoubleLeftRightRotation(AVLTree A) //LR旋转
{
A->Left = SingleRightRotation(A->Left);
return SingleLeftRotation(A);
}
AVLTree DoubleRightLeftRotation(AVLTree A) //RL旋转 = LL旋转 + RR旋转(顺序是反的)
{
A->Right = SingleLeftRotation(A->Right);
return SingleRightRotation(A);
}
AVLTree Insert(AVLTree T, ElementType X)
{ /* 将X插入AVL树T中,并且返回调整后的AVL树 */
if (!T) { /* 若插入空树,则新建包含一个结点的树 */
T = (AVLTree)malloc(sizeof(struct AVLNode));
T->Data = X;
T->Height = 0;
T->Left = T->Right = NULL;
} /* if (插入空树) 结束 */
else if (X < T->Data) {
/* 插入T的左子树 */
T->Left = Insert(T->Left, X);
/* 如果需要左旋 */
if (GetHeight(T->Left) - GetHeight(T->Right) == 2)
if (X < T->Left->Data)
T = SingleLeftRotation(T); /* 左单旋 */
else
T = DoubleLeftRightRotation(T); /* 左-右双旋 */
} /* else if (插入左子树) 结束 */
else if (X > T->Data) {
/* 插入T的右子树 */
T->Right = Insert(T->Right, X);
/* 如果需要右旋 */
if (GetHeight(T->Left) - GetHeight(T->Right) == -2)
if (X > T->Right->Data)
T = SingleRightRotation(T); /* 右单旋 */
else
T = DoubleRightLeftRotation(T); /* 右-左双旋 */
} /* else if (插入右子树) 结束 */
/* else X == T->Data,无须插入 */
/* 别忘了更新树高 */
T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + 1;
return T;
}
int main()
{
int N, i;
ElementType t;
AVLTree T = NULL;
scanf("%d", &N);
for (i = 0; i < N; i++) {
scanf("%d", &t);
T = Insert(T, t);
}
if (T)
printf("%d", T->Data);
return 0;
}
3、04-树6 Complete Binary Search Tree (30分)![在这里插入图片描述](https://img-blog.csdnimg.cn/20200423223005206.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01pa2VzdXBlcl9ibG9n,size_16,color_FFFFFF,t_70)
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int arr[1000], rearr[1000];
int compare( const void* a, const void* b ){
return *(int*)a - *(int*)b;
}
//利用二叉树的性质:
//满二叉树第i层有 2^(i-1) 个结点,
//高为h的满二叉树有 2^h - 1 个结点(从1开始)
int getLeftLength(int n){
double h, x, L, t;
h = (double)(int)( log((double)n+1) / log(2.0) );
//h = floor( log((double)n+1) / log(2.0) );
x = n - pow(2.0, h) + 1 ;
t = pow(2.0, h - 1.0);
x = x < t ? x : t;
L = t - 1 + x;
return (int)L;
}
void solve( int left, int right, int root ){
//初始调用: solve(0, n-1, 0);
int n, L, leftRoot, rightRoot;
n = right - left + 1; //数组中的总个数
if(n == 0) return ; //递归退出的条件
L = getLeftLength(n); //计算出左子树的结点
rearr[root] = arr[left + L]; //将新的根结点放入新的数组
leftRoot = root * 2 + 1; //左孩子
rightRoot = leftRoot + 1; //右孩子
solve(left, left + L - 1, leftRoot);
solve(left + L + 1, right, rightRoot);
}
int main(){
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
qsort(arr, n, sizeof(int), compare);
solve(0, n-1, 0);
for(int i = 0; i < n; i++){
if( i != 0 ) printf(" ");
printf("%d", rearr[i]);
}
system("pause");
return 0;
}
4、04-树7 二叉搜索树的操作集 (30分)
#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 ); /* 先序遍历,由裁判实现,细节不表 */
void InorderTraversal( BinTree BT ); /* 中序遍历,由裁判实现,细节不表 */
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;
}
/* 你的代码将被嵌在这里 */
总结
树和图是数据结构中非常重要的高级数据结构,在后期的学习中应用很广,本次的二叉搜索树也是非常重要。树的相关代码用到递归比较多。迷迷糊糊,懵懵懂懂。