首页 > 代码库 > 编程题小练习 03

编程题小练习 03

求二叉树的深度和宽度

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

#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 = http://www.mamicode.com/SrcDirName;>

编程题小练习 03