C++ 树形结构

转载自:http://blog.csdn.net/stephenxu111/article/details/2446382
修改了此篇文章中 代码显示的一点小问题。

///Tree.h 文件
#pragma once
#include <list>
#include <algorithm>
using namespace std;

struct TreeNode;   //定义一个结构体原形
class Tree;        //定义一个类原形
class Iterator;    //定义一个类原形
typedef list<TreeNode*> List; //重命名一个节点链表

TreeNode* clone(TreeNode*, List&, TreeNode*);//Clone复制函数

struct TreeNode {
    int _data;                         //数据
    TreeNode* _parent;                 //父节点
    List _children;                    //子节点
    TreeNode(int, TreeNode* );          //构造函数
    void SetParent(TreeNode& );         //设置父节点
    void InsertChildren(TreeNode& );    //插入子节点
};

class Tree{
public:

    //下面是构造器和运算符重载
    Tree();                                //默认构造函数
    Tree(const Tree&);                     //复制构造函数
    Tree(const int);                       //带参数构造函数
    Tree(const int,const list<Tree*>&);    //带参数构造函数
    ~Tree();                               //析构函数
    Tree& operator=(const Tree&);           //=符号运算符重载
    bool operator==(const Tree&);           //==符号运算符重载
    bool operator!=(const Tree&);           //!=符号运算符重载

                                        //下面是成员函数
    void Clear();                         //清空
    bool IsEmpty()const;                  //判断是否为空
    int Size()const;                      //计算节点数目
    int Leaves();                         //计算叶子数
    int Root()const;                      //返回根元素
    int Height();                         //计算树的高度


                                      //下面是静态成员函数
    static bool IsRoot(Iterator);         //判断是否是根
    static bool isLeaf(Iterator);         //判断是否是叶子
    static Iterator Parent(Iterator);     //返回其父节点
    static int NumChildren(Iterator);     //返回其子节点数目

                                     //跌代器函数
    Iterator begin();                //Tree Begin
    Iterator end();                  //Tree End
    friend class Iterator;            //Iterator SubClass
private:
    list<TreeNode*> _nodes;            //节点数组
    list<TreeNode*>::iterator LIt;     //一个节点迭代器
    int height(TreeNode*);
    int level(TreeNode*,Iterator);
};

//This is TreeSub Class Iterator
class Iterator{
private:
    Tree* _tree;                        //Tree data
    list<TreeNode*>::iterator _lit;     //List Iterator
public:
    Iterator();                                 //默认构造函数
    Iterator(const Iterator&);                  //复制构造函数
    Iterator(Tree*,TreeNode*);                  //构造函数
    Iterator(Tree*,list<TreeNode*>::iterator);  //构造函数
                                                //运算符重载
    void operator=(const Iterator&);            //赋值运算符重载
    bool operator==(const Iterator&);           //关系运算符重载
    bool operator!=(const Iterator&);           //关系运算符重载
    Iterator& operator++();                     //前缀++运算符
    Iterator operator++(int);                   //后缀++运算符
    int operator*()const;                       //获得节点信息
    bool operator!();                           //赋值运算符重载

    typedef list<TreeNode*>::iterator List;
    friend class Tree;
};
///Tree.cpp 文件
#include "stdafx.h"
#include "Tree.h"

//***** 下面是对于TreeNode结构体的定义实现*****///

TreeNode::TreeNode(int type = 0, TreeNode* Parent = 0) {
    _data = type;
    _parent = Parent;
}
void TreeNode::SetParent(TreeNode& node) {
    _parent = &node;
}
void TreeNode::InsertChildren(TreeNode& node) {
    TreeNode* p = &node;
    _children.push_back(p);
}



//***** 下面是对于Tree类的定义实现*****///
Tree::Tree() {

}

Tree::Tree(const int type) {
    _nodes.push_back(new TreeNode(type));
}

Tree::Tree(const Tree& t) {
    if (t._nodes.empty())return;
    clone(t._nodes.front(), _nodes, 0);
}
Tree::Tree(const int type, const list<Tree*>& lit) {
    TreeNode* root = new TreeNode(type);//建立根节点
    _nodes.push_back(root);//放入树中
    list<Tree*>::const_iterator it;
    for (it = lit.begin(); it != lit.end(); it++) {
        if (!((*it)->_nodes.empty())) {//如果当前节点元素不为空
            Tree* tp = new Tree(**it);
            TreeNode* p = tp->_nodes.front();
            root->_children.push_back(p); //设置根的子节点
            p->_parent = root;            //设置节点的父节点为根
            list<TreeNode*>::iterator lit1 = tp->_nodes.begin();
            list<TreeNode*>::iterator lit2 = tp->_nodes.end();
            list<TreeNode*>::iterator lit3 = _nodes.end();
            _nodes.insert(lit3, lit1, lit2);
        }
    }
}

Tree::~Tree() {
    for (list<TreeNode*>::iterator it = _nodes.begin(); it != _nodes.end(); it++) {
        delete* it;
    }
}

