二叉树完整操作集(源代码)

背景

我在做单片机项目时,遇到了一个很担心的隐患:单片机在用异步串口传输数据的时候,底层直接传输比特流,但是经过多次测试存在一个问题:控制代码、GPIO和数学运算实在是太费时间了,很有可能导致数据传输不稳定,传输过程抖动,频繁建立握手连接,最终导致信息传递不及时,控制动作无法按时到达导致直接挂机。为了解决潜在的隐患,避免实际成品挂掉,必须压缩传输的数据编码。
编码算法?解码算法呢?怎么办?苦思冥想不得其解。查阅相关资料,发现Huffman树好像可以解决此问题,但是需要先修二叉树,三天时间自学了一下二叉树,记录一下学习过程写下的操作集合。

结构

class BiTNode
{
public:
    int data = -1;
    BiTNode *left, *right;
public:
    explicit BiTNode(int x = -1);
    void Visit();
    void PreOrderTraverse();
    void PreOrderTraverse2();
    void InOrderTraverse();
    void InOrderTraverse2();
    void PostOrderTraverse();
    void PostOrderTraverse2();
    void LevelTraverse();
    unsigned int Level();
    void InvertLevelTraverse();                                                                                                  
    unsigned int Level2();                                                                                                       
    BiTNode *PreInCreate(int Pre[], int In[], unsigned int first1, unsigned int last1, unsigned int first2, unsigned int last2); 
    bool IsCBT();                                                                                                                
    unsigned int DoubleWSonNodes();                                                                                              
    void SwapSons();                                                                                                             
    void KthPreNode(unsigned int k);                                                                                             
    void DeleteXTree(int X);                                                                                                     
    void PrintParentsOfX(int x);                                                                                                 
    BiTNode *NearestCommonAcenstor(BiTNode *node1, BiTNode *node2);                                                              
    unsigned int GetWidth();                                                                                                     
    BiTNode *LevelToPost(int Pre[], unsigned int node, unsigned int left, unsigned int right, unsigned int PreLen);
    void PreToPost(int Pre[], unsigned int first1, unsigned int last1, int Post[], unsigned int first2, unsigned int last2);
    void TraverseLeafNodes();                                                                                                    
    bool IsSimilar(BiTNode *T);                                                                                                  
    unsigned int GetWPL();  
    /**************BSTree**************/
    BiTNode* BST_Search(int key);
    bool IsInBST(int key);
    static BiTNode* Delete(BiTNode* &T, int key);
private:
    static void KillTree(BiTNode *&T);
    /**************BSTree**************/
    BiTNode* FindMin();
    BiTNode* FindMax();
};

源代码

直接可以运行
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
class BiTNode
{
public:
    int data = -1;
    BiTNode *left, *right;
public:
    explicit BiTNode(int x = -1);
    void Visit();
    void PreOrderTraverse();
    void PreOrderTraverse2();
    void InOrderTraverse();
    void InOrderTraverse2();
    void PostOrderTraverse();
    void PostOrderTraverse2();
    void LevelTraverse();
    unsigned int Level();
    void InvertLevelTraverse();                                                                                                  
    unsigned int Level2();                                                                                                       
    BiTNode *PreInCreate(int Pre[], int In[], unsigned int first1, unsigned int last1, unsigned int first2, unsigned int last2); 
    bool IsCBT(); //是否为完全二叉树                                                                                                               
    unsigned int DoubleWSonNodes();                                                                                              
    void SwapSons();                                                                                                             
    void KthPreNode(unsigned int k);                                                                                             
    void DeleteXTree(int X);                                                                                                     
    void PrintParentsOfX(int x);                                                                                                 
    BiTNode *NearestCommonAcenstor(BiTNode *node1, BiTNode *node2);                                                              
    unsigned int GetWidth();                                                                                                     
    BiTNode *LevelToPost(int Pre[], unsigned int node, unsigned int left, unsigned int right, unsigned int PreLen);
    void PreToPost(int Pre[], unsigned int first1, unsigned int last1, int Post[], unsigned int first2, unsigned int last2);
    void TraverseLeafNodes();                                                                                                    
    bool IsSimilar(BiTNode *T);                                                                                                  
    unsigned int GetWPL();
    /**************BSTree**************/
    BiTNode* BST_Search(int key);
    bool IsInBST(int key);
    static BiTNode* Delete(BiTNode* &T, int key);
private:
    static void KillTree(BiTNode *&T);
    /**************BSTree**************/
    BiTNode* FindMin();
    BiTNode* FindMax();
};

