#include <iostream>
#include<string>
#include<stack>
using namespace std;
class hosNode
{
private:
int num;
string name;
public:
friend class node;
friend class tree;
hosNode()
{
}
hosNode(int n,string s)
{
num=n;
name=s;
}
string Name()
{
return name;
}
int Num()
{
return num;
}
void visit()
{
cout<<" name: "<<name<<" num: "<<num<<endl;
}
};
ostream& operator<<(ostream& out,hosNode& node)
{
out<<"名称: "<<node.Name()<<" × "<<node.Num()<<endl;
return out;
}
class node
{
private:
hosNode value;
node *child,*brother;
public:
friend class tree;
node()
{
child=brother=NULL;
}
node(int i,string s)
{
hosNode value(i,s);
child=brother=NULL;
}
node(hosNode p)
{
value=p;
child=brother=NULL;
}
hosNode getValue()
{
return value;
}
void setValue(hosNode p)
{
value=p;
}
node* Child()
{
return child;
}
node* Brother()
{
return brother;
}
node* lastBrother(node *p)
{
if(p->brother) return lastBrother(p->brother);
else return p;
}
node* pre_show(node *p) //前序递归访问(显示)p及其子结点,兄弟结点
{
if(p)
{
cout<<p->value;
if(p->child) pre_show(p->child);
if(p->brother) pre_show(p->brother);
}
else return NULL;
}
node* Find(node *p,string par) //在p中寻找名为par的结点
{
stack<node*> s;
if(p) s.push(p);
while(!s.empty())
{
node* p=s.top();
s.pop();
if(p)
if(p->value.name==par) return p;
if(p->brother) s.push(p->brother);
if(p->child) s.push(p->child);
}
}
};
class tree
{
private:
node* root;
public:
tree()
{
root=new node;
root->value.name="医院";
root->value.num=1;
root->brother=root->child=NULL;
}
node* Root()
{
return root;
}
node* parent(node * n,node* t) //n中寻找t的父结点,返回父结点
{
if(t==root) return NULL;
node *p=n;
stack<node*> s;
if(p) s.push(p);
while(!s.empty())
{
node* p=s.top();
s.pop();
if(p->child)
{
node *pp=p->child;
if(pp==t) return p;
do
{
if(pp->brother)
{
if(pp->brother==t) return p;
pp=pp->brother;
}
}
while(pp->brother);
}
if(p->brother) s.push(p->brother);
if(p->child) s.push(p->child);
}
// 递归失败:
// if(n->child==t) return n;
// else
// {
// if(n->child) return parent(n->child,t);
// }
}
bool Insert(string par,hosNode rootvalue) //向名为par的结点插入子结点
{
node *newnode=new node(rootvalue);
node *p=root->Find(root,par);
if(!p) return false;
if(!p->child)
{
p->child=newnode;
}
else
{
p->child->lastBrother(p->child)->brother=newnode;
}
return true;
}
int countNum(tree bt,string par,string ch) //返回par中ch的数量
{
node *parent=root->Find(root,par);
node *t=parent->Find(parent,ch);
if(!t) return 0; //没找到
int total=1;
while(parent->value.name!=t->value.name) //累加数量
{
total=total*(t->value.num);
t=bt.parent(root,t);
}
return total;
}
bool findNode(string str) //在系统中寻找名为str的结点并输出其及其子结点,没找到返回false
{
node *p=root,*t=NULL;
stack<node*> s;
if(p) s.push(p);
while(!s.empty())
{
node* p=s.top();
s.pop();
if(p)
if(p->value.name==str) t=p;
if(p->brother) s.push(p->brother);
if(p->child) s.push(p->child);
}
if(!t)
{
cout<<"not found"<<endl;
return false;
}
else
{
cout<<t->value;
t->pre_show(t->child);
}
}
void showStructure() //输出整个系统的结构
{
root->pre_show(root);
}
};
int main()
{
tree t;
{
hosNode p1(10,"楼层");
t.Insert("医院",p1);
hosNode p2(1,"中央大厅");
t.Insert("楼层",p2);
hosNode p3(4,"配楼"); t.Insert("楼层",p3);
hosNode p4(1,"电视"); t.Insert("中央大厅",p4);
hosNode p5(2,"沙发"); t.Insert("中央大厅",p5);
hosNode p6(2,"长走廊"); t.Insert("配楼",p6);
hosNode p7(1,"走廊连接"); t.Insert("配楼",p7);
hosNode p8(21,"病房"); t.Insert("长走廊",p8);
hosNode p9(5,"库房"); t.Insert("走廊连接",p9);
hosNode p10(1,"卫生间"); t.Insert("病房",p10);
hosNode p11(4,"插座"); t.Insert("病房",p11);
hosNode p12(2,"病床"); t.Insert("病房",p12);
hosNode p13(1,"洗面盆"); t.Insert("卫生间",p13);
hosNode p14(1,"坐便器"); t.Insert("卫生间",p14);
hosNode p15(2,"插口"); t.Insert("插座",p15);
hosNode p16(1,"插板"); t.Insert("插座",p16);
}
string s1="长走廊",s2="插口",s3="医院",s4="库房";
cout<<s1<<"中的"<<s2<<",共有:"<<t.countNum(t,s1,s2)<<" 个"<<endl;
cout<<s3<<"中的"<<s4<<",共有:"<<t.countNum(t,s3,s4)<<" 个"<<endl;
cout<<"输出插座及其子结点:"<<endl;
t.findNode("插座");
cout<<"整个医院的结构如下:"<<endl;
t.showStructure();
cout << "Hello world!" << endl;
return 0;
}
(c++)数据结构与算法之树的应用:医院设施管理
最新推荐文章于 2024-06-14 12:34:14 发布
本文使用C++实现了一个树形结构来管理医院的设施,包括插入节点、查找节点、统计节点数量等功能。例如,可以查找某个楼层的电视数量,或者展示整个医院的设施结构。
摘要由CSDN通过智能技术生成