数据结构之折半查找

接下来的几篇博客都是关于查找的;主要有顺序查找;折半查找(应用于有序表);二叉排序树查找;哈希查找;

排序直接的数据结构介绍;

        顺序查找和折半查找是顺序表存储;二叉排序树是二叉树存储;哈希查抄是类图(和图的邻接表存储相似);

 

头文件(Search.h);

# ifndef _SORT_

typedef int KeyType;

typedef struct
{
	KeyType key;
}DataType;

# endif

头文件(SeqList.h);

# ifndef _SEQLIST_
# define _SEQLIST_

# include <iostream>
# include "Search.h"//引用这里的DataType;
using namespace std;

# define MAXSIZE 64

typedef struct
{
	DataType data[MAXSIZE];
	int length;
}SeqList, * PSeqList;

typedef struct Node
{
	DataType elem;
	struct Node * lchild;
	struct Node * rchild;
}BSTree, * PBSTree;

//线性表查找基本准备;
PSeqList Init_SeqList(void);
bool Full_SeqList(PSeqList p);
int Push_SeqList(PSeqList p, KeyType keyValue);
void Traversal_SeqList(PSeqList p);
ostream & operator<<(ostream & os, DataType dataValue);

//基于线性表的查找方法;

//顺序查找;
int SeqSearch(PSeqList p, KeyType key);

//折半查找;
int BinSearch(PSeqList p, KeyType key);


# endif

实现文件(SeqList.cpp);

# include "SeqList.h"

PSeqList Init_SeqList(void)
{
	PSeqList p = (PSeqList)malloc(sizeof(SeqList));

	if (NULL != p)
	{
		p->length = 0;
		return p;
	}
	else
	{
		cout << "Memory allocate is error! " << endl;
		system("pause");
		exit(0);
	}
}


bool Full_SeqList(PSeqList p)
{
	if (p->length >= MAXSIZE)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int Push_SeqList(PSeqList p, KeyType keyValue)
{
	if (Full_SeqList(p))
	{
		cout << "SeqList is full! " << endl;

		return -1;
	}

	p->data[p->length].key = keyValue;
	p->length++;

	return 0;
}

void Traversal_SeqList(PSeqList p)
{
	for (int i = 0; i < p->length; i++)
	{
		cout << p->data[i] << " ";
	}
	cout << endl;

	return;
}

ostream & operator<<(ostream & os, DataType dataValue)
{
	cout << dataValue.key;

	return os;
}

//基于线性表的查找方法;;

//顺序查找;
int SeqSearch(PSeqList p, KeyType key)
{
	int i = 0;
	p->data[p->length].key = key;
	while (p->data[i].key != key)//使用这种带前哨站的算法必须是非满表;不然出错;
	{
		i++;
	}

	if (i == p->length)
	{
		return -1;
	}
	else
	{
		return i;
	}

	//for (int i = 0; i < p->length; i++)
	//{
	//	if (p->data[i].key == key)
	//	{
	//		return i;
	//	}
	//}

	return -1;
}

//折半查找;
int BinSearch(PSeqList p, KeyType key)
{
	int low = 0;
	int mid = 0;
	int high = p->length - 1;

	while (low <= high)
	{
		mid = (low + high) / 2;

		if (key == p->data[mid].key)
		{
			return mid;
		}
		else if (key > p->data[mid].key)
		{
			low = mid + 1;
		}
		else
		{
			high = mid - 1;
		}
	}

	return -1;
}

主函数(Main.cpp);

# include "SeqList.h"

int main(int argc, char ** argv)
{
	PSeqList p = Init_SeqList();

	for (int i = 0; i < 10; i++)
	{
		Push_SeqList(p, i + 1);
	}

	cout << "------------------------SeqList Search------------------------" << endl;
	Traversal_SeqList(p);
    
    //这里是测试区;可以测试各种基于线性表的存储结构;
    //后面的就不一一改了;都是一样的; 改一个函数名就可以了;
	cout << "Your search position is : " << SeqSearch(p, 0) << endl;
	cout << "Your search position is : " << SeqSearch(p, 1) << endl;
	cout << "Your search position is : " << SeqSearch(p, 5) << endl;
	cout << "Your search position is : " << SeqSearch(p, 10) << endl;
	cout << "Your search position is : " << SeqSearch(p, 11) << endl << endl;


	cout << "------------------------TreeList Search------------------------" << endl;

    //这里是测试区;可以测试各种基于二叉树的存储结构;
	PBSTree pt = NULL;
	PBSTree pp = NULL;
	BSTreeInsert(&pt, 34);
	BSTreeInsert(&pt, 18);
	BSTreeInsert(&pt, 76);
	BSTreeInsert(&pt, 13);
	BSTreeInsert(&pt, 25);
	BSTreeInsert(&pt, 52);
	BSTreeInsert(&pt, 82);
	BSTreeInsert(&pt, 20);
	BSTreeInsert(&pt, 67);
	BSTreeInsert(&pt, 91);
	BSTreeInsert(&pt, 58);
	BSTreeInsert(&pt, 73);

	InOrder(pt);
	cout << endl;

	if (NULL != (pp = BSTreeSearch(pt, 13)))
	{
		cout << pp->elem.key << endl;
	}

	BSTreeDelete(&pt, 34);

	InOrder(pt);
	cout << endl;

	cout << "------------------------Hash Search------------------------" << endl;
	
    //这里是测试区;可以测试哈希表的存储结构;
	PHashTable pht = CreateHashTable();
	HashTableInsert(pht, 26);
	HashTableInsert(pht, 38);
	HashTableInsert(pht, 73);
	HashTableInsert(pht, 21);
	HashTableInsert(pht, 54);
	HashTableInsert(pht, 35);
	HashTableInsert(pht, 167);
	HashTableInsert(pht, 32);
	HashTableInsert(pht, 7);
	HashTableInsert(pht, 223);
	HashTableInsert(pht, 62);

	HashTableTraversal(pht);


	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值