天天看點

【二叉樹】47 二叉樹:查找x

問題描述 :

目的:使用C++模闆設計并逐漸完善二叉樹的抽象資料類型(ADT)。

内容:

(1)請參照連結清單的ADT模闆,設計二叉樹并逐漸完善的抽象資料類型。(由于該環境目前僅支援單檔案的編譯,故将所有内容都集中在一個源檔案内。在實際的設計中,推薦将抽象類及對應的派生類分别放在單獨的頭檔案中。參考教材、課件,以及網盤中的連結清單ADT原型檔案,自行設計二叉樹的ADT。)

注意:二叉樹ADT的基本操作的算法設計很多要用到遞歸的程式設計方法。

(2)基本操作8:在二叉樹的二叉連結清單存儲形式建立的基礎上,使用遞歸的程式設計方法,設計并完成查找二叉樹中元素值為x的結點的位置的算法。完成後将其加入到二叉樹的ADT基本操作集中。

初始條件:二叉樹T存在,x是T中某個結點。

操作結果:若x是T的結點,則傳回指向它的指針(查找成功,由于OJ的測試環境,顯示TRUE);否則傳回NULL(查找失敗,顯示FALSE)。

參考函數原型:

(1)查找結點的位置(外殼部分,使用者函數)

//查找結點的位置(外殼) 

template<class ElemType>

BinaryTreeNode<ElemType> * Location(BinaryTree<ElemType> &T, ElemType &x);

(2)查找結點的位置(遞歸部分,成員函數)

//查找結點的位置(遞歸) 

template<class ElemType>  //指針location為查找結果

void BinaryTree<ElemType>::Location_Cursive( BinaryTreeNode<ElemType> * root, const ElemType &x, BinaryTreeNode<ElemType> * &location );

輸入說明 :

第一行:表示無孩子或指針為空的特殊分隔符

第二行:二叉樹的先序序列(結點元素之間以空格分隔)

第三行:元素值x

輸出說明 :

第一行:查找成功:TRUE

              查找失敗:顯示 FALSE

輸入範例 :

#

A B # C D # # E # # F # G # H # #

C

輸出範例 :

TRUE

解題代碼:

// tree.cpp : 此檔案包含 "main" 函數。程式執行将在此處開始并結束。
//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <sstream>
#include <stack>
#include <map>
#include <ctime>
#include <array>
#include <set>
using namespace std;
vector<string> departString_string(string data)
{
	vector<int> back_part;//output type
	int i, j;
	vector<string> part;
	string A_part;
	stringstream room;
	room.str(data);
	while (room >> A_part)
		part.push_back(A_part);
	return part;
}
//————————————————
//版權聲明:本文為CSDN部落客「systemyff」的原創文章,遵循CC 4.0 BY - SA版權協定,轉載請附上原文出處連結及本聲明。
//原文連結:https ://blog.csdn.net/u014377763/article/details/113845555
template<class ElemType>
struct tree_point {
	ElemType data;//資料
	struct tree_point* l_child, * r_child;//左、右孩子指針
};
template<class ElemType>
class BinaryTree {
private:
	vector<tree_point<ElemType>*> outlist;
	tree_point<ElemType>* root;   // 頭指針
public:
	BinaryTree() :root(NULL)
	{
		//無參數的構造函數
	}
	~BinaryTree()
	{
		//析構函數
	}
	void BinaryTree_fron(vector<ElemType> lis, ElemType nut)
	{
		stack<tree_point<ElemType>*> s;
		tree_point<ElemType>* p_Parent = NULL,* p_Child = NULL;
		int i = 0;
		int flag = 0;//控制左右
		p_Parent = new tree_point<ElemType>;
		p_Parent->data = lis[i];
		p_Parent->l_child = p_Parent->r_child = NULL;
		s.push(p_Parent);
		root = p_Parent;
		i = 1;
		flag = 0;
		while (!s.empty())
		{
			if (lis[i] != nut)
			{
				p_Parent = new tree_point<ElemType>;
				p_Parent->data = lis[i];
				p_Parent->l_child = p_Parent->r_child = NULL;
				if (flag == 0)
				{
					p_Child = s.top();
					p_Child->l_child = p_Parent;
				}
				else if (flag == 1)
				{
					p_Child = s.top();
					s.pop();
					p_Child->r_child = p_Parent;
				}
				s.push(p_Parent);
				flag = 0;
			}
			else
			{
				if (flag == 0)
					flag = 1;
				else if (flag == 1)
					s.pop();
			}
			i++;
		}
	}
	
