zoukankan      html  css  js  c++  java
  • 二叉树的遍历

    struct Node
    {
        int value;
        Node* left;
        Node* right;
        Node(int val):value(val),left(NULL),right(NULL)
        {
        }    
    };
     
    #include <windows.h>
    #include <stack>
    #include <list>
    using namespace std;
    void preOrder(Node* root)
    {
        if(root != NULL)
        {
            printf("%d-",root->value);
            preOrder(root->left);
            preOrder(root->right);
        }
    }
    void inOrder(Node* root)
    {
        if(root != NULL)
        {
            inOrder(root->left);
            printf("%d-",root->value);        
            inOrder(root->right);
        }
    }
    void postOrder(Node* root)
    {
        if(root != NULL)
        {
            postOrder(root->left);
            postOrder(root->right);
            printf("%d-",root->value);        
        }
    }
    void preOrder_2(Node* root)
    {
        stack<Node*> stk;
        Node* cur = root;
        while(stk.size() >0 || cur != NULL)
        {
            while(cur != NULL)
            {
                printf("%d-",cur->value);
                stk.push(cur);
                cur = cur->left;
            }
            if(stk.size() >0)
            {
                cur = stk.top();
                stk.pop();
                cur = cur->right;
            }
        }    
    }
    void inOrder_2(Node* root)
    {
        stack<Node*> stk;
        Node* cur = root;
        while(stk.size() >0 || cur != NULL)
        {
            while(cur != NULL)
            {            
                stk.push(cur);
                cur = cur->left;
            }
            if(stk.size() >0)
            {
                cur = stk.top();
                printf("%d-",cur->value);
                stk.pop();
                cur = cur->right;
            }
        }    
    }
    void postOrder_2(Node* root,stack<Node*>& postStk)
    {
        stack<Node*> stk;
        Node* cur = root;
        while(stk.size() >0 || cur != NULL)
        {
            while(cur != NULL)
            {            
                stk.push(cur);
                postStk.push(cur);
                cur = cur->right;
            }
            if(stk.size() >0)
            {
                cur = stk.top();
                stk.pop();
                cur = cur->left;
            }
        }    
    }
    void levelOrder(Node* root)
    {
        Node* cur = root;
        list<Node*> nList;
        nList.push_back(cur);
        while(nList.size() >0)
        {
            cur = nList.front();
            nList.pop_front();
            printf("%d-",cur->value);
            if(cur->left != NULL)
            {
                nList.push_back(cur->left);
            }
            if(cur->right != NULL)
            {
                nList.push_back(cur->right);
            }
        }    
    }
    void createTree(Node*& root)
    {
        root = new Node(0);
        Node* n1 = new Node(1);
        Node* n2 = new Node(2);
        Node* n3 = new Node(3);
        Node* n4 = new Node(4);
        Node* n5 = new Node(5);
        Node* n6 = new Node(6);
        Node* n7 = new Node(7);
        Node* n8 = new Node(8);
        root->left = n1;
        root->right = n2;
        n1->left = n3;
        n1->right = n4;
        n2->left = n5;
        n2->right = n6;
        n3->left = n7;
        n3->right = n8;
    }
    int main(int argc, char* argv[])
    {
        Node* root;
        createTree(root);
        preOrder(root);
        printf(" ");
        preOrder_2(root);
        printf(" ");
        inOrder(root);
        printf(" ");
        inOrder_2(root);
        printf(" ");
        postOrder(root);
        printf(" ");
        stack<Node*> postStk;
        postOrder_2(root,postStk);
        while(postStk.size() >0)
        {
            printf("%d-",postStk.top()->value);
            postStk.pop();
        }
        printf(" ");
        levelOrder(root);
        system("pause");
        return 0;
    }
     
  • 相关阅读:
    第二节:简单工厂模式(静态工厂模式)
    第一节:不使用设计模式的传统方式
    第三章:设计模式概述
    第二节:类与类之间的关系
    高斯混合模型(GMM)
    随机森林
    LDA主题模型
    Adaboost算法
    线性代数
    k-means聚类
  • 原文地址:https://www.cnblogs.com/nzbbody/p/4320553.html
Copyright © 2011-2022 走看看