优先队列,二叉搜索树(非平衡),并查集


//
// Create by Running Photon on 2015-03-10
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define CLR(x) memset(x,0,sizeof x)
#define ll long long
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;
const int MOD=5e5+5;
/*
//优先队列
int heap[maxn], sz = 0;
void push(int x)
{
    int i = sz++;
    while(i > 0){
        int p = (i - 1) / 2;
        if(heap[p] <= x) break;
        heap[i] = heap[p];
        i = p;
    }
    heap[i] = x;
}
int pop()
{
    int ret = heap[0];
    int x = heap[--sz];
    int i = 0;
    while(i * 2 + 1 < sz){
        int a = i * 2 + 1, b = i * 2 + 2;
        if(b < sz && heap[b] < heap[a]) a = b;
        if(x <= heap[a]) break;
        heap[i] = heap[a];
        i = a;
    }
    heap[i] = x;
    return ret;
}
bool Empty()
{
    return sz == 0;
}
*/
//二叉搜索树
struct node
{
    int val;
    node *lch, *rch;
    node(int val = 0, node *lch = NULL, node *rch = NULL):
        val(val), lch(lch), rch(rch){}
};
node *Insert(node *p, int x)
{
    if(p == NULL){
        node *q = new node;
        q -> val = x;
        return q;
    }
    else{
        if(x < p -> val) p -> lch = Insert(p -> lch, x);
        else p -> rch = Insert(p -> rch, x);
        return p;
    }
}
bool Find(node *p, int x)
{
    if(p == NULL) return false;
    else if(x == p -> val) return true;
    else if(x < p -> val) return Find(p -> lch, x);
    else return Find(p -> rch, x);
}
node *Remove(node *p, int x)
{
    if(p == NULL) return NULL;
    else if(x < p -> val) return p -> lch = Remove(p -> lch, x);
    else if(x > p -> val) return p -> rch = Remove(p -> rch, x);
    else if(p -> lch == NULL){
        node *q = p -> rch;
        delete p;
        return q;
    }
    else if(p -> lch -> rch == NULL){
        node *q = p -> lch;
        q -> rch = p -> rch;
        delete p;
        return q;
    }
    else{
        node *q;
        for(q = p -> lch; q -> rch -> rch != NULL; q = q -> rch);
        node *r = q -> rch;
        q -> rch = r -> lch;
        r -> lch = p -> lch;
        r -> rch = p -> rch;
        delete p;
        return r;
    }
    return p;
}
/*
//并查集
int par[maxn], high[maxn];
void init(int n)
{
    for(int i = 0; i < n; i++) par[i] = i, high[i] = 0;
}
int Find_boot(int x)
{
    if(par[x] == x) return x;
    else return par[x] = Find_boot(par[x]);
}
void unite(int x, int y)
{
    x = Find_boot(x);
    y = Find_boot(y);
    if(x == y) return;
    if(high[x] < high[y]) par[x] = par[y];
    else{
        par[y] = par[x];
        if(high[x] == high[y]) high[x]++;
    }
}
bool same(int x, int y)
{
    return Find_boot(x) == Find_boot(y);
}
*/
int main()
{
#ifdef LOCAL
 freopen("in.txt","r",stdin);
 //freopen("out.txt","w",stdout);
#endif
 ios_base::sync_with_stdio(0);
 /*
    //优先队列测试
 push(3), push(5), push(1), push(21);
 while(!Empty())
    {
        printf("%d ",pop());
        printf("\n");
    }
    */
    //二叉搜索树测试
    node *root = NULL;
    root = Insert(root, 1);
    Insert(root, 2),Insert(root, 5),Insert(root, 3),Insert(root, 4),Insert(root, 100);
    for(int i = 0; i <= 100; i++){
        if(Find(root, i)) printf("%d ", i);
    }
    root = Remove(root, 1);
    for(int i = 0; i <= 100; i++){
        if(Find(root, i)) printf("%d ", i);
    }
    //并查集测试
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值