遍历二叉树的神级方法(Morris遍历)

#include "Tree.h"
using namespace std;
//Morris方法中序遍历,可以保证时间复杂度N,空间复杂度1
void morrisIn(Node* head)
{
    if(head == nullptr)
        return;
    Node* cur1 = head;
    Node* cur2 = nullptr;
    while(cur1)
    {
        cur2 = cur1->left;
        if(cur2)
        {
            while(cur2->right && cur2->right != cur1)
                cur2 = cur2->right;
            if(cur2->right)
                cur2->right = nullptr;
            else
            {
                cur2->right = cur1;
                cur1 = cur1->left;
                continue;
            }
        }
        cout << cur1->value << endl;
        cur1 = cur1->right;
    }
}
//Morris方法先序遍历,可以保证时间复杂度N,空间复杂度1
void morrisPre(Node* head)
{
    if(head == nullptr)
        return;
    Node* cur1 = head;
    Node* cur2 = nullptr;
    while(cur1)
    {
        cur2 = cur1->left;
        if(cur2)
        {
            while(cur2->right && cur2->right != cur1)
                cur2 = cur2->right;

            if(cur2->right)
                cur2->right = nullptr;
            else
            {
                cout << cur1->value << endl;
                cur2->right = cur1;
                cur1 = cur1->left;
                continue;
            }
        }
        else
        {
            cout << cur1->value << endl;
        }
        cur1 = cur1->right;
    }
}
//Morris方法后序遍历,可以保证时间复杂度N,空间复杂度1
Node* reverseEdge(Node* from)
{
    Node* pre = nullptr;
    Node* next = nullptr;
    while(from)
    {
        next = from->right;
        from->right = pre;
        pre = from;
        from = next;
    }
    return pre;
}
void printEdge(Node* head)
{
    Node* tail = reverseEdge(head);
    Node* cur = tail;
    while(cur)
    {
        cout << cur->value << endl;
        cur = cur->right;
    }
    reverseEdge(tail);
    return;
}

void morrisPos(Node* head)
{
    if(head == nullptr)
        return;
    Node* cur1 = head;
    Node* cur2 = nullptr;
    while(cur1)
    {
        cur2 = cur1->left;
        if(cur2)
        {
        while(cur2->right && cur2->right != cur1)
                cur2 = cur2->right;
            if(cur2->right)
            {
                cur2->right = nullptr;
                printEdge(cur1->left);
            }
            else
            {
                    cur2->right = cur1;
                    cur1 = cur1->left;
                    continue;
            }
        }
        cur1 = cur1->right;
    }
    printEdge(head);
}
int main()
{
     Node* pNode0 = new Node(0);
    Node* pNode1 = new Node(1);
    Node* pNode2 = new Node(2);
    Node* pNode3 = new Node(3);
    Node* pNode4 = new Node(4);
    Node* pNode5 = new Node(5);
    Node* pNode6 = new Node(6);

    connectTree(pNode0, pNode1, pNode2);
    connectTree(pNode1, pNode3, pNode4);
    connectTree(pNode2, pNode5, pNode6);

    morrisIn(pNode0);
    cout << "=============" << endl;
    morrisPre(pNode0);
    cout << "==============" << endl;
    morrisPos(pNode0);
}

猜你喜欢

转载自blog.csdn.net/wzc2608/article/details/80720319