问题描述 :
内容:(1)请参照链表的ADT模板,设计二叉树并逐步完善的抽象数据类型。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。参考教材、课件,以及网盘中的链表ADT原型文件,自行设计二叉树的ADT。)
注意:二叉树ADT的基本操作的算法设计很多要用到递归的程序设计方法。
(2)基本操作14:在二叉树的二叉链表存储形式建立的基础上,使用递归的程序设计方法,设计并完成查找以元素值为x的结点的兄弟结点算法。完成后将其加入到二叉树的ADT基本操作集中。
初始条件:二叉树T存在,x是T中某个结点。
操作结果:若x是T的结点,查找并返回指向x的兄弟结点的指针。
flag=0,返回左兄弟,若x是其双亲结点的左孩子或无左兄弟,则返回NULL。
flag=1,返回右兄弟,若x是其双亲结点的右孩子或无右兄弟,则返回NULL。
参考函数原型:
(1)//返回指向值为x的结点的兄弟结点的指针(外壳部分,用户函数)
//返回指向值为x的结点的兄弟结点的指针(flag=0,左兄弟;flag=1,右兄弟)
template<class ElemType>
BinaryTreeNode<ElemType> * GetSibling(BinaryTree<ElemType> &T, ElemType &x, int flag);
(2)返回指向值为x的结点的兄弟结点的指针(成员函数)
//返回指向值为x的结点的兄弟结点的指针(flag=0,左兄弟;flag=1,右兄弟)
template<class ElemType>
BinaryTreeNode<ElemType> * BinaryTree<ElemType>::Get_Sibling( BinaryTreeNode<ElemType> *parent, BinaryTreeNode<ElemType> *location, int flag ) const;
(3)辅助函数:获取父结点(参见基本操作7)
(4)辅助函数:查找元素值为x的结点的位置(参见基本操作8)
输入说明 :
第一行:表示无孩子或指针为空的特殊分隔符
第二行:二叉树T的先序序列(结点元素之间以空格分隔)
第三行:元素值x
第四行:flag值
输出说明 :
查找成功
第一行:flag=0:左兄弟元素值
flag=1:右兄弟元素值
查找失败
第一行:显示 NULL
输入范例 :
#
A B # C D # # E # # F # G # H # #
E
0
输出范例 :
D
解题代码:
// 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(t);
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 P_cengshu(tree_point<ElemType>* t)
{
c_t = 0;
cengxu(t);
outlist.clear();
return c_t;
}
int twocnt()
{
cnt = 0;
t_cnt(root);
return cnt;
}
int P_twocnt(tree_point<ElemType>* t)
{
cnt = 0;
t_cnt(t);
return cnt;
}
int pointcnt()
{
p_cnt = 0;
t_cnt(root);
return p_cnt;
}
tree_point<ElemType>* father_p;
void find_fatherNow(tree_point<ElemType>* t, ElemType ss)
{
if (t->l_child != NULL)
if (t->l_child->data == ss)
{
father_p = t;
return;
}
if (t->r_child != NULL)
if (t->r_child->data == ss)
{
father_p = t;
return;
}
if (t->l_child != NULL)
find_fatherNow(t->l_child, ss);
if (t->r_child != NULL)
find_fatherNow(t->r_child, ss);
return ;
}
tree_point<ElemType>* find_father(tree_point<ElemType>* t, ElemType ss)
{
father_p = NULL;
find_fatherNow(t, ss);
return father_p;
}
tree_point<ElemType>* nas_p;
void find_selfNOW(tree_point<ElemType>* t, ElemType ss)
{
if (t->data == ss)
nas_p = t;
if (t->l_child != NULL)
find_selfNOW(t->l_child, ss);
if (t->r_child != NULL)
find_selfNOW(t->r_child, ss);
return ;
}
tree_point<ElemType>* find_self(tree_point<ElemType>* t, ElemType ss)
{
nas_p = NULL;
find_selfNOW(t, ss);
return nas_p;
}
void fast_delChild(tree_point<ElemType>* t,bool flag)
{
if (flag == 0)
t->l_child = NULL;
else
t->r_child = NULL;
return;
}
void slow_delChild(tree_point<ElemType>* t, bool flag)
{
if (flag == 0)
t->l_child = NULL;
else
t->r_child = NULL;
return;
}
};
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;
bool flag;
cin >> flag;
//======================================
/*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 << endl;*/
//cout<<a.pointcnt()<<endl;
/*a.cengxu(a.get_root());
a.out_lis();*/
//cout << a.cengshu() << endl;
tree_point<string>* ans = a.find_father(a.get_root(), found);
if (ans == NULL)
cout << "NULL" << endl;
else
{
if (flag == 0)// left child
{
if (ans->l_child == NULL || ans->l_child->data==found)
cout << "NULL" << endl;
else
{
cout << ans->l_child->data << endl;
}
}
else
{
if (ans->r_child == NULL || ans->r_child->data == found)
cout << "NULL" << endl;
else
{
cout << ans->r_child->data<< endl;
}
}
}
//cout << a.P_cengshu(ans) << endl;
return 0;
}