C++之二叉树的创建、查找、四种次序的周游遍历方法以及内存的释放

数据结构的学习心得,还有很多不足之处。。希望看到的大神们能够给出宝贵的修改意见。《C++之二叉树的创建、查找、四种次序的周游遍历方法以及内存的释放》《C++之二叉树的创建、查找、四种次序的周游遍历方法以及内存的释放》《C++之二叉树的创建、查找、四种次序的周游遍历方法以及内存的释放》

以下为运行结果

前序优先次序周游:

5 0 1 2 3 4 5 6 7 8 9

改进后的前序优先次序周游:

5 0 1 2 3 4 5 6 7 8 9

中序优先次序周游:

0 1 2 3 4 5 5 6 7 8 9

后序优先次序周游:

5 4 3 2 1 0 9 8 7 6 5

改进后的后序优先次序周游:

5 4 3 2 1 0 9 8 7 6 5

广度优先优先次序周游:

5 0 6 1 7 2 8 3 9 4 5

4 11
4存在于此二叉树中

11不存在于此二叉树中

所动态申请的内存已被成功释放

Process returned 0 (0x0)   execution time : 9.945 s
Press any key to continue.

#include <cstdio>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3
#define ll long long
using namespace std;
typedef struct treen * treenode;//定义结构体类型名
struct treen
{
    treenode left;
    treenode right;
    treenode father;
    int data;
};
treenode head;
bool creattree(int firstnum)//创建已申请头指针的二叉树头节点
{
    if(head == nullptr)
    {
        treenode tree2 = new treen;//这一行代码也可以表达成:tree * tree2 = new treen
        tree2->data = firstnum;
        tree2->left = nullptr;
        tree2->right = nullptr;
        tree2->father = nullptr;
        head = tree2;
        return true;
    }
    return false;
}
bool in_sert(int num)//向已创建的头节点中插入数据元素
{
    treenode p = head;
    if(head == nullptr)
    {
        treenode tree = new treen;
        tree->data = num;
        tree->left = nullptr;
        tree->right = nullptr;
        tree->father = nullptr;
        head = tree;
        return true;
    }
    while(1)
    {
        if(num<=p->data)
        {
            if(p->left==nullptr)
            {
                treenode tree= new treen;
                tree->data = num;
                tree->left = nullptr;
                tree->right = nullptr;
                tree->father = p;
                p->left = tree;
                return true;
            }
            p = p->left;
        }
        else if(num>p->data)
        {
            if(p->right==nullptr)
            {
                treenode tree = new treen;
                tree->data = num;
                tree->left = nullptr;
                tree->right = nullptr;
                tree->father = p;
                p->right = tree;
                return true;
            }
            p = p->right;
        }
    }
    return false;
}
bool f_find(int num)//查找变量num代表的数据是否在此二叉树中
{
    treenode p = head;
    if(p == nullptr)
    {
        return false;
    }
    else if(p->data == num)
        return true;
    else
    {
        while(p != nullptr)
        {
            if(num<=p->data)
            {
                p = p->left;
                if(p != nullptr && p->data == num)
                {
                    return true;
                }
            }
            else if(num>p->data)
            {
                p = p->right;
                if(p != nullptr && p->data==num)
                {
                    return true;
                }
            }
        }
        return false;
    }
}
void preorder()//前序优先次序遍历
{
    treenode p = head;
    stack<treenode> sta;
    treenode c;
    if(p == nullptr) return ;
    sta.push(p);
    while(!sta.empty())
    {
        c = sta.top();
        sta.pop();
        if(c != nullptr)
        {
            cout<<c->data<<" ";
            sta.push(c->right);
            sta.push(c->left);
        }
    }
    cout<<endl<<endl;
}
void preorder1()//改进后的前序优先次序遍历
{
    treenode p = head;
    stack<treenode> sta;
    treenode c;
    if(p == nullptr) return ;
    sta.push(p);
    int flag = 1;
    while(!sta.empty())
    {
        c = sta.top();
        sta.pop();
        if(c->left != nullptr && c->right != nullptr)
        {
            cout<<c->data<<" ";
            sta.push(c->right);
            sta.push(c->left);
        }
        else if(c->left != nullptr && c->right == nullptr)
        {
            cout<<c->data<<" ";
            sta.push(c->left);
        }
        else if(c->left == nullptr && c->right != nullptr)
        {
            cout<<c->data<<" ";
            sta.push(c->right);
        }
        else if(c->left == nullptr && c->right == nullptr)
        {
            cout<<c->data<<" ";
        }
    }
    cout<<endl<<endl;
}
void inorder()//中序优先次序遍历
{
    treen t1;
    treenode p = head;
    stack<treenode> sta;
    if(p == nullptr) return ;
    do{
        while(p != nullptr)
        {
            sta.push(p);
            p = p->left;
        }
        p = sta.top();
        sta.pop();
        cout<<p->data<<" ";
        p = p->right;
    }while(p != nullptr || !sta.empty());
    cout<<endl<<endl;
}
void postorder()//后序优先次序遍历
{
    struct node
    {
        int flag;
        treenode t;
    };
    stack<node> sta;
    node w;
    treenode p = head;
    do{
        while(p != nullptr)
        {
            w.t = p;
            w.flag = 1;
            sta.push(w);
            p = p->left;
        }
        while(!sta.empty())
        {
            w = sta.top();
            sta.pop();
            p = w.t;
            if(w.flag == 1)
            {
                w.flag = 2;
                sta.push(w);
                p = p->right;
                break;
            }
            else cout<<p->data<<" ";
        }
    }while(!sta.empty());
    cout<<endl<<endl;
}
void postorder1()//改进后的后序优先次序遍历
{
    stack<treenode> sta;
    treenode p = head;
    while(p != nullptr || !sta.empty())
    {
        while(p != nullptr)
        {
            sta.push(p);
            p = p->left ? p->left : p->right;
        }
        p = sta.top();
        sta.pop();
        cout<<p->data<<" ";
        if(!sta.empty() && sta.top()->left == p)
            p = sta.top()->right;
        else
            p = nullptr;
    }
    cout<<endl<<endl;
}
void levelorder()//广度优先次序遍历,也就是宽度优先搜索遍历
{
    treenode p,pp;
    p = head;
    queue<treenode> que;
    if(p == nullptr) return ;
    que.push(p);
    while(!que.empty())
    {
        p = que.front();
        que.pop();
        cout<<p->data<<" ";
        pp = p->left;
        if(pp != nullptr) que.push(pp);
        pp = p->right;
        if(pp != nullptr) que.push(pp);
    }
    cout<<endl<<endl;
}
bool de_lete()//释放内存
{
    treenode p = head;
    stack<treenode> sta;
    if(p != nullptr)
        sta.push(p);
    while(!sta.empty())
    {
        treenode pp = sta.top();
        sta.pop();
        if(pp->left != nullptr)
        {
            sta.push(pp->left);
        }
        if(pp->right != nullptr)
        {
            sta.push(pp->right);
        }
        delete pp;
    }
    if(!sta.empty()) return false;
    return true;
}
int main()
{
    treenode head = nullptr;
    creattree(5);
    for(int i = 0; i<10; i++)
    {
        in_sert(i);
    }
    cout<<"前序优先次序周游:"<<endl<<endl;
    preorder();
    cout<<"改进后的前序优先次序周游:"<<endl<<endl;
    preorder1();
    cout<<"中序优先次序周游:"<<endl<<endl;
    inorder();
    cout<<"后序优先次序周游:"<<endl<<endl;
    postorder();
    cout<<"改进后的后序优先次序周游:"<<endl<<endl;
    postorder1();
    cout<<"广度优先优先次序周游:"<<endl<<endl;
    levelorder();
    int s,k;
    cin>>s>>k;
    if(f_find(s))
        cout<<s<<"存在于此二叉树中"<<endl<<endl;
    else
        cout<<s<<"不存在于此二叉树中"<<endl<<endl;
    if(f_find(k))
        cout<<k<<"存在于此二叉树中"<<endl<<endl;
    else
        cout<<k<<"不存在于此二叉树中"<<endl<<endl;
    if(de_lete())
        cout<<"所动态申请的内存已被成功释放"<<endl<<endl;
    else cout<<"所动态申请的内存没有成功被释放"<<endl<<endl;
    return 0;
}

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/sxtopc/article/details/52507191
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