码迷,mamicode.com
首页 > 其他好文 > 详细

编程题小练习 03

时间:2014-12-10 01:53:12      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:c++   算法   

求二叉树的深度和宽度

求二叉树的深度和宽度,深度为最深的层数,宽度为最宽的层宽度;

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

typedef struct tagBiNode {
	char data;
	struct tagBiNode *left;
	struct tagBiNode *right;
} BiNode;

int getWidth(BiNode* head) {
	if (head == NULL)
		return 0;
	queue<BiNode*> q;
	q.push(head);
	int width = 1, current = 1;
	while (!q.empty()) {
		while (current--) {
			if (q.front()->left != NULL)
				q.push(q.front()->left);
			if (q.front()->right != NULL)
				q.push(q.front()->right);
			q.pop();
		}
		current = q.size();
		width = max(width, current);
	}
	return width;
}

int getHeight(BiNode* head) {
	if (head == NULL)
		return 0;
	return max(getHeight(head->left), getHeight(head->right)) + 1;
}

int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth,
		unsigned int *pulHeight) {
	*pulWidth = getWidth(&head);
	*pulHeight = getHeight(&head);
	return 0;
}

int main() {
	BiNode dataa = { 'a', NULL, NULL };
	BiNode datab = { 'b', NULL, NULL };
	BiNode datac = { 'c', NULL, NULL };
	BiNode datad = { 'd', NULL, NULL };
	BiNode datae = { 'e', NULL, NULL };
	BiNode dataf = { 'f', NULL, NULL };
	BiNode datag = { 'g', NULL, NULL };
	BiNode datah = { 'h', NULL, NULL };
	BiNode datai = { 'i', NULL, NULL };
	BiNode dataj = { 'j', NULL, NULL };
	BiNode datak = { 'k', NULL, NULL };
	BiNode datal = { 'l', NULL, NULL };
	BiNode datam = { 'm', NULL, NULL };
	BiNode datan = { 'n', NULL, NULL };
	BiNode datao = { 'o', NULL, NULL };
	BiNode datap = { 'p', NULL, NULL };

	dataa.left = &datab;
	dataa.right = &datac;
	datab.left = &datad;
	datab.right = &datae;
	datac.left = &dataf;
	datac.right = &datag;
	datad.left = &datah;
	datad.right = &datai;
	datae.left = &dataj;
	datae.right = &datak;
	dataf.left = &datal;
	dataf.right = &datam;
	datag.left = &datan;
	datag.right = &datao;
	datah.left = &datap;

	unsigned int ulWidth = 0;
	unsigned int ulHeight = 0;
	GetBiNodeInfo(dataa, &ulWidth, &ulHeight);
	cout << ulWidth << endl;  // 8
	cout << ulHeight << endl;  // 5
}

内存文件系统

内存文件系统,模拟文件和文件夹的创建,移动,与删除;根目录为root,缺省存在,目录名和文件名全局唯一;

#include <iostream>
#include <string>
#include <map>
using std::map;
using std::string;
class Dir;
class File;

class Dir {
public:
	Dir(const string dirName) :
			dirName(dirName) {
		parent = NULL;
	}
public:
	Dir *parent;
	string dirName;
	map<string, Dir*> subDirs;
	map<string, File*> subFiles;
};

class File {
public:
	File(const string fileName) :
			fileName(fileName) {
		parent = NULL;
	}
public:
	Dir *parent;
	string fileName;
};

Dir *root = new Dir("root");
map<string, Dir*> dirs;
map<string, File*> files;

Dir* findDir(const string& dirName) {
	if (dirName == "root")
		return root;
	map<string, Dir*>::iterator it = dirs.find(dirName);
	if (it == dirs.end())
		return NULL;
	return it->second;
}

File* findFile(const string& fileName) {
	map<string, File*>::iterator it = files.find(fileName);
	if (it == files.end())
		return NULL;
	return it->second;
}

Dir* removeFile(const string fileName) {
	File *pFile = findFile(fileName);
	if (pFile == NULL)
		return NULL;
	Dir *parent = pFile->parent;
	files.erase(fileName);
	return parent;
}

