C語言二叉樹建立,遍歷(遞歸與非遞歸),交換子樹

對二叉樹幾乎沒怎麼用過,先學下最簡單的吧

//
//  main.cpp
//  test1
//
//  Created by lj on 13-4-1.
//  Copyright (c) 2013年 lj. All rights reserved.
//

#include<iostream>
#include<queue>
#include<stack>
using namespace std;
//建樹
typedef struct Node
{
    int data;
    Node *lchild,*rchild;
}btree;
btree *create(int a[],int n,int i)
{
    btree *t;
    if(i>n)
        t=NULL;
    else
    {
        t=new btree;
        t->data=a[i-1];
        t->lchild=create(a,n,2*i);
        t->rchild=create(a,n,2*i+1);
    }
    return t;
}
//前序遍歷(遞歸)
void preorder(btree *p)
{
    if(p!=NULL)
    {
        cout<<p->data<<endl;
        preorder(p->lchild);
        preorder(p->rchild);
    }
}
//前序遍歷(非遞歸)
void preorder1(btree *p)
{
    stack<btree *> s;
    while(!s.empty()||p!=NULL)
    {
        while(p!=NULL)
        {
            cout<<p->data<<endl;//該語句在中序遍歷中的位置不同
            s.push(p);
            p=p->lchild;
        }
        p=s.top();
        s.pop();
        p=p->rchild;
    }
}

//中序遍歷(遞歸)
void inorder(btree *p)
{
    if(p!=NULL)
    {
        inorder(p->lchild);
        cout<<p->data<<endl;
        inorder(p->rchild);
    }
}
//中序遍歷(非遞歸)
void inorder1(btree *p)
{
    stack<btree *> s;
    while(!s.empty()||p!=NULL)
    {
        while(p!=NULL)
        {
            s.push(p);
            p=p->lchild;
        }
        p=s.top();
        cout<<p->data<<endl;//該語句在前序遍歷中的位置不同
        s.pop();
        p=p->rchild;
    }
}

//後序遍歷(遞歸)
void postorder(btree *p)
{
    if(p!=NULL)
    {
        postorder(p->lchild);
        postorder(p->rchild);
        cout<<p->data<<endl;
    }
}
//後序遍歷(非遞歸)
struct node
{
    btree *t;
    int flag;
};
void postorder1(btree *p)
{
    stack<node> s;
    node post;
    while(!s.empty()||p!=NULL)
    {
        while(p!=NULL)
        {
            post.t=p;
            post.flag=0;
            s.push(post);
            p=p->lchild;
        }
        if(!s.empty())
        {
            post=s.top();
            s.pop();
            if(post.flag==0)
            {
                post.flag=1;
                s.push(post);
                p=(post.t)->rchild;
            }
            else
            {
                cout<<(post.t)->data<<endl;
                p=NULL;
            }
        }//if
    }//while
}
//層次遍歷(非遞歸)
void layerorder(btree *p)
{
    queue<btree *> q;
    btree *t;
    if(p!=NULL)
        q.push(p);
    while(!q.empty())
    {
        t=q.front();
        cout<<t->data<<endl;
        q.pop();
        if(t->lchild!=NULL)
            q.push(t->lchild);
        if(t->rchild!=NULL)
            q.push(t->rchild);
    }
}

//對二叉樹 t 中所有結點的左右子樹進行交換
void exchange(btree *p)
{
    btree *t;
    if(p!=NULL)
    {
        t=p->lchild;
        p->lchild=p->rchild;
        p->rchild=t;
        exchange(p->lchild);
        exchange(p->rchild);
    }
}
int main()
{
    btree *root;
    int a[5]={1,2,3,4,5};
    root=create(a,5,1);
    cout<<"preorder:"<<endl;
    preorder(root);
    cout<<"preorder1:"<<endl;
    preorder1(root);
    cout<<"inorder:"<<endl;
    inorder(root);
    cout<<"inorder1:"<<endl;
    inorder1(root);
    cout<<"postorder:"<<endl;
    postorder(root);
    cout<<"postorder1:"<<endl;
    postorder1(root);
    cout<<"layerorder:"<<endl;
    layerorder(root);
    exchange(root);
    cout<<"after exchange(layerorder):"<<endl;
    layerorder(root);
    return 0;
}

輸出:

preorder:

1

2

4

5

3

preorder1:

1

2

4

5

3

inorder:

4

2

5

1

3

inorder1:

4

2

5

1

3

postorder:

4

5

2

3

1

postorder1:

4

5

2

3

1

layerorder:

1

2

3

4

5

after exchange(layerorder):

1

3

2

5

4


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章