zoukankan      html  css  js  c++  java
  • 二叉树的遍历(递归、非递归)

    #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("深度遍历二叉树(先序遍历,非递归):
    ");
        DFSBinaryTree(tree,Visit);
        printf("
    ");
    
        printf("广度遍历二叉树:
    ");
        BFSBinaryTree(tree, Visit);
        printf("
    ");
    
        printf("先序遍历二叉树:
    ");
        PreoderTraverse(tree, Visit);
        printf("
    ");
    
        printf("中序遍历二叉树:
    ");
        InorderTraverse(tree, Visit);
        printf("
    ");
    
        printf("中序遍历二叉树(非递归):
    ");
        InorderTraverse2(tree, Visit);
        printf("
    ");
    
        printf("后序遍历二叉树:
    ");
        PostorderTraverese(tree, Visit);
        printf("
    ");
    
    
        return 0;
    }
  • 相关阅读:
    【总结】Centos中,Kerberos安装
    Go语言mgo
    Go语言mgo
    理解 Serenity,Part-1:深度抽象
    ZCash零知识证明
    零知识证明(Zero-Knowledge Proof)原理详解:非交互式证明实现自动验证防止作假
    tf.shape(x)、x.shape、x.get_shape()函数解析(最清晰的解释)
    Java生成(m.n)之间的随机数
    tf.cond()函数解析(最清晰的解释)
    OpenStack Blazar 架构解析与功能实践
  • 原文地址:https://www.cnblogs.com/Jason-Damon/p/3657398.html
Copyright © 2011-2022 走看看