查找(一)

顺序查找:

#include<iostream>
using namespace std;
//串的定长存储表示
#define MAX_STRLEN 256
#define ERROR 0
#define OK 1
typedef int Status
#define MAX_ROW 100
#define MAX_SIZE 101
typedef int elemtype;
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)<(b))
#define LQ(a,b) ((a)<=(b))
#define EQ(!strcmp((a),(b)))
#define LT(a,b) (strcmp((a,(b)<0)
#define LQ(a,b) (strcmp((a),(b))<=0)
//顺序查找
#define MAX_SIZE 100
typedef struct RecType
{
    KeyType key;
}Rectype;
typedef struct SSTable
{
    RecType elem[MAX_SIZE];   //顺序表
    int length;
}SSTable;
int Seq_Search(SStable ST, KeyType key)
{
    int p;
    ST.elem[0].key = key;
    for (p = ST.length; !EQ(ST.elem[p].key, key); p--)
        return p;
}

折半查找:

//折半查找  前提条件  有序
int Bin_Search(SStable ST, KeyType key)
{
    int Low = 1, High = ST.length, Mid;
    while ((Low < High)
    {
        Mid = (Low + High) / 2;
        if (EQ(ST.elem[Mid].key, key))
            return (Mid);
        else if (LT(ST.elem[Mid].key, key))
            Low = Mid + 1else High = Mid - 1;
    }
    return 0;     //查找失败
}

分块查找:

//分块查找  索引查找   快间有序  块内无序
typedef struct IndexType
{
    KeyType maxkey;   //快中的中最大关键字
    int startpos;   //快的起始指针
}Index;
int Block_search(RecType ST[], Index ind[], KeyType key, int n, int b)
//在分块查找表中 查找关键字为key的记录
//表常为 n,块数为b
{
    int i = 0, j, k;
    while ((i < b) && LT(ind[i].maxkey, key)) i++;
    if (i > b) { cout << "Not Found!" << endl; return 0; }
    j = ind[i].startpos;
    while ((j < n) && LQ(ST[j].key, ind[i].maxkey))
    {
        if (EQ(ST[j].key, key))  break;
        j++;
    }   //块内查找
    if (j > n || !EQ(ST[j].key, key))
    {
        j = 0; cout << "Not Found" << endl;
    }
    return j;
}

Fibonacci查找:

//Fibonacci查找
int fib(int n)
{
    int i, f, f0 = 0, f1 = 1;
    if (n == 0) return 0;
    if (n == 1) return 1;
    for (i = 2; i <= n; i++)
    {
        f = f0 + f1; f0 = f1; f1 = f;
    }
    return f;
}
int Fib_search(RecType ST[], KeyType key, int n)
{
    int Low = 1, High, Mid, f1, f2;
    High = n; f1 = fib(n - 1); f2 = fib(n - 2);
    while (Low <= High)
    {
        Mid = Low + f1 - 1;
        if (EQ(ST.[Mid].key, key)) return Mid;
        else if (LT(key, ST.[Mid].key))
        {
            High = Mid - 1; f2 = f1 - f2; f1 = f1 - f2;
        }
        else {
            Low = Mid + 1; f1 = f1 - f2; f2 = f2 - f1;
        }
    }
    return 0;
}

二叉树:

//二叉树排序
//左子树 所有的节点值都小于根节点的值
//右子树的值都大于根节点的值
//中序遍历 得到递增序列
typedef struct Node
{
    KeyType key;
    Node* Lchild, * Rchild;
}BSTNode;
BSTNode* BST_Search(BSTNode* T, KeyType key)
{
    if (T == NULL) return NULL;
    else
    {
        if (EQ(T->key, key)) return T;
        else if (LT(key, T->key))
            return(BST_Search(T->Lchild, key));
        else return(BST_Search(T->Rchild, key));
    }
}
//非递归算法
BSTNode* BST_Search(BSTNode* T, KeyType key)
{
    BSTNode p = T;
    while (p != NULL && EQ(p->key, key))
    {
        if (LT(key, p->key))p = p->Lchild;
        else p = p->Rchild;
    }
    if (EQ(p->key, key)) return p;
    else return NULL;
}
//BST树的插入
//递归算法
void Insert_BST(BSTNode* T, KetType key)
{
    BSTNode* x;
    x = (BSTNode*)malloc(sizeof(BSTNode));
    x->key = key; x->Lchild = x->Rchild = NULL;
    if (T == NULL) T = x;
    else {
        if (EQ(T->key, x->key)) return;    //已有节点
        else if (LT(x->key, T->key))
            Insert_BST(T->Lchild, key);
        else Insert_BST(T->Rchild, key);
    }
}
//非递归算法
void Insert_BST(BSTNode* T, KeyType key)
{
    NSTNode* x, * p, * q;
    x = (BSTNode*)malloc(sizeof(BSTNode));
    x->key = key; x->Lchild = x->Rchild = NULL;
    if (T == NULL) T = x;
    else
    {
        p = T;
        whle(p != NULL)
        {
            if (EQ(p->key, x->key)) retun;
            q = p;    // q作为p的父节点
            if (LT(x->key, p->key)) p = p->Lchild;
            else p = p->Rchild;
        }
        if (LT(x->key, q->key))  q->Lchild = x;
        else q->Rchild = x;
    }
}

利用二叉树进行相应的插入删除操作:

//利用BST树的插入操作 可以从空树开始逐个插入每个节点
#define ENDKEY 65535
BSTNode* create_BST()
{
    KeyType key;
    BSTNode* T = NULL;
    cin >> key;
    while (key != ENDKEY)
    {
        Insert_BST(T, key);
        cin >> key;
    }
    return T;
}
//BST的删除操作
void Delete_BST(BSTNode* T, KeyTye key)
{
    BSTNode* p = T, * f = NULL, * q, * s;
    while (p != NULL && !EQ(p->key, key))
    {
        f = p;
        if (LT(key, p->key))p = p->Lchild;   //搜索左子树
        else p = p->Rchild;       //搜索右子树
    }
    if (p == NULL) return;    //没有要删除大的节点
    s = p;
    if (p->Lchild != NULL && p->Rchild != NULL)
    {
        f = p; s = p->Lchild;
        while(s->Rchild!=NULL)
        {
            f = s; s = s->Rchild;   //左右子树都不为空,找左子树的最右边节点
        }
        p->key = s->key; p->otherinfo = s->otherinfo;   //用节点s的内容替换p内容
    }
    if (s->Lchild != NULL)   //若s有左子树 右子树为空
        q = s->Lchild;
    else q = s->Rchild;
    if (f == NULL) T = q;
    else if (f->Lchild == s)f->Lchild = q;
    else f->Rchild = q;
    free(s);
}

 

待续..........

posted @ 2020-04-14 21:47  为红颜  阅读(82)  评论(0编辑  收藏