树的邻接表表示法

实现了书上这样的存储方式:

 

#ifndef listTree_H_
#define listTree_H_
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
using namespace std;

struct childNode
{
    int child;
    childNode *next;
};

struct headNode
{
    char data;
    int parent;
    childNode *firstChild;
};

class ListTree
{
public:
    ListTree();
    ~ListTree();
    void creat();
    void preOrder();
    void leverOrder();
private:
    headNode*node;
    int num;
};

ListTree::ListTree()
{
    node=NULL;
    cout<<"Please input the number of node in the tree."<<endl;
    cin>>num;
}
//lever order to creat
void ListTree::creat()
{
    char temp[20];
    childNode *tempChild;
    node=new headNode[num];
    int i,j,k;
    cout<<"Input every data of the tree.(level order,first is root)"<<endl;
    for(i=0; i<num; i++)
    {
        cin>>node[i].data;
        node[i].parent=-1;
        node[i].firstChild=NULL;
    }
    getchar();
    for(i=0; i<num; i++)
    {
        cout<<"input children of "<<node[i].data<<endl;
        cin.getline(temp,20);
        int len=strlen(temp);
        for(j=num-1; j>=0; j--)
        {
            for(k=0; k<len; k++)
            {
                if(temp[k]==node[j].data)
                {
                    tempChild=new childNode;
                    tempChild->child=j;
                    tempChild->next=node[i].firstChild;
                    node[i].firstChild=tempChild;
                    node[j].parent=i;
                }
            }
        }
    }
}

void ListTree::leverOrder()
{
    int i;
    for(i=0; i<num; i++)
    {
        cout<<node[i].data<<" ";
    }
    cout<<endl;
}

//it's so like when compare to DFS
void ListTree::preOrder()
{
    bool visited[num];
    memset(visited,0,sizeof(visited));
    stack<int>S;
    cout<<node[0].data<<" ";
    visited[0]=1;
    S.push(0);
    while(!S.empty())
    {
        int top=S.top();
        childNode*cur=node[top].firstChild;
        while(cur)
        {
            if(cur && !visited[cur->child])
            {
                cout<<node[cur->child].data<<" ";
                visited[cur->child]=1;
                S.push(cur->child);
                break;
            }
            cur=cur->next;
        }
        if(!cur)S.pop();
    }
}

ListTree::~ListTree()
{
    int i;
    childNode*temp;
    for(i=0; i<num; i++)
    {
        childNode*cur=node[i].firstChild;
        while(cur)
        {
            temp=cur;
            cur=cur->next;
            delete(temp);
        }
        delete(&node[i]);
    }
}
#endif

测试:

#include <iostream>
#include "listTree.h"
using namespace std;

int main()
{
    ListTree test;
    test.creat();
    test.leverOrder();
    cout<<endl;
    test.preOrder();
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值