(c++)数据结构与算法之树的应用:医院设施管理

#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;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值