关于二叉树的常用函数代码

template 
class BinaryTreeNode
{
    friend class BinaryTree;

private:
    T info;

public:
    BinaryTreeNode *leftchild() const;
    BinaryTreeNode *rightchild() const;

    BinaryTreeNode();
    BinaryTreeNode(const T &ele); // 声明二叉树类为友元类 // 二叉树结点数据域
    // 缺省构造函数 // 给定数据的构造
    BinaryTreeNode(const T &ele, BinaryTreeNode *l, BinaryTreeNode *r); // 子树构造结点
    T value() const;

    // 返回当前结点数据 // 返回左子树 // 返回右子树
    void setLeftchild(BinaryTreeNode *); // 设置左子树

    void setRightchild(BinaryTreeNode *); // 设置右子树
    void setValue(const T &val);
    bool isLeaf() const;
    // 设置数据域 // 判断是否为叶结点
    BinaryTreeNode &operator=(const BinaryTreeNode &Node);
};

template 
class BinaryTree
{
private:
    BinaryTreeNode *root;

public:
    BinaryTree() { root = NULL; };
    ~BinaryTree() { DeleteBinaryTree(root); };
    bool isEmpty() const;
    //二叉树根结点
    //构造函数 //析构函数
    //判定二叉树是否为空树 BinaryTreeNode* Root() {return root;}; //返回根结点
    BinaryTreeNode *Parent(BinaryTreeNode *current);                             //返回父
    BinaryTreeNode *LeftSibling(BinaryTreeNode *current);                        //左兄
    BinaryTreeNode *RightSibling(BinaryTreeNode *current);                       //右兄
    void CreateTree(const T &info, BinaryTree &leftTree, BinaryTree &rightTree); // 构造树
    void PreOrder(BinaryTreeNode *root);                                            // 前序遍历二叉树
    void InOrder(BinaryTreeNode *root);
    // 中序遍历二叉树
    void PostOrder(BinaryTreeNode *root);        // 后序遍历二叉树
    void LevelOrder(BinaryTreeNode *root);       // 按层次遍历二叉树
    void DeleteBinaryTree(BinaryTreeNode *root); // 删除二叉树
};

enum Tags
{
    Left,
    Right
};
template 
class StackElement
{
public:
    BinaryTreeNode *pointer;
    Tags tag;
};
template 
void BinaryTree::PostOrderWithoutRecursion(BinaryTreeNode *root)
{
    using std::stack;
    // 使用STL的栈
    StackElement element;
    stack> aStack;
    BinaryTreeNode *pointer;
    pointer = root;
    while (!aStack.empty() || pointer)
    {
        if (pointer != NULL)
        {
            // 沿非空指针压栈,并左路下降
            element.pointer = pointer;
            element.tag = Left;
            aStack.push(element);
            pointer = pointer->leftchild();
        }
        else
        {
            element = aStack.pop();
            pointer = element.pointer;
            if (element.tag == Left)
            {
                // 获得栈顶元素,并退栈 // 如果从左子树回来
                element.tag = Right;
                aStack.push(element); //置标志位为Right pointer = pointer->rightchild();
            }
            else
            {
                Visit(pointer);
                pointer = NULL;
            }
        }
    } //end while
}

void BinaryTree::DeleteBinaryTree(BinaryTreeNode *root)
{
    if (root != NULL)
    {
        DeleteBinaryTree(root->left);
        DeleteBinaryTree(root->right);
        delete root;
        //递归删除左子树 //递归删除右子树 // 删除根结点
    }
}

template 
void BinarySearchTree::InsertNode(BinaryTreeNode *root, *newpointer)
{
    //向二叉搜索树插入新结点newpointer
    BinaryTreeNode *pointer;
    if (root == NULL)
    { //用指针newpointer初始化二叉搜索树树根,赋值实现
        Initialize(newpointer);
        return;
    }
    else
        pointer = root;
    while (1)
    {
        if (newpointer->value() == pointer->value())
            return; //=则不处理
        else if (newpointer->value() < pointer->value())
        { //左子树
            if (pointer->leftchild() == NULL)
            {
                //作为左孩子
                pointer->left = newpointer;
                return;
            }
            else
                pointer = pointer->leftchild();
        }
        else
        { //作为右子树
            if (pointer->rightchild() == NULL)
            { //右孩子空,则作为右孩子
                pointer->right = newpointer;
                return;
            }
            else
                pointer = pointer->rightchild();
        }
    }
}

template 
void BinarySearchTree::DeleteNodeEx(BinaryTreeNode *delpointer)
{
    BinaryTreeNode *replpointer;
    BinaryTreeNode *replparent = NULL;             //替换结点 //替换结点的父结点
    BinaryTreeNode *delparent = Parent(delpointer); //待删结点父结点
    //若待删除结点的左子树为空,就将其右子树代替它
    if (delpointer->leftchild() == NULL)
        replpointer = delpointer->rightchild();
    else
    {
        replpointer = delpointer->leftchild();
        while (replpointer->rightchild() != NULL)
        {
            replparent = replpointer;
            replpointer = replpointer->rightchild();
        } //替换结点就是被删结点的左子结点, 左子树挂接为其父(被删)的左子树
        if (replparent == NULL)
            delpointer->left = replpointer->leftchild();
        // 替换结点的左子树挂接为其父的右子树 // 暂时
        else
            replparent->right = replpointer->leftchild();
        replpointer->left = delpointer->leftchild();   //继承待删结点左子树
        replpointer->right = delpointer->rightchild(); //继承待删结点右子树
    }
    // 用替换结点去替代真正的删除结点
    if (delparent == NULL)
        root = replpointer;
    else if (delparent->leftchild() == delpointer)
        delparent->left = replpointer;
    else
        delparent->right = replpointer;
    delete delpointer;
    delpointer = NULL;
    return;
}

你可能感兴趣的:(algorithm)