二叉树的存储于基本操作

#include <cstring>

#include <cstdio>

#include <iostream>

#include <queue>

#include <algorithm>

const int maxn = 50;

using namespace std;

struct node{

    int data;

    int layer;

    node* lchild;

    node* rchild;

};

node* root = NULL;

node* newNode(int v)

{

    node* Node = new node;

    Node->data = v;

    Node->lchild = Node->rchild = NULL;

    return Node;

}

void search(node* root, int x,int newdata)

{

    if(root == NULL) return;

    if(root->data == x)

        root->data = newdata;

    search(root->lchild,x,newdata);

    search(root->rchild,x,newdata);

}

void insert(node* &root, int x)

{

    if(root == NULL)

    {

        root = newNode(x);

    }

    if(1) insert(root->lchild,x);

    else insert(root->rchild,x);

}

node *Create(int data[],int n)

{

    node* root = NULL;

    for(int i = 0;i<n;i++)

        insert(root, data[i]);

    return root;

}

void preOrder(node* root){

    if(root == NULL)

        return;

    printf("%d ",root->data);

    preOrder(root->lchild);

    preOrder(root->rchild);

}

void inOrder(node* root)

{

    if(root == NULL) return;

    preOrder(root->lchild);

    printf("%d ",root->data);

    preOrder(root->rchild);

}

void postOrder(node* root)

{

    if(root == NULL) return;

    postOrder(root->lchild);

    postOrder(root->rchild);

    printf("%d ",root->data);

}

void layerOrder(node* root)

{

    queue<node*>Q;

    root->layer = 1;

    Q.push(root);

    while(!Q.empty())

    {

        node* top = Q.front();

        Q.pop();

        printf("%d ",top->data);

        if(top->lchild != NULL){

            top->lchild->layer = top->layer + 1;

            Q.push(top->lchild);

        }

        if(top->rchild != NULL) {

            top->rchild->layer = top->layer + 1;

            Q.push(top->rchild);

        }

    }

}

//根据二叉树的先序遍历和中序遍历构建一颗二叉树

int pre[maxn],in[maxn],post[maxn],n;

node* create(int preL, int preR, int inL, int inR)

{

    if(preL > preR) return NULL;

    node* root = new node;

    root->data = pre[preL];

    int k = 0;

    for(k = inL;k<=inR;k++)

        if(in[k] == pre[preL])

            break;

    int numLeft = k - inL;

    root->lchild = create(preL+1, preL+numLeft, inL, k-1);

    root->rchild = create(preL+numLeft+1, preR, k+1, inR);

    return root;

}

//根据二叉树的后序遍历和中序遍历构建一颗二叉树

node* create1(int postL, int postR, int inL, int inR)

{

    if(postL > postR) return NULL;

    node* root = new node;

    root->data = post[postR];

    int k = 0;

    for(k = inL;k<=inR;k++)

        if(in[k] == post[postR])

            break;

    int numLeft = k - inL;

    root->lchild = create1(postL, postL+numLeft-1, inL, k-1);

    root->rchild = create1(postL+numLeft, postR-1, k+1, inR);

    return root;

}

int num;

void BFS(node* root)

{

    queue<node*>Q;

    Q.push(root);

    while(!Q.empty())

    {

        node* top = Q.front();

        Q.pop();

        printf("%d",top->data);

        num++;

        if(num < n) printf(" ");

        if(top->lchild != NULL) Q.push(top->lchild);

        if(top->rchild != NULL) Q.push(top->rchild);

    }

}

struct node1{

    int data;

    int lchild;

    int rchild;

}Node[maxn];

int index1;

int newNode1(int v)

{

    Node[index1].data = v;

    Node[index1].lchild = -1;

    Node[index1].rchild = -1;

    return index1++;

}

void search1(int root, int x, int newdata)

{

    if(root == -1)

        return;

    if(Node[root].data == x)

        Node[root].data = newdata;

    search1(Node[root].lchild, x, newdata);

    search1(Node[root].rchild, x, newdata);

}

void insert1(int &root, int x)

{

    if(root == -1)

    {

        root = newNode1(x);

        return;

    }

    if(1) insert1(Node[root].lchild, x);

    else insert1(Node[root].rchild, x);

}

int Create1(int data[], int n)

{

    int root = -1;

    for(int i = 0;i<n;i++)

        insert1(root, data[i]);

    return root;

}

void preOrder1(int root)

{

    if(root == -1)

        return;

    printf("%d ",Node[root].data);

    preOrder1(Node[root].lchild);

    preOrder1(Node[root].rchild);

}

void inOrder1(int root)

{

    if(root == -1)

        return;

    inOrder1(Node[maxn].lchild);

    printf("%d ",Node[root].data);

    inOrder1(Node[maxn].rchild);

}

void postOrder1(int root)

{

    if(root == -1) return;

    postOrder1(Node[root].lchild);

    postOrder1(Node[root].rchild);

    printf("%d ",Node[root].data);

}

void layerOrder1(int root)

{

    queue<int> Q;

    Q.push(root);

    while (!Q.empty()) {

        int top = Q.front();

        Q.pop();

        printf("%d ",Node[top].data);

        if(Node[top].lchild != -1) Q.push(Node[top].lchild);

        if(Node[top].rchild != -1) Q.push(Node[top].rchild);

    }

}

int main()

{

    scanf("%d",&n);

    for(int i = 0;i<n;i++)

        scanf("%d",&post[i]);

    for(int i = 0;i<n;i++)

        scanf("%d",&in[i]);

    node* root = create1(0, n-1, 0, n-1);

    BFS(root);

    return 0;

}

原文地址:https://www.cnblogs.com/longxue1991/p/12199988.html