DS树--找出直系亲属

题目描述

如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A,B的grandchild,如果A,B是C的(外)曾祖父,曾祖母,则A,B是C的great-grandparent,C是A,B的great-grandchild,之后再多一辈,则在关系上加一个great-

输入

输入包含多组测试用例,每组用例首先包含2个整数n(0<=n<=26)和m(0 当n和m为0时结束输入。

输出

如果询问的2个人是直系亲属,请按题目描述输出2者的关系,如果没有直系关系,请输出-。
具体含义和输出格式参见样例.

输入样例

DS树--找出直系亲属_第1张图片

 解题思路:

先建树,然后从位于上面的那个data较小的node开始往下找data较大的node。

思路说起来简单,但lookfor()函数和lookfor2()函数可着实费了我心思,尤其是lookfor2()的返回值。

#include 
using namespace std;
class BiTreeNode {
public:
	char  data;					//数据域
	BiTreeNode* leftChild, * rightChild, * parent;
	BiTreeNode() :leftChild(NULL), rightChild(NULL), parent(NULL) {}
	~BiTreeNode() {}
};

class BiTree {
private:
	BiTreeNode* root;	//根结点指针
public:
	void Init() {
		root = new BiTreeNode();
	}
	void link(string str) {
		BiTreeNode* newroot = lookfor(root, str[0]);
		newroot->leftChild = new BiTreeNode();
		if (newroot->leftChild->data != '-')
			newroot->leftChild->data = str[1];
		newroot->rightChild = new BiTreeNode();
		if (newroot->rightChild->data != '-')
			newroot->rightChild->data = str[2];
	}
	void createTree(string ss) {
		root->data = ss[0];
		root->leftChild = new BiTreeNode();
		if (root->leftChild->data != '-')
			root->leftChild->data = ss[1];
		root->rightChild = new BiTreeNode();
		if (root->rightChild->data != '-')
			root->rightChild->data = ss[2];
	}
	BiTreeNode* lookfor(BiTreeNode*& root, char ch) {
		if (root != NULL) {
			if (root->data == ch) {
				return root;
			}
			else {
				BiTreeNode* i = lookfor(root->leftChild, ch);
				if (i != NULL) return i;
				BiTreeNode* j = lookfor(root->rightChild, ch);
				if (j != NULL) return j;
				return NULL;
			}
		}
		else
			return NULL;
	}
	int lookfor2(BiTreeNode*& node, char ch, int i) {
		if (node != NULL) {
			if (node->data == ch) {
				return i;
			}
			int j = lookfor2(node->leftChild, ch, i + 1);
			if (j != -999) return j;
			int k = lookfor2(node->rightChild, ch, i + 1);
			if (k != -999) return k;
			if (i == 0)
				return -1;
			else
				return -999;
		}
		else
			return -999;
	}
	void output(char a, char b) {
		char xiao, da;
		int tag;
		if (a > b)
			xiao = b, da = a, tag = 0;
		else
			xiao = a, da = b, tag=1;
		int depth;
		BiTreeNode* xiaonode = lookfor(root, xiao);
		depth = lookfor2(xiaonode, da, 0);
		if (depth == -1) {
			cout << "-" << endl;
		}
		else if (depth == 1) {
			if(tag==0)
			cout << "parent" << endl;
			else
			cout << "child" << endl;
		}
		else if (depth == 2) {
			if (tag == 0)
				cout << "grandparent" << endl;
			else
				cout << "grandchild" << endl;
		}
		else if (depth >= 3) {
			for (int x = 3; x <= depth; x++) {
				cout << "great-";
			}
			if (tag == 0)
				cout << "grandparent" << endl;
			else
				cout << "grandchild" << endl;
		}
	}
};

//主函数
int main()
{
	int n, m;
	string bulid;
	string search;
	while (cin >> n >> m && n != 0 && m != 0) {
		BiTree b;
		b.Init();
		for (int i = 0; i < n; i++) {
			cin >> bulid;
			if (i == 0) {
				b.createTree(bulid);
			}
			else {
				b.link(bulid);
			}
		}
		for (int i = 0; i < m; i++) {
			cin >> search;
			b.output(search[0], search[1]);
		}
	}
	return 0;
}

 

你可能感兴趣的:(Programming,Questions,c++,数据结构,算法)