BiTNode::BiTNode(int x)
{
    this->data = x;
    this->left = this->right = nullptr;
}

void BiTNode::KillTree(BiTNode *&T)
{
    if (T)
    {
        KillTree(T->left);
        KillTree(T->right);
        cout << ' ' << T->data;
        free(T);
        T = nullptr;
    }
}

void CreateTree(BiTNode *&T)
{
    int num;
    cin >> num;
    if (!num)
        T = nullptr;
    else
    {
        T = new BiTNode(num);
        CreateTree(T->left);
        CreateTree(T->right);
    }
}

void BiTNode::Visit()
{
    if (this)
    {
        cout << ' ' << this->data;
    }
}

void BiTNode::PreOrderTraverse()
{
    if (this)
    {
        cout << this->data << ' ';
        this->left->PreOrderTraverse();
        this->right->PreOrderTraverse();
    }
}

void BiTNode::PreOrderTraverse2()
{
    if (!this)
    {
        cout << "Empty Tree";
        return;
    }
    stack<BiTNode *> s;
    s.push(this);
    while (!s.empty())
    {
        BiTNode *tmp = s.top();
        s.pop();
        tmp->Visit();
        if (tmp->right)
        {
            s.push(tmp->right);
        }
        if (tmp->left)
        {
            s.push(tmp->left);
        }
    }
}

void BiTNode::InOrderTraverse()
{
    if (this)
    {
        this->left->InOrderTraverse();
        this->Visit();
        this->right->InOrderTraverse();
    }
}

void BiTNode::InOrderTraverse2()
{
    if (!this)
    {
        cout << "Empty Tree";
        return;
    }
    auto p = this;
    stack<BiTNode *> s;
    while (!s.empty() || p)
    {
        while (p)
        {
            s.push(p);
            p = p->left;
        }
        if (!s.empty())
        {
            p = s.top();
            s.pop();
            p->Visit();
            p = p->right;
        }
    }
}

void BiTNode::PostOrderTraverse()
{
    if (this)
    {
        this->left->PostOrderTraverse();
        this->right->PostOrderTraverse();
        this->Visit();
    }
}

void BiTNode::PostOrderTraverse2()
{
    if (!this)
    {
        cout << "Empty Tree";
        return;
    }
    stack<BiTNode *> s1, s2;
    s1.push(this);
    while (!s1.empty())
    {
        auto p = s1.top();
        s1.pop();
        if (p)
        {
            s2.push(p);
            s1.push(p->left);
            s1.push(p->right);
        }
    }
    while (!s2.empty())
    {
        s2.top()->Visit();
        s2.pop();
    }
}

void BiTNode::LevelTraverse()
{
    if (!this)
    {
        cout << "Empty Tree" << endl;
        return;
    }
    queue<BiTNode *> queue;
    queue.push(this);
    while (!queue.empty())
    {
        auto tmp = queue.front();
        tmp->Visit();
        if (tmp->left)
            queue.push(tmp->left);
        if (tmp->right)
            queue.push(tmp->right);
        queue.pop();
    }
}

unsigned int BiTNode::Level()
{
    if (!this)
        return 0;
    else
    {
        return 1 + (this->left->Level() > this->right->Level() ? this->left->Level() : this->right->Level());
    }
}