Tree& Tree::operator =(const Tree & t) {
    Clear();
    Tree* p = new Tree(t);
    _nodes = p->_nodes;
    return *this;
}

bool Tree::operator ==(const Tree& t) {
    if (_nodes.size() != t._nodes.size()) {
        return false;
    }
    list<TreeNode*>::iterator it = _nodes.begin();
    list<TreeNode*>::const_iterator _it = t._nodes.begin();
    while (it != _nodes.end() && _it != t._nodes.end()) {
        if ((*it)->_data != (*_it)->_data) {
            return false;
        }
        it++;
        _it++;
    }
    return true;
}

bool Tree::operator !=(const Tree& t) {
    if (_nodes.size() != _nodes.size()) {
        return true;
    }
    else {
        list<TreeNode*>::iterator it = _nodes.begin();
        list<TreeNode*>::const_iterator _it = t._nodes.begin();
        while (it != _nodes.end() && _it != t._nodes.end()) {
            if ((*it)->_data != (*_it)->_data) {
                return true;
            }
            it++;
            _it++;
        }
        return false;
    }
}

void Tree::Clear() {
    for (list<TreeNode*>::iterator it = _nodes.begin(); it != _nodes.end(); it++) {
        delete* it;
    }
    _nodes.clear();
}

bool Tree::IsEmpty()const {
    return _nodes.empty();
}

int Tree::Size()const {
    return (int)_nodes.size();
}

int Tree::Leaves() {
    int i = 0;
    list<TreeNode*>::iterator it = _nodes.begin();
    while (it != _nodes.end()) {
        if ((*it)->_children.size() == 0) {
            i++;
        }
        it++;
    }
    return i;
}


int Tree::Height() {
    if (_nodes.size() != 0) {
        TreeNode* TNode = _nodes.front();
        return height(TNode);
    }
    else {
        return -1; //判断为空树
    }
}

int Tree::height(TreeNode* node) {
    if (!node) {
        return -1;
    }
    else {
        list<TreeNode*> plist = node->_children;
        if (plist.size() == 0) {
            return 0;
        }
        int hA = 0;
        for (list<TreeNode*>::iterator it = plist.begin(); it != plist.end(); it++) {
            int hB = height(*it);
            if (hB>hA) {
                hA = hB;
            }
        }
        return hA + 1;
    }
}


Iterator Tree::begin() {
    return Iterator(this, _nodes.begin());
}

Iterator Tree::end() {
    return Iterator(this, _nodes.end());
}
int Tree::Root()const {
    return (*_nodes.begin())->_data;
}


bool Tree::IsRoot(Iterator it) {
    TreeNode p = *it;
    if (p._parent == 0) {
        return true;
    }
    return false;
}

bool Tree::isLeaf(Iterator it) {
    TreeNode p = *it;
    if (p._children.size() == 0) {
        return true;
    }
    return false;
}

Iterator Tree::Parent(Iterator it) {
    TreeNode p = *it;
    Tree* t = it._tree;
    Iterator Ite(t, p._parent);
    return Ite;
}


int Tree::NumChildren(Iterator it) {
    TreeNode p = *it;
    return (int)p._children.size();
}

//***** 下面是对于Tree::Iterator类的定义实现*****///
Iterator::Iterator() {
}

Iterator::Iterator(const Iterator& it) {
    _tree = it._tree;
    _lit = it._lit;
}

Iterator::Iterator(Tree* t, TreeNode* n) {
    _tree = t;
    list<TreeNode*>& nodes = _tree->_nodes;
    _lit = find(nodes.begin(), nodes.end(), n);//<algorithm> Members
}

Iterator::Iterator(Tree * t, list<TreeNode*>::iterator lt) {
    _tree = t;
    _lit = lt;
}

void Iterator::operator =(const Iterator& it) {
    _tree = it._tree;
    _lit = it._lit;
}

bool Iterator::operator ==(const Iterator & it) {
    return _tree == it._tree && _lit == it._lit;
}

bool Iterator::operator !=(const Iterator & it) {
    return _tree != it._tree || _lit != it._lit;
}

Iterator& Iterator::operator ++() {
    ++_lit;
    return *this;
}

Iterator Iterator::operator ++(int) {
    Iterator it(*this);
    ++_lit;
    return it;
}

int Iterator::operator *() const {
    return ((*_lit)->_data);
}

bool Iterator::operator !() {
    return _lit == _tree->_nodes.end();
}

//Clone函数
TreeNode* clone(TreeNode* node, List& nodes, TreeNode* nodep) {
    TreeNode* cp = new TreeNode(node->_data, nodep);
    nodes.push_back(cp);
    List& l = node->_children;
    List& cl = cp->_children;
    for (list<TreeNode*>::iterator lt = l.begin(); lt != l.end(); lt++) {
        cl.push_back(clone(*lt, nodes, cp));
    }
    return cp;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值