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;
    }
     
  • 相关阅读:
    /etc/sysctl.conf 控制内核相关配置文件
    python 并发编程 非阻塞IO模型
    python 并发编程 多路复用IO模型
    python 并发编程 异步IO模型
    python 并发编程 阻塞IO模型
    python 并发编程 基于gevent模块 协程池 实现并发的套接字通信
    python 并发编程 基于gevent模块实现并发的套接字通信
    python 并发编程 io模型 目录
    python 并发编程 socket 服务端 客户端 阻塞io行为
    python 并发编程 IO模型介绍
  • 原文地址:https://www.cnblogs.com/nzbbody/p/4320553.html
Copyright © 2011-2022 走看看