	tree_point<ElemType>* get_root()
	{
		return root;
	}
	void qianxu(tree_point<ElemType>* t)
	{
		outlist.push_back(t);
		if (t->l_child != NULL)
			qianxu(t->l_child);
		if (t->r_child != NULL)
			qianxu(t->r_child);
		return ;
	}
	void zhongxu(tree_point<ElemType>* t)
	{
		if (t->l_child != NULL)
			zhongxu(t->l_child);
		outlist.push_back(t);
		if (t->r_child != NULL)
			zhongxu(t->r_child);
		return;
	}
	void houxu(tree_point<ElemType>* t)
	{
		if (t->l_child != NULL)
			houxu(t->l_child);
		if (t->r_child != NULL)
			houxu(t->r_child);
		outlist.push_back(t);
		return;
	}
	int c_t = 0;
	void cengxu(tree_point<ElemType>* t)
	{
		vector<tree_point<ElemType>* > q;
		q.push_back(root);
		int last = 1, cur = 0;
		while (cur < q.size())
		{
			last = q.size();
			while (cur < last)
			{
				outlist.push_back( q[cur]);
				if (q[cur]->l_child)
					q.push_back(q[cur]->l_child);
				if (q[cur]->r_child)
					q.push_back(q[cur]->r_child);
				++cur;
			}
			c_t++;
		}
	}
	void out_lis()
	{
		int i;
		for (i = 0; i < outlist.size(); i++)
		{
			cout << outlist[i]->data;
			if (i != outlist.size() - 1)
				cout << ",";
			else
				cout << endl;
		}
		outlist.clear();
	}
	int cnt = 0, p_cnt = 0;
	void t_cnt(tree_point<ElemType>* t)
	{
		p_cnt++;
		if (t->l_child != NULL && t->r_child != NULL)
			cnt++;
		if(t->l_child != NULL)
			t_cnt(t->l_child);
		if (t->r_child != NULL)
			t_cnt(t->r_child);
	}
	int cengshu()
	{
		c_t = 0;
		cengxu(root);
		outlist.clear();
		return c_t;
	}
	int twocnt()
	{
		cnt = 0;
		t_cnt(root);
		return cnt;
	}
	int pointcnt()
	{
		p_cnt = 0;
		t_cnt(root);
		return p_cnt;
	}
	tree_point<ElemType>* find_father(tree_point<ElemType>* t, ElemType ss)
	{
		if (t->l_child != NULL)
			if (t->l_child->data == ss)
				return t;
		if (t->r_child != NULL)
			if (t->r_child->data == ss)
				return t;
		if (t->l_child != NULL)
			return find_father(t->l_child,ss);
		if (t->r_child != NULL)
			return find_father(t->r_child,ss);
		return NULL;
	}
	tree_point<ElemType>* find_self(tree_point<ElemType>* t, ElemType ss)
	{
		if (t->data == ss)
			return t;
		if (t->l_child != NULL)
			return find_father(t->l_child, ss);
		if (t->r_child != NULL)
			return find_father(t->r_child, ss);
		return NULL;
	}
};

int main()
{
	string s, ins, nulls;
	vector<string> part_in;
	BinaryTree<string> a;
	cin >> nulls;
	cin.get();
	getline(cin, ins);
	part_in = departString_string(ins);
	string found;
	cin >> found;
	//======================================
	/*for (auto i : part_in)
	{
		cout << i << endl;
	}
	cout << part_in.size() << endl;
	cout << nulls << endl;*/
	//======================================
	
	a.BinaryTree_fron(part_in, nulls);
	/*a.qianxu(a.get_root());
	a.out_lis();
	a.zhongxu(a.get_root());
	a.out_lis();
	a.houxu(a.get_root());
	a.out_lis();*/
	//cout<<a.pointcnt()<<endl;
	/*a.cengxu(a.get_root());
	a.out_lis();*/
	//cout << a.cengshu() << endl;
	tree_point<string>* ans=a.find_self(a.get_root(), found);
	if (ans == NULL)
		cout << "FALSE" << endl;
	else
		cout <<"TRUE" << endl;
	return 0;
}
           

繼續閱讀