二叉树三叉链表实现类

来源:互联网 发布:淘宝关键词修改不见 编辑:程序博客网 时间:2024/05/16 01:02


#include <iostream>using namespace std;enum Statue{    TRUE, FALSE, INSERT_ERROR};//二叉树的三叉链表节点结构template <class NodeType> class BinaryNode{public:    NodeType value;    BinaryNode<NodeType> *parent, *lChild, *rChild;    BinaryNode();};template <class NodeType>BinaryNode<NodeType>::BinaryNode(){    parent = NULL;    lChild = NULL;    rChild = NULL;}//二叉树类,三叉链表实现template <class NodeType> class BinaryTree{public:    BinaryTree();    BinaryTree(BinaryNode<NodeType> *root);    ~BinaryTree();    void DestroyTree();    void CreatBinaryTree(BinaryNode<NodeType> *root);    Statue IsEmpty();  //判断是否为空树    int GetDepth(BinaryNode<NodeType> *node);     //计算从node节点开始的树的层数    //在二叉树中寻找值为value的节点,如有重复则返回中序遍历的第一个。PS:NodeType需要重载==符号    BinaryNode<NodeType>* Find(NodeType value);    //往node节点位置插入child节点    Statue InsertChild(BinaryNode<NodeType> *nodeParent, BinaryNode<NodeType> *node, BinaryNode<NodeType> *child);    void DeleteTree(BinaryNode<NodeType> *node);  //删除node节点为根的树,同时释放其占用内存private:    BinaryNode<NodeType> *root;    BinaryNode<NodeType>* FindFrom(BinaryNode<NodeType> *from, NodeType value);};template <class NodeType>BinaryTree<NodeType>::BinaryTree(){    root = NULL;}template <class NodeType>BinaryTree<NodeType>::BinaryTree(BinaryNode<NodeType> *root){    CreatBinaryTree(root);}template <class NodeType>void BinaryTree<NodeType>::DestroyTree(){    DeleteTree(root->lChild);    DeleteTree(root->rChild);    delete(root);}template <class NodeType>BinaryTree<NodeType>::~BinaryTree(){    DestroyTree();}template <class NodeType>Statue BinaryTree<NodeType>::IsEmpty(){    return (NULL == root) ? TRUE : FALSE;}template <class NodeType>int BinaryTree<NodeType>::GetDepth(BinaryNode<NodeType> *node){    if (NULL == node)    {        return 0;    }    int lDepth, rDepth;    lDepth = GetDepth(node->lChild);    rDepth = GetDepth(node->rChild);    return (lDepth > rDepth) ? lDepth + 1 : rDepth + 1;}template <class NodeType>void BinaryTree<NodeType>::CreatBinaryTree(BinaryNode<NodeType> *node){    if (NULL == node)    {        DestroyTree();        root = NULL;        return ;    }    root = new BinaryNode<NodeType>;    root->value = node->value;    InsertChild(root, root->lChild, node->lChild);    InsertChild(root, root->rChild, node->rChild);}template <class NodeType>Statue BinaryTree<NodeType>::InsertChild(BinaryNode<NodeType> *nodeParent,                                         BinaryNode<NodeType> *node, BinaryNode<NodeType> *child){    Statue result = INSERT_ERROR;    if (NULL == child)    {        DestroyTree();        result = TRUE;        return result;    }    node = new BinaryNode<NodeType>;    node->parent = nodeParent;    result = InsertChild(node, node->lChild, child->lChild);    if (INSERT_ERROR == result)    {        return result;    }    result = InsertChild(node, node->rChild, child->rChild);    return result;}template <class NodeType>void BinaryTree<NodeType>::DeleteTree(BinaryNode<NodeType> *node){    if (NULL = node)    {        return;    }    DeleteTree(node->lChild);    DeleteTree(node->rChild);    free(node);    node = NULL;}template <class NodeType>BinaryNode<NodeType>* BinaryTree<NodeType>::FindFrom(BinaryNode<NodeType> *from, NodeType value){    if (NULL == from)    {        return NULL;    }    if (from->value == value)    {        return from;    }    BinaryNode<NodeType> * findresult;    findresult = FindFrom(from->lChild, value);    if (NULL == findresult)    {        findresult = FindFrom(from->rChild, value);    }    return findresult;}template <class NodeType>BinaryNode<NodeType>* BinaryTree<NodeType>::Find(NodeType value){    return FindFrom(root, value);}



0 0