二叉树的建立(先序+兄弟)和遍历(先序+中序+后序+层序)

//这一行删不了,好气啊,这个编辑器太水了。。。

大家先来看看二叉树的遍历顺序

QQ图片20180503152138.png

如果还没理解,这里有一篇不错的博客:看懂二叉树的三种遍历


我们来简单谈谈二叉树:其实二叉树可以用结构体指针来实现,也可以用结构体数组来实现(父节点为tree[k],左儿子为tree[2*k+1], 右儿子为tree[2*k+2])

结构体指针实现二叉树


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define sa(a) scanf("%d", &a);
#define bug printf("--------");
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int mod  = 1e9+7;

int x;

struct node
{
    int data;
    node *l; //指向左儿子
    node *r; //指向右儿子
    node() {
        l = NULL;
        r = NULL;
    }
};

//先序法建树(dfs)
void pre_build(node* &T)
{
    T = new node;
    cin>>x;
    if(x != -1) { //我们假设输入-1为结束分支
        T->data = x;
        pre_build(T->l);
        pre_build(T->r);
    }else T = NULL;
}

//兄弟(层序)法建树(bfs)
void bre_build(node* &T)
{
    queue<node*> q;
    T = new node;
    cin>>x;
    if(x != -1) {
        T->data = x;
        q.push(T);
    }
    while(!q.empty()) {
        node *p;
        p = q.front();
        q.pop();
        if(p->data != -1) {
            cin>>x;
            node *L;
            L = new node;
            if(x != -1) {
                L->data = x;
                q.push(L);
            }else L = NULL;
            p->l = L;
            cin>>x;
            node *R;
            R = new node;
            if(x != -1) {
                R->data = x;
                q.push(R);
            }else R = NULL;
            p->r = R;
        }
    }
}

//先序遍历(根左右)
void pre_vis(node *T)
{
    if(T) {
        cout<<T->data<<" ";
        pre_vis(T->l);
        pre_vis(T->r);
    }
}

//中序遍历(左根右)
void mid_vis(node *T)
{
    if(T) {
        mid_vis(T->l);
        cout<<T->data<<" ";
        mid_vis(T->r);
    }
}

//后序遍历(左右根)
void post_vis(node *T)
{
    if(T) {
        post_vis(T->l);
        post_vis(T->r);
        cout<<T->data<<" ";
    }
}

//层序遍历(每层从左往右)
void cen_vis(node* &T)
{
    queue<node*> q;
    q.push(T);
    while(!q.empty()) {
        node *p;
        p = q.front();
        q.pop();
        if(p) {
            cout<<p->data<<" ";
            q.push(p->l);
            q.push(p->r);
        }
    }
}

int main()
{
    node *T1;
    node *T2;
    T1 = NULL;
    T2 = NULL;
    cout<<"先序遍历建树"<<endl;
    pre_build(T1);
    cout<<"先序遍历:"<<endl;
    pre_vis(T1);
    cout<<endl<<endl;
    cout<<"中序遍历:"<<endl;
    mid_vis(T1);
    cout<<endl<<endl;
    cout<<"后序遍历:"<<endl;
    post_vis(T1);
    cout<<endl<<endl;
    cout<<"层序遍历:"<<endl;
    cen_vis(T1);
    cout<<endl<<endl;

    cout<<"兄弟法建树(类似层序)"<<endl;
    bre_build(T2);
    cout<<"先序遍历:"<<endl;
    pre_vis(T2);
    cout<<endl<<endl;
    cout<<"中序遍历:"<<endl;
    mid_vis(T2);
    cout<<endl<<endl;
    cout<<"后序遍历:"<<endl;
    post_vis(T2);
    cout<<endl<<endl;
    cout<<"层序遍历:"<<endl;
    cen_vis(T2);
    cout<<endl<<endl;
    return 0;
}

/*
测试数据,大家自己输入运行试试

1 2 3 -1 -1 4 -1 -1 5 6 -1 -1 7 -1 -1

1 2 5 3 4 6 7 -1 -1 -1 -1 -1 -1 -1 -1
*/

结构体数组实现二叉树

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define sa(a) scanf("%d", &a);
#define bug printf("--------");
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int mod  = 1e9+7;

int x;

struct node
{
    int val;
}tree[1010*4];

//先序法建树(dfs)
void pre_build(int k) //根节点为1开始
{
    cin>>x;
    tree[k].val = x;
    if(x != -1) { //我们假设输入-1为结束分支
        pre_build(2*k+1);
        pre_build(2*k+2);
    }
}

//兄弟(层序)法建树(bfs)
void bre_build(int k)
{
    queue<int> q;
    q.push(k);
    while(!q.empty()) {
        int cur;
        cur = q.front();
        q.pop();
        cin>>x;
        tree[cur].val = x;
        if(x != -1) {
            q.push(2*cur+1);
            q.push(2*cur+2);
        }
    }
}

//先序遍历(根左右)
void pre_vis(int k)
{
    if(tree[k].val != -1) {
        cout<<tree[k].val<<" ";
        pre_vis(2*k+1);
        pre_vis(2*k+2);
    }
}

//中序遍历(左根右)
void mid_vis(int k)
{
    if(tree[k].val != -1) {
        mid_vis(2*k+1);
        cout<<tree[k].val<<" ";
        mid_vis(2*k+2);
    }
}

//后序遍历(左右根)
void post_vis(int k)
{
    if(tree[k].val != -1) {
        post_vis(2*k+1);
        post_vis(2*k+2);
        cout<<tree[k].val<<" ";
    }
}

//层序遍历(每层从左往右)
void cen_vis(int k)
{
    queue<int> q;
    q.push(k);
    while(!q.empty()) {
        int cur;
        cur = q.front();
        q.pop();
        if(tree[cur].val != -1) {
            cout<<tree[cur].val<<" ";
            q.push(2*cur+1);
            q.push(2*cur+2);
        }
    }
}

int main()
{
    cout<<"先序遍历建树"<<endl;
    pre_build(1);
    cout<<"先序遍历:"<<endl;
    pre_vis(1);
    cout<<endl<<endl;
    cout<<"中序遍历:"<<endl;
    mid_vis(1);
    cout<<endl<<endl;
    cout<<"后序遍历:"<<endl;
    post_vis(1);
    cout<<endl<<endl;
    cout<<"层序遍历:"<<endl;
    cen_vis(1);
    cout<<endl<<endl;

    cout<<"兄弟法建树(类似层序)"<<endl;
    bre_build(1);
    cout<<"先序遍历:"<<endl;
    pre_vis(1);
    cout<<endl<<endl;
    cout<<"中序遍历:"<<endl;
    mid_vis(1);
    cout<<endl<<endl;
    cout<<"后序遍历:"<<endl;
    post_vis(1);
    cout<<endl<<endl;
    cout<<"层序遍历:"<<endl;
    cen_vis(1);
    cout<<endl<<endl;
    return 0;
}
/*
测试数据

1 2 3 -1 -1 4 -1 -1 5 6 -1 -1 7 -1 -1

1 2 5 3 4 6 7 -1 -1 -1 -1 -1 -1 -1 -1

*/


猜你喜欢

转载自blog.csdn.net/i_believe_cwj/article/details/80178218