一、二叉查找树(BST)
#include<bits/stdc++.h>
using namespace std;
typedef struct node* Bintree;
struct node
{
int data;
Bintree left;
Bintree right;
};
void search(Bintree BST,int x)
{
if(BST==NULL)
{
printf("search failed\n");
return;
}
if(x==BST->data)
printf("success!\n");
else if(x<BST->data)
search(BST->left,x);
else search(BST->right,x);
}
void insert(Bintree &BST,int x)
{
if(BST==NULL)
{
Bintree newNode=(Bintree)malloc(sizeof(struct node));
newNode->data=x;
newNode->left=NULL;
newNode->right=NULL;
}
if(x==BST->data) return;
else if(x<BST->data)
insert(BST->left,x);
else
insert(BST->right,x);
}
Bintree create(int data[],int n)
{
//建树,新建一个根结点
Bintree BST=NULL;
for(int i=0;i<n;i++)
{
insert(BST,data[i]);
}
return BST;
}
//二叉查找树的删除
Bintree FindMin(Bintree BT)
{
if(BT)
{
while(BT->right!=NULL)
{
BT=BT->right;
}
}
return BT;
}
Bintree Delete(Bintree &BST,int x)
{
Bintree temp;
if(BST==NULL)
printf("未找到");
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)
{
temp=FindMin(BST->right);
BST->data=temp->data;
BST->right=Delete(BST->right,BST->data);
}
else if(BST->left==NULL)
{
temp=BST;
BST=BST->right;
free(BST);
}
else
{
temp=BST;
BST=BST->left;
free(BST);
}
}
}
return BST;
}
PATA1043 Is it a Binary Search Tree
#include<stdio.h>
#include<vector>
using namespace std;
typedef struct Node *Bintree;
struct Node
{
int data;
Bintree left, right;
};
void insert(Bintree &root, int x)
{
if (root == NULL)
{
root = new Node;
root->data = x;
root->left = NULL;
root->right = NULL;
return;
}
if (x < root->data)
insert(root->left, x);
else if (x >= root->data)
insert(root->right, x);
}
void preOrder(vector<int> &vi, Bintree root)
{
if(root==NULL) return;
vi.push_back(root->data);
preOrder(vi,root->left);
preOrder(vi,root->right);
}
void preOrderMirror(vector<int> &vi,Bintree root)
{
if(root==NULL) return;
vi.push_back(root->data);
preOrderMirror(vi,root->right);
preOrderMirror(vi,root->left);
}
void postOrder(vector<int> &vi,Bintree root)
{
if(root==NULL) return;
postOrder(vi,root->left);
postOrder(vi,root->right);
vi.push_back(root->data);
}
void postOrderMirror(vector<int> &vi,Bintree root)
{
if(root==NULL) return;
postOrderMirror(vi,root->right);
postOrderMirror(vi,root->left);
vi.push_back(root->data);
}
vector<int> origin,pre,preM,post,postM;
int main()
{
int n,data;
scanf("%d",&n);
int i;
Bintree root=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&data);
origin.push_back(data);
insert(root,data);
}
preOrder(pre,root);
preOrderMirror(preM,root);
postOrder(post,root);
postOrderMirror(postM,root);
if(origin==pre)
{
printf("YES\n");
for(i=0;i<n;i++)
{
printf("%d",post[i]);
if(i<post.size()-1) printf(" ");
}
}
else if(origin==preM)
{
printf("YES\n");
for(i=0;i<n;i++)
{
printf("%d",postM[i]);
if(i<postM.size()-1) printf(" ");
}
}
else printf("NO\n");
}
二、并查集
定义: UNION,FIND,SET
合并:合并两个集合
查找:判断两个元素是否在同一集合
int father[N];
如果father[i]==i 说明元素i是集合的根结点
#include<bits/stdc++.h>
using namespace std;
const int maxn=100;
int father[maxn];
//查找根结点
int findFather(int x)
{
while(x!=father[x])
{
x=father[x];
}
return x;
}
//合并
//先判断这两个元素是否属于同一个集合,只有当两个元素属于不同集合时才合并,
//而合并的过程一般是把其中一个集合的根结点父亲指向另一个集合的根结点
void Union(int a,int b)
{
int faA=findFather(a);
int faB=findFather(b);
if(faA!=faB)
{
father[faA]=faB;
}
}
//路径压缩后查找根结点
int findFather2(int x)
{
int a=x;
while(x!=father[x])
{
x=father[x];
}
// x已经是根结点
while(a!=father[a])
{
int z=a;
a=father[a];
father[z]=x;
}
return x; //返回根结点
}
void init(int n)
{
for(int i=0;i<n;i++)
{
father[i]=i;
}
}
例题:好朋友
p332
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int father[maxn];
bool isRoot[maxn];
//查找根结点
int findFather(int x)
{
while (x != father[x])
{
x = father[x];
}
return x;
}
//合并
//先判断这两个元素是否属于同一个集合,只有当两个元素属于不同集合时才合并,
//而合并的过程一般是把其中一个集合的根结点父亲指向另一个集合的根结点
void Union(int a, int b)
{
int faA = findFather(a);
int faB = findFather(b);
if (faA != faB)
{
father[faA] = faB;
}
}
//路径压缩后查找根结点
int findFather2(int x)
{
int a = x;
while (x != father[x])
{
x = father[x];
}
// x已经是根结点
while (a != father[a])
{
int z=a;
a=father[a];
father[z]=x;
}
return x; //返回根结点
}
void init(int n)
{
for (int i = 1; i <= n; i++)
{
father[i] = i;
isRoot[i]=false;
}
}
int main()
{
int n,m,a,b;
scanf("%d%d",&n,&m);
init(n);
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
Union(a,b);
}
for(int i=1;i<=n;i++)
{
isRoot[findFather2(i)]=true;
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans+=isRoot[i];
}
printf("%d\n",ans);
return 0;
}