void BiTNode::InvertLevelTraverse()
{
    if (!this)
    {
        cout << "Empty Tree" << endl;
        return;
    }
    auto p = this;
    queue<BiTNode *> queue;
    stack<BiTNode *> s;
    queue.push(p);
    while (!queue.empty())
    {
        auto tmp = queue.front();
        //tmp->Visit();
        s.push(tmp);
        if (tmp->left)
            queue.push(tmp->left);
        if (tmp->right)
            queue.push(tmp->right);
        queue.pop();
    }
    while (!s.empty())
    {
        auto q = s.top();
        q->Visit();
        s.pop();
    }
}

unsigned int BiTNode::Level2()
{
    if (!this)
    {
        return 0;
    }
    unsigned int level = 0;
    queue<BiTNode *> Elsa;
    Elsa.push(this);
    auto Anna = Elsa.front();
    while (!Elsa.empty())
    {
        auto p = Elsa.front();
        Elsa.pop();
        if (p->left)
            Elsa.push(p->left);
        if (p->right)
            Elsa.push(p->right);
        if (p == Anna)
        {
            level++;
            Anna = Elsa.back();
        }
    }
    return level;
}

BiTNode *BiTNode::PreInCreate(int Pre[], int In[], unsigned int first1, unsigned int last1, unsigned int first2, unsigned int last2)
{
    BiTNode *root = new BiTNode;
    root->data = Pre[first1];
    unsigned int i;
    for (i = first2; In[i] != root->data; i++)
        ;
    auto LeftTreeLen = i - first2;
    auto RightTreeLen = last2 - i;
    if (LeftTreeLen)
    {
        root->left = PreInCreate(Pre, In, first1 + 1, first1 + LeftTreeLen, first2, first2 + LeftTreeLen - 1);
    }
    else
    {
        root->left = nullptr;
    }
    if (RightTreeLen)
    {
        root->right = PreInCreate(Pre, In, last1 - RightTreeLen + 1, last1, last2 - RightTreeLen + 1, last2);
    }
    else
    {
        root->right = nullptr;
    }
    return root;
}
bool BiTNode::IsCBT()
{ //空树也视作CBT
    if (!this)
        return true;
    queue<BiTNode *> que;
    que.push(this);
    while (!que.empty())
    {
        auto p = que.front();
        que.pop();
        if (p)
        {
            que.push(p->left);
            que.push(p->right);
        }
        else
        {
            while (!que.empty())
            {
                auto s = que.front();
                que.pop();
                if (s)
                    return false;
            }
        }
    }
    return true;
}

unsigned int BiTNode::DoubleWSonNodes()
{
    unsigned int count = 0;
    if (!this)
    {
        return 0;
    }
    else
    {
        queue<BiTNode *> que;
        que.push(this);
        while (!que.empty())
        {
            auto p = que.front();
            que.pop();
            if (p->left && p->right)
                count++;
            if (p->left)
                que.push(p->left);
            if (p->right)
                que.push(p->right);
        }
    }
    return count;
}

void BiTNode::SwapSons()
{
    if (!this)
        return;
    else
    {
        queue<BiTNode *> que;
        queue<BiTNode *> tmp;
        que.push(this);
        while (!que.empty())
        {
            auto p = que.front();
            que.pop();
            if (p->left)
                que.push(p->left);
            if (p->right)
                que.push(p->right);
            tmp.push(p);
        }
        while (!tmp.empty())
        {
            auto q = tmp.front();
            tmp.pop();
            swap(q->left, q->right);
        }
    }
    return;
}

void BiTNode::KthPreNode(unsigned int k)
{
    if (!this)
    {
        cout << "Error! An empty tree!" << endl;
        return;
    }
    else
    {
        if (k == 0)
        {
            cout << "Wrong k" << endl;
        }
        stack<BiTNode *> s;
        stack<BiTNode *> sln;
        s.push(this);
        while (!s.empty())
        {
            auto p = s.top();
            s.pop();
            sln.push(p);
            if (p->right)
            {
                s.push(p->right);
            }
            if (p->left)
            {
                s.push(p->left);
            }
        }
        auto size = sln.size();
        if (k > size)
        {
            cout << "k is out of range!" << endl;
            return;
        }
        else
        {
            while (size > k)
            {
                sln.pop();
                size--;
            }
            cout << sln.top()->data << endl;
        }
    }
}