Dir* removeDir(const string dirName) {
	Dir *pDir = findDir(dirName);
	if (pDir == NULL)
		return NULL;
	if (!pDir->subDirs.empty()) {
		map<string, Dir*>::iterator iterDir = pDir->subDirs.begin();
		for (; iterDir != pDir->subDirs.end(); ++iterDir) {
			removeDir(iterDir->first);
		}
	}
	map<string, File*>::iterator iterFile = pDir->subFiles.begin();
	for (; iterFile != pDir->subFiles.end(); ++iterFile) {
		removeFile(iterFile->first);
	}
	Dir *parent = pDir->parent;
	delete pDir;
	dirs.erase(dirName);
	return parent;
}

int CreateDir(const char * ParentDirName, const char * DirName) {
	string parentDirName = ParentDirName;
	Dir *parentDir = findDir(parentDirName);
	if (parentDir == NULL)
		return -1;
	string dirName = DirName;
	if (findDir(dirName) != NULL)
		return -1;
	Dir *newDir = new Dir(dirName);
	parentDir->subDirs[dirName] = newDir;
	newDir->parent = parentDir;
	dirs[dirName] = newDir;
	return 0;
}

void DeleteDir(const char * DirName) {
	string dirName = DirName;
	Dir *parent = removeDir(dirName);
	if (parent != NULL) {
		parent->subDirs.erase(dirName);
	}
	return;
}

int MoveDir(const char * SrcDirName, const char * DestDirName) {
	string src = SrcDirName;
	string des = DestDirName;
	Dir *srcDir = findDir(src);
	Dir *desDir = findDir(des);
	if (srcDir == NULL || desDir == NULL) {
		return -1;
	}
	if (srcDir->parent == desDir) {
		return -1;
	}
	Dir *desDirCheck = desDir;
	while (desDirCheck != root) {
		if (desDirCheck == srcDir) {
			return -1;
		}
		desDirCheck = desDirCheck->parent;
	}
	Dir *srcParent = srcDir->parent;
	srcParent->subDirs.erase(srcDir->dirName);
	srcDir->parent = desDir;
	desDir->subDirs[srcDir->dirName] = srcDir;
	return 0;
}

int CreateFile(const char * DirName, const char * FileName) {
	string fileName = FileName;
	File *pFile = findFile(fileName);
	if (pFile != NULL) {
		return -1;
	}
	string dirName = DirName;
	Dir *pDir = findDir(dirName);
	if (pDir == NULL) {
		return -1;
	}
	pFile = new File(fileName);
	files[fileName] = pFile;
	pFile->parent = pDir;
	pDir->subFiles[fileName] = pFile;
	return 0;
}

void DeleteFile(const char * FileName) {
	string fileName = FileName;
	Dir *parent = removeFile(fileName);
	if (parent != NULL) {
		parent->subFiles.erase(fileName);
	}
	return;
}

unsigned int GetFileNum(const char * DirName) {
	string dirName = DirName;
	Dir *pDir = findDir(dirName);
	if (pDir == NULL) {
		return 0;
	}
	unsigned int fileNum = pDir->subFiles.size();
	map<string, Dir*>::iterator it = pDir->subDirs.begin();
	for (; it != pDir->subDirs.end(); ++it) {
		fileNum += GetFileNum(it->first.c_str());
	}
	return fileNum;
}

void Clear(void) {
	dirs.clear();
	files.clear();
	root->subDirs.clear();
	root->subFiles.clear();
	return;
}

int main() {
	CreateFile("root", "hi1");
	CreateFile("root", "hi2");
	CreateFile("root", "hi3");
	CreateDir("root","a1");
	CreateFile("a1","h4");
	CreateFile("a1","h5");
	CreateFile("a1","h6");
	CreateDir("a1","b1");
	CreateFile("b1","h7");
	std::cout << GetFileNum("root") << "\n"; // 7
	std::cout << GetFileNum("a1") << "\n";  // 4
	CreateDir("root", "a2");
	CreateFile("a2","hi8");
	MoveDir("a2","b1");
	std::cout << GetFileNum("a1") << "\n";  // 5
	removeDir("a1");
	std::cout << GetFileNum("root") << "\n";     //3
	DeleteFile("hi3");
	std::cout << GetFileNum("root") << "\n";     //2

}

编程题小练习 03

标签:c++   算法   

原文地址:http://blog.csdn.net/thisinnocence/article/details/41835813

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!