广度优先搜索与深度优先搜索

1.  广度优先代码

// BreadthSearch.h


/**
 * 广度优先搜索算法
 * 记录每一层的目录路径,检查该层的所有文件
 */

#ifndef _BREADTHSEARCH_H_
#define _BREADTHSEARCH_H_

#include <vector>
#include <xstring>
#include <Windows.h>
using namespace std;

class BreadthSearch
{
public:
	BreadthSearch();
	~BreadthSearch();

public:
	/**
	 * 初始化查询信息
	 * @aPos	搜索起始位置
	 * @aTarget	搜索目标名(支持)
	 * @Return	成功返回0
	 */
	int Construct(int aPos);

	void Close();

public:
	/**
	 * 开始搜索
	 */
	int StartSearch();

private:
	/**
	 * 判断一个路径是否是目录
	 * @Param	aPath	传入的路径
	 * 
	 */
	BOOL IsDirectory(const wchar_t *aPath);

	/**
	 * 搜索当前目录
	 * @Param       aDir    需要搜索的目录
	 */
	int SearchDircetory(wstring aDir);

public:
	// 本层文件夹路径集合,下层文件夹路径集合
	vector<wstring> iDirectoryPath0, iDirectoryPath1;

	DWORD iFilesCount;
	DWORD iDirCount;

private:
	HANDLE hFindFile;
};

#endif

// BreadthSearch.cpp


#include "BreadthSearch.h"

BreadthSearch::BreadthSearch()
:hFindFile(NULL)
,iFilesCount(0)
,iDirCount(0)
{

}

BreadthSearch::~BreadthSearch()
{

}

int BreadthSearch::Construct(int aPos)
{
	int errCode = -9002;
	// 查询所有盘符
	vector<wstring> drivers;
	wchar_t aDrivers[10240] = {0};
	DWORD len = GetLogicalDriveStrings(10240, aDrivers);
	wchar_t *pDrivers = aDrivers;
	DWORD driverCount = len / 4;

	// 筛选出可用盘符
	for (int i = 0; i < driverCount; ++i)
	{
		DWORD t = GetDriveType(pDrivers);
		switch (t)
		{
		case DRIVE_FIXED:
		case DRIVE_REMOVABLE:
			wstring driver = pDrivers;
			drivers.push_back(driver);
			break;
		}

		pDrivers += 4;
	}

	switch (aPos)
	{
	case -1:
		// 填充所有
		iDirectoryPath0 = drivers;
		errCode = 0;
		break;
	default:
		if (aPos <= (int)drivers.size())
		{
			iDirectoryPath0.push_back(drivers[aPos]);
			errCode = 0;
		}
		// 没找到,返回-9002
		errCode = -9002;
		break;
	}

	return errCode;
}

int BreadthSearch::StartSearch()
{
	int errCode = -9005;
	vector<wstring>::iterator iter;
	// 从根目录开始循环
	while (iDirectoryPath0.size() > 0)
	{
		for (iter = iDirectoryPath0.begin(); iter != iDirectoryPath0.end(); ++iter)
		{
			errCode = SearchDircetory(iter->c_str());
		}

		// 完成此轮循环
		iDirectoryPath0.clear();
		iDirectoryPath0 = iDirectoryPath1;
		iDirectoryPath1.clear();
	}

	return errCode;
}

int BreadthSearch::SearchDircetory(wstring aDir)
{
	int errCode = -9005;
	wstring fullPath0, fullPath1;
	if (aDir[aDir.length() - 1] != L'\\')
		aDir.append(L"\\");

	fullPath0 = aDir;
	
	aDir.append(L"*");
	WIN32_FIND_DATA findData;
	HANDLE findFile = FindFirstFile(aDir.c_str(), &findData);
	if (findFile == INVALID_HANDLE_VALUE)
		return -6005;
	
	fullPath1 = fullPath0;
	fullPath1.append(findData.cFileName);
	if (wcscmp(findData.cFileName ,L".") == 0 || wcscmp(findData.cFileName ,L"..") == 0)
	{
		// Do Nothing
	}
	else if (IsDirectory(fullPath1.c_str()))
	{
		
		iDirectoryPath1.push_back(fullPath1);
		++iDirCount;
	}
	else
	{
		++iFilesCount;
	}

	OutputDebugString(fullPath1.c_str());
	OutputDebugString(L"\n");

	while (FindNextFile(findFile, &findData))
	{
		fullPath1 = fullPath0;
		fullPath1.append(findData.cFileName);
		if (IsDirectory(fullPath1.c_str()))
		{
			if (wcscmp(findData.cFileName ,L".") == 0 || wcscmp(findData.cFileName ,L"..") == 0)
				continue;
			iDirectoryPath1.push_back(fullPath1);
			++iDirCount;
		}
		else
		{
			++iFilesCount;
		}

		OutputDebugString(fullPath1.c_str());
		OutputDebugString(L"\n");

	}

	return 0;
}

BOOL BreadthSearch::IsDirectory(const wchar_t *aPath)
{
	DWORD rlt = GetFileAttributes(aPath);
	if (rlt == FILE_ATTRIBUTE_DIRECTORY)
		return TRUE;
	else
		return FALSE;
}
// 测试代码


#include <iostream>
#include <Windows.h>
#include "BreadthSearch.h"
using namespace std;

int main()
{
	BreadthSearch search;
	search.Construct(0);
	DWORD t1 = GetTickCount();
	search.StartSearch();
	DWORD t2 = GetTickCount();

	DWORD second = (t2 - t1) / 1000;

	wcout<<L"File Count is : "<<search.iFilesCount<<endl;
	wcout<<L"Dir Count is : "<<search.iDirCount<<endl;
	wcout<<L"Time is : "<<second<<endl;

	getchar();
	return 0;
}

2. 深度优先搜索




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值