void BiTNode::DeleteXTree(int X)
{
    if (!this)
        return;
    else
    {
        queue<BiTNode *> que;
        queue<BiTNode *> KillList;
        que.push(this);
        while (!que.empty())
        {
            auto tmp = que.front();
            que.pop();
            if (tmp->left)
                que.push(tmp->left);
            if (tmp->right)
                que.push(tmp->right);
            if (tmp->data == X)
                KillList.push(tmp);
        }
        if (KillList.empty())
        {
            cout << "No node's value is equal to " << X << endl;
            return;
        }
        while (!KillList.empty())
        {
            BiTNode *Killee = KillList.front();
            KillList.pop();
            KillTree(Killee);
        }
        cout << " has been killed" << endl;
    }
    return;
}

void BiTNode::PrintParentsOfX(int x)
{
    auto p = this;
    BiTNode *r = nullptr;
    stack<BiTNode *> s;
    bool flag = false;
    while (p || !s.empty())
    {
        if (p)
        {
            s.push(p);
            p = p->left;
        }
        else
        {
            p = s.top();
            if (p->right && p->right != r)
            {
                p = p->right;
            }
            else
            {
                s.pop();
                if (p->data == x)
                {
                    flag = true;
                    break;
                }
                r = p;
                p = nullptr;
            }
        }
    }
    if (flag)
    {
        while (!s.empty())
        {
            s.top()->Visit();
            s.pop();
        }
    }
    else
    {
        cout << x << " is not in this tree";
    }
}

BiTNode *BiTNode::NearestCommonAcenstor(BiTNode *node1, BiTNode *node2)
{
    if (!this || !node1 || !node2)
    {
        return nullptr;
    }
    else
    {
        queue<BiTNode *> q1, q2;
        auto p1 = this;
        auto p2 = this;
        BiTNode *r1 = nullptr;
        BiTNode *r2 = nullptr;
        stack<BiTNode *> s1, s2;
        bool flag1 = 0;
        bool flag2 = 0;
        while (p1 || !s1.empty())
        {
            if (p1)
            {
                s1.push(p1);
                p1 = p1->left;
            }
            else
            {
                p1 = s1.top();
                if (p1->right && p1->right != r1)
                {
                    p1 = p1->right;
                }
                else
                {
                    s1.pop();
                    if (p1 == node1)
                    {
                        flag1 = 1;
                        break;
                    }
                    r1 = p1;
                    p1 = nullptr;
                }
            }
        }
        while (p2 || !s2.empty())
        {
            if (p2)
            {
                s2.push(p2);
                p2 = p2->left;
            }
            else
            {
                p2 = s2.top();
                if (p2->right && p2->right != r2)
                {
                    p2 = p2->right;
                }
                else
                {
                    s2.pop();
                    if (p2 == node2)
                    {
                        flag2 = 1;
                        break;
                    }
                    r2 = p2;
                    p2 = nullptr;
                }
            }
        }
        if (flag1 && flag2)
        {
            vector<BiTNode *> vec1, vec2;
            while (!s1.empty())
            {
                vec1.push_back(s1.top());
                s1.pop();
            }
            while (!s2.empty())
            {
                vec2.push_back(s2.top());
                s2.pop();
            }
            for (unsigned int k1 = 0; k1 < vec1.size(); k1++)
            {
                for (unsigned int k2 = 0; k2 < vec2.size(); k2++)
                {
                    if (vec1[k1] == vec2[k2])
                    {
                        return vec1[k1];
                    }
                }
            }
            cout << node1->data << " and " << node2->data << " don't have any common Ancestor " << endl;
        }
    }
    return nullptr;
}

unsigned int BiTNode::GetWidth()
{
    if (!this)
        return 0;
    unsigned int Width = 0;
    queue<BiTNode *> que;
    que.push(this);
    auto Anna = que.front();
    while (!que.empty())
    {
        auto tmp = que.front();
        que.pop();
        if (tmp->left)
        {
            que.push(tmp->left);
        }
        if (tmp->right)
        {
            que.push(tmp->right);
        }
        if (Anna == tmp)
        {
            if (que.size() > Width)
            {
                Width = que.size();
            }
            Anna = que.back();
        }
    }
    return Width;
}

