Visitors hit counter dreamweaver

二叉树的遍历(递归、非递归)

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <stack>
#include <queue>

using namespace std;
#define Element char
#define Format "%c"

//构造树节点
typedef struct Node{
    Element data;
    struct Node *lchild;
    struct Node *rchild;
}*Tree;

int index = 0;

//构造二叉树,按照先序遍历构造二叉树
//无左子树或者右子树用"#"表示
void TreeConstructor(Tree &root, Element data[])
{
    Element e = data[index++];
    if (e == '#')
    {
        root = NULL;
    }else{
        root = (Node *)malloc(sizeof(Node));
        root->data = e;
        TreeConstructor(root->lchild, data);
        TreeConstructor(root->rchild, data);
    }
}

//深度遍历二叉树(先序遍历,非递归)
//根节点先入栈,当栈不空时,弹出栈元素并遍历它,同时把它的右子树和左子树入栈
void DFSBinaryTree(Tree root, void (*VisitFunc)(Tree t))
{
    stack<Tree> st;
    st.push(root);
    Tree pTree;
    while (!st.empty())
    {
        //遍历节点
        pTree = st.top();
        st.pop();
        VisitFunc(pTree);

        //如果右子树存在,入栈
        if (pTree->rchild != NULL)
        {
            st.push(pTree->rchild);
        }

        //如果左子树存在,入栈
        if (pTree->lchild != NULL)
        {
            st.push(pTree->lchild);
        }
    }
}

//广度遍历二叉树
void BFSBinaryTree(Tree root, void (*VisitFunc)(Tree t))
{
    queue<Tree> qt;
    qt.push(root);
    Tree pTree;

    while (!qt.empty())
    {
        pTree = qt.front();
        qt.pop();
        VisitFunc(pTree);

        if (pTree->lchild != NULL)
        {
            qt.push(pTree->lchild);
        }
        if (pTree->rchild != NULL)
        {
            qt.push(pTree->rchild);
        }
    }
}

//先序遍历二叉树
void PreoderTraverse(Tree root, void (*VisitFunc)(Tree t))
{
    if (root == NULL)
    {
        return;
    }
    else
    {
        VisitFunc(root);
        PreoderTraverse(root->lchild, VisitFunc);
        PreoderTraverse(root->rchild, VisitFunc);
    }
}


//中序遍历二叉树
void InorderTraverse(Tree root, void (*VisitFunc)(Tree t))
{
    if (root == NULL)
    {
        return;
    }
    else
    {
        InorderTraverse(root->lchild, VisitFunc);
        VisitFunc(root);
        InorderTraverse(root->rchild, VisitFunc);
    }
}

//中序遍历二叉树,非递归
//先把根节点和它的左子树都入栈,当没有左子树的时候,弹栈,遍历,
//并把对右子树和它的左子树入栈,
void InorderTraverse2(Tree root, void (*VisitFunc)(Tree t))
{
    stack<Tree> st;
    Tree pTree = root;

    while (pTree != NULL || !st.empty())
    {
        //沿左子树搜索,左子树先都入栈
        while (pTree !=NULL)
        {
            st.push(pTree);
            pTree = pTree->lchild;
        }

        pTree = st.top();
        VisitFunc(pTree);
        st.pop();
        
        pTree = pTree->rchild;
    }
}

//后序遍历二叉树
void PostorderTraverese(Tree root, void (*VisitFunc)(Tree t))
{
    if (root == NULL)
    {
        return;
    }
    else
    {
        PostorderTraverese(root->lchild, VisitFunc);
        PostorderTraverese(root->rchild, VisitFunc);
        VisitFunc(root);
    }
}

void Visit(Tree t)
{
    printf(Format, t->data);
}

BinaryTreeTraverse.cpp

// DFSBinaryTree.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "DFSBinaryTree.h"


int _tmain(int argc, _TCHAR* argv[])
{
    Element data[15] = {'A', 'B', 'D', '#', '#', 'E', '#', '#', 'C', 'F', '#', '#', 'G', '#', '#'};
    Tree tree;
    TreeConstructor(tree, data);
    printf("深度遍历二叉树(先序遍历,非递归):\n");
    DFSBinaryTree(tree,Visit);
    printf("\n");

    printf("广度遍历二叉树:\n");
    BFSBinaryTree(tree, Visit);
    printf("\n");

    printf("先序遍历二叉树:\n");
    PreoderTraverse(tree, Visit);
    printf("\n");

    printf("中序遍历二叉树:\n");
    InorderTraverse(tree, Visit);
    printf("\n");

    printf("中序遍历二叉树(非递归):\n");
    InorderTraverse2(tree, Visit);
    printf("\n");

    printf("后序遍历二叉树:\n");
    PostorderTraverese(tree, Visit);
    printf("\n");


    return 0;
}

 

posted @ 2014-04-10 21:14  Jason Damon  阅读(1669)  评论(0编辑  收藏  举报