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;
    }
  • 相关阅读:
    权限设计
    ts infer关键字
    Array初始化 以及 Array.prototype.map()的一些问题
    同步、异步、事件循环
    Spring学习笔记(一)
    【面试】关于get和post两种方法的不同。
    【算法】背包问题
    当你在浏览器输入一个网址(如http://www.taobao.com),按回车之后发生了什么?
    数据库语句复习笔记
    【算法】雀魂启动(笔试题)
  • 原文地址:https://www.cnblogs.com/Jason-Damon/p/3657398.html
Copyright © 2011-2022 走看看