BiTNode *BiTNode::LevelToPost(int Pre[], unsigned int node, unsigned int left, unsigned int right, unsigned int PreLen)
{
    BiTNode *root = new BiTNode(Pre[node]);
    unsigned int LeftTreeAncestor = 2 * node;
    unsigned int RightTreeAncestor = 2 * node + 1;
    if (LeftTreeAncestor <= PreLen)
    {
        root->left = LevelToPost(Pre, LeftTreeAncestor, LeftTreeAncestor * 2, LeftTreeAncestor * 2 + 1, PreLen);
    }
    if (RightTreeAncestor <= PreLen)
    {
        root->right = LevelToPost(Pre, RightTreeAncestor, RightTreeAncestor * 2, RightTreeAncestor + 1, PreLen);
    }
    return root;
}

void BiTNode::PreToPost(int Pre[], unsigned int first1, unsigned int last1, int Post[], unsigned int first2, unsigned int last2)
{
    unsigned int half;
    if (last1 >= first1)
    {
        Post[last2] = Pre[first1];
        half = (last1 - first1) / 2;
        PreToPost(Pre, first1 + 1, first1 + half, Post, first2, first2 + half - 1);
        PreToPost(Pre, first1 + half + 1, last1, Post, first2 + half, last2 - 1);
    }
}

void BiTNode::TraverseLeafNodes()
{
    if (this->data == -1)
    {
        cout << "No leaf nodes" << endl;
        return;
    }
    else if (this->data != -1 && !this->left && !this->right)
    {
        this->Visit();
    }
    else{
        queue<BiTNode *> que;
        que.push(this);
        while(!que.empty()){
            auto p = que.front();
            que.pop();
            if(p->left){
                que.push(p->left);
            }
            if(p->right){
                que.push(p->right);
            }
            if(!p->left && !p->right){
                p->Visit();
            }
        }
    }
    return;
}

bool BiTNode::IsSimilar(BiTNode *T){
    bool flag;
    if(!this && !T){
        return true;
    }
    else if(!this || !T){
        return false;
    }
    else{
        return this->left->IsSimilar(T->left) && this->right->IsSimilar(T->right);
    }

}

unsigned int BiTNode::GetWPL() {
    unsigned int WPL = 0;
    if(!this){
        return 0;
    }
    else if(!this->left && !this->right){
        return this->data;
    }
    else{
        auto p = this;
        BiTNode *r = nullptr;
        stack<BiTNode*> s;
        while(!s.empty() || p){
            if(p){
                s.push(p);
                p = p->left;
            }
            else{
                p = s.top();
                if(p->right && p->right != r){
                    p = p->right;
                }
                else{
                    s.pop();
                    if(!p->left && !p->right){
                        stack<BiTNode*> TmpSln = s;
                        WPL += p->data;
                        while(!TmpSln.empty()){
                            WPL += (TmpSln.top()->data);
                            TmpSln.pop();
                        }
                    }
                    r = p;
                    p = nullptr;
                }
            }
        }
    }
    return WPL;
}


/**************ThreadTree**************/
class ThreadNode: virtual public BiTNode
{
public:
    //int data;
    ThreadNode *left, *right;
    bool ltag = 0, rtag = 0;
public:
    void CreateInThread();
    void InOrder();
    static void CreateTree(ThreadNode* &T);
private:
    explicit ThreadNode(int x = -1);
    void InThread(ThreadNode* &pre){
        if(this){
            this->left->InThread(pre);
            if(!this->left){
                this->left = pre;
                this->ltag = 1;
            }
            if(pre && !pre->right){
                pre->right = this;
                pre->rtag = 1;
            }
            pre = this;
            this->right->InThread(pre);
        }
    }
    ThreadNode* FirstNode(){
        auto p = this;
        while(p->ltag == 0){
            p = p->left;
        }
        return p;
    }
    ThreadNode* NextNdoe(){
        auto p = this;
        if(p->rtag == 0){
            return p->right->FirstNode();
        }
        else{
            return p->right;
        }
    }
};
ThreadNode::ThreadNode(int x){
    this->left = this->right = nullptr;
    this->data = x;
}
void ThreadNode::CreateInThread(){
    ThreadNode *pre = nullptr;
    if(this){
        this->InThread(pre);
        pre->right = nullptr;
        pre->rtag = 1;
    }
}
void ThreadNode::InOrder(){
    for(auto p = this->FirstNode(); p; p = p->NextNdoe())
        p->Visit();
}

void ThreadNode::CreateTree(ThreadNode *&T) {
    int num;
    cin >> num;
    if (!num){
        T = nullptr;
    }
    else{
        T = new ThreadNode;
        T->data = num;
        CreateTree(T->left);
        CreateTree(T->right);
    }
}

/**************BSTree**************/

bool BST_Insert(BiTNode* &T, int key){
    if(!T){
        T = new BiTNode(key);
        return true;
    }
    else{
        if(T->data > key){
            return BST_Insert(T->left, key);
        }
        else if(T->data < key){
            return BST_Insert(T->right, key);
        }
        else{
            return false;
        }
    }
}
BiTNode* Create_BST_From_Arr(BiTNode* &T, int Arr[], unsigned int ArrLen){
    T = nullptr;
    for(unsigned int i = 0; i < ArrLen; i++){
        BST_Insert(T, Arr[i]);
    }
}
BiTNode* BiTNode::BST_Search(int key){
    if(!this || key == this->data){
        return this;
    }
    else if(key < this->data){
        return this->left->BST_Search(key);
    }
    else{
        return this->right->BST_Search(key);
    }
}
bool BiTNode::IsInBST(int key){
    return this->BST_Search(key) != nullptr;
}
BiTNode* BiTNode::Delete(BiTNode* &T, int key){
    auto del = T->BST_Search(key);
    if(!del){
        cout << key << " is not in this BST" << endl;
    }
    else if(key < T->data){
        T->left = Delete(T->left, key);
    }
    else if(key > T->data){
        T->right = Delete(T->right, key);
    }
    else{
        BiTNode* tmp;
        if(T->left && T->right){
            tmp = T->right->FindMin();
            T->data = tmp->data;
            T->right = Delete(T->right, tmp->data);
        }
        else{
            if(!T->left)
                T = T->right;
            else if(!T->right)
                T = T->left;
            else{}
        }
    }
}
BiTNode* BiTNode::FindMax() {
    if(!this || (!this->left && !this->right && this)){
        return this;
    }
    else{
        auto tmp = this;
        while(tmp->right){
            tmp = tmp->right;
        }
        return tmp;
    }
}
BiTNode* BiTNode::FindMin() {
    if(!this || (!this->left && !this->right && this)){
        return this;
    }
    else{
        auto tmp = this;
        while(tmp->left){
            tmp = tmp->left;
        }
        return tmp;
    }
}
int main()
{
    BiTNode *T;
    CreateTree(T);
    /**************PreOrderTraverse**************/
    cout << "PreOrderTraverse: ";
    T->PreOrderTraverse();
    cout << endl;
    /**************PreOrderTraverse2**************/
    cout << "PreOrderTraverse2: ";
    T->PreOrderTraverse2();
    cout << endl;
    /**************InOrderTraverse**************/
    cout << "InOrderTraverse: ";
    T->InOrderTraverse();
    cout << endl;
    /**************InOrderTraverse2**************/
    cout << "InOrderTraverse2: ";
    T->InOrderTraverse2();
    cout << endl;
    /**************PostOrderTraverse**************/
    cout << "PostOrderTraverse: ";
    T->PostOrderTraverse();
    cout << endl;
    /**************PostOrderTraverse2**************/
    cout << "PostOrderTraverse2: ";
    T->PostOrderTraverse2();
    cout << endl;
    /**************LevelTraverse**************/
    cout << "LevelTraverse: ";
    T->LevelTraverse();
    cout << endl;
    /**************Level**************/
    cout << "Level: " << T->Level() << endl;
    /**************InvertLevelTraverse**************/
    cout << "InvertLevelTraverse: ";
    T->InvertLevelTraverse();
    cout << endl;
    /**************Level2**************/
    cout << "Level2: " << T->Level2() << endl;
    /**************IsCBT**************/
    cout << "IsCBT: " << (T->IsCBT() ? "yeah" : "nope") << endl;
    /**************DoubleWSonNodes**************/
    cout << "DoubleWSonNodes: " << T->DoubleWSonNodes() << endl;
    /**************SwapSons**************/
    cout << "SwapSons: ";
    T->SwapSons();
    T->LevelTraverse();
    cout << endl;
    T->SwapSons();
    /**************KthPreNode**************/
    unsigned int k;
    cout << "KthPreNode: ";
    cin >> k;
    T->KthPreNode(k);
    /**************DeleteXTree**************/
    /*
    int Killee;
    cout << "DeleteXTree: ";
    cin >> Killee;
    T->DeleteXTree(Killee);
    */
    /**************PrintParentsOfX**************/
    cout << "PrintParentsOf_X: ";
    int n;
    cin >> n;
    T->PrintParentsOfX(n);
    cout << endl;
    /**************NearestCommonAcenstor**************/
    cout << "NearestCommonAcenstor: ";
    //T->NearestCommonAcenstor(T->left->left->left->left, T->left->left->right->right)->Visit();
    T->NearestCommonAcenstor(T->right, T->right->left)->Visit();
    cout << endl;
    /**************PreInCreate**************/
    int Pre[32] = {-1, 1, 2, 4, 8, 16, 17, 9, 18, 19, 5, 10, 20, 21, 11, 22, 23, 3, 6, 12, 24, 25, 13, 26, 27, 7, 14, 28, 29, 15, 30, 31};
    int In[32] = {-1, 16, 8, 17, 4, 18, 9, 19, 2, 20, 10, 21, 5, 22, 11, 23, 1, 24, 12, 25, 6, 26, 13, 27, 3, 28, 14, 29, 7, 30, 15, 31};
    BiTNode *T1 = new BiTNode;
    T1 = T1->PreInCreate(Pre, In, 1, 31, 1, 31);
    cout << "PreInCreate: ";
    T1->LevelTraverse();
    cout << endl;
    cout << "DeleteXTree:";
    T1->DeleteXTree(1); //这里,根节点已经被删掉了,不要再删除了
    //delete T1;
    //T1 = nullptr;
    /**************GetWidth**************/
    cout << "GetWidth: ";
    cout << T->GetWidth() << endl;
    /**************LevelToPost**************/
    cout << "LevelToPost: ";
    int Level[8] = {-1, 1, 2, 3, 4, 5, 6, 7};
    BiTNode *T2 = new BiTNode;
    T2 = T2->LevelToPost(Level, 1, 1, 7, 7);
    T2->LevelTraverse();
    cout << "\nKill T2:";
    T2->DeleteXTree(1);
    /**************PreToPost**************/
    cout << "PreToPost: ";
    int Post[32];
    Post[0] = -1;
    BiTNode *T3 = new BiTNode;
    T3->PreToPost(Pre, 1, 31, Post, 1, 31);
    //T3->DeleteXTree(1);
    for (auto i = 1; i <= 31; i++)
        cout << ' ' << Post[i];
    cout << endl;
    /**************TraverseLeafNodes**************/
    cout << "TraverseLeafNodes: ";
    T->TraverseLeafNodes();
    cout << endl;
    /**************GetWPL**************/
    cout << "GetWPL: " << T->GetWPL() << endl;
    /**************ThreadTree**************/
    cout << "ThreadTree: " << endl;
    cout << "Initializer: ";
    ThreadNode *ThreadT;
    ThreadT->CreateTree(ThreadT);
    ThreadT->CreateInThread();
    cout << "InOrderTraverse: ";
    ThreadT->InOrder();
    cout << "PostOrderTraverse2: ";
    ThreadT->PostOrderTraverse2();
    return 0;
}

测试数据和输出

/*
1 2 3 4 5 6 0 0 0 0 0 0 7 8 10 0 11 12 14 0 0 15 0 0 13 16 0 17 0 0 0 0 9 0 0
1 2 4 8 16 0 0 17 0 35 0 0 9 18 0 0 19 0 0 5 10 20 0 0 21 0 0 11 22 0 0 23 0 0 3 6 12 24 0 0 25 0 0 13 26 0 0 27 0 0 7 14 28 0 0 29 0 0 15 30 0 0 31 0 0
1 2 4 0 0 5 0 0 3 6 0 0 7 0 0
1 0 2 3 0 4 5 0 0 0 0//交错的链表
*/
/*
1 2 4 8 16 0 0 17 0 0 9 18 0 0 19 0 0 5 10 20 0 0 21 0 0 11 22 0 0 23 0 0 3 6 12 24 0 0 25 0 0 13 26 0 0 27 0 0 7 14 28 0 0 29 0 0 15 30 0 0 31 0 0
PreOrderTraverse: 1 2 4 8 16 17 9 18 19 5 10 20 21 11 22 23 3 6 12 24 25 13 26 27 7 14 28 29 15 30 31
PreOrderTraverse2:  1 2 4 8 16 17 9 18 19 5 10 20 21 11 22 23 3 6 12 24 25 13 26 27 7 14 28 29 15 30 31
InOrderTraverse:  16 8 17 4 18 9 19 2 20 10 21 5 22 11 23 1 24 12 25 6 26 13 27 3 28 14 29 7 30 15 31
InOrderTraverse2:  16 8 17 4 18 9 19 2 20 10 21 5 22 11 23 1 24 12 25 6 26 13 27 3 28 14 29 7 30 15 31
PostOrderTraverse:  16 17 8 18 19 9 4 20 21 10 22 23 11 5 2 24 25 12 26 27 13 6 28 29 14 30 31 15 7 3 1
PostOrderTraverse2:  16 17 8 18 19 9 4 20 21 10 22 23 11 5 2 24 25 12 26 27 13 6 28 29 14 30 31 15 7 3 1
LevelTraverse:  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Level: 5

InvertLevelTraverse:  31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Level2: 5
IsCBT: yeah
DoubleWSonNodes: 15
SwapSons:  1 3 2 7 6 5 4 15 14 13 12 11 10 9 8 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
KthPreNode: 31
31
PrintParentsOf_X: 21
10 5 2 1
NearestCommonAcenstor:  1
PreInCreate:  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
DeleteXTree: 16 17 8 18 19 9 4 20 21 10 22 23 11 5 2 24 25 12 26 27 13 6 28 29 14 30 31 15 7 3 1 has been killed
GetWidth: 16
LevelToPost:  1 2 3 4 5 6 7
Kill T2: 4 5 2 6 7 3 1 has been killed
PreToPost:  16 17 8 18 19 9 4 20 21 10 22 23 11 5 2 24 25 12 26 27 13 6 28 29 14 30 31 15 7 3 1
TraverseLeafNodes:  16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
GetWPL: 704
ThreadTree:
Initializer: 1 2 4 8 16 0 0 17 0 0 9 18 0 0 19 0 0 5 10 20 0 0 21 0 0 11 22 0 0 23 0 0 3 6 12 24 0 0 25 0 0 13 26 0 0 27 0 0 7 14 28 0 0 29 0 0 15 30 0 0 31 0 0
InOrderTraverseThreadTree:  16 8 17 4 18 9 19 2 20 10 21 5 22 11 23 1 24 12 25 6 26 13 27 3 28 14 29 7 30 15 31
*/
发布了6 篇原创文章 · 获赞 3 · 访问量 351

猜你喜欢

转载自blog.csdn.net/weixin_44587168/article/details/104218094