【数据结构之二叉树】二叉树的创建、遍历等操作

二叉树的基本操作:

1.创建二叉树

2.销毁二叉树

3.遍历二叉树:1)前序遍历 2)中序遍历 3)后序遍历 4)层次遍历 

4.搜索二叉树

5.删除子叶

6.插入子叶

7.获取左/右子叶的值

8.获取树深度

9.获取叶子结点数

 

1.创建二叉树

这里创建的是链式存储结构的二叉树,包含数据域,左右两结点的指针域;在读取创建树时,以#代替空格,输入格式的规范为:以前序遍历的顺序输入,如果该结点的左子叶为空,则输入#,以此类推;

e.g:

        -
  +         \
a    *    e    f
    b   -
       c  d

(a+b*c-d-e/f)

输入:
-+a##*b##-c##d##/e##f##

#include<iostream>
#include<stdio.h>
#define Max 100
using namespace std;


typedef struct Bit { char data; struct Bit *lchild,*rchild; }Bitnode,*Bitree; //定义二叉树 void CreateBitree(Bitree *t) { char ch; scanf("%c",&ch); if (ch=='#') *t=NULL; else { *t=(Bitree )malloc(sizeof(Bitnode)); if (!(*t)) exit(-1); (*t)->data=ch; CreateBitree(&(*t)->lchild); CreateBitree(&(*t)->rchild); } }

 

2.销毁二叉树

void destroy(Bitree *t)
{
    if (*t)
    {
        if ((*t)->lchild)
            destroy(&(*t)->lchild);
        if ((*t)->rchild)
            destroy(&(*t)->rchild);
        free(*t);
        *t=NULL;
    }
}

 

3.二叉树的遍历

1)前序遍历

//前序遍历 递归
void Preorder(Bitree t)
{
    if (t)
    {
        cout<<t->data;
        Preorder(t->lchild);
        Preorder(t->rchild);
    }
}

//前序遍历 非递归
void Preorder2(Bitree t)
{
    Bitree Stack[Max];
    int top=0;
    Bitnode *p = NULL;
    p=t;

    while (p||top!=0)
    {
        while (p)
        {
            cout<<p->data;
            Stack[top++]=p;
            p=p->lchild;
        }

        if (top!=0)
        {
            p=Stack[--top];
            p=p->rchild;
        }
    }
}

2)中序遍历

//中序遍历 递归
void Inorder(Bitree t)
{
    if (t)
    {
        Inorder(t->lchild);
        cout<<t->data;
        Inorder(t->rchild);
    }
}

//中序遍历 非递归
void Inorder2(Bitree t)
{
    Bitree Stack[Max];
    int top=0;
    Bitnode *p=NULL;
    p=t;

    while (p||top!=0)
    {
        while (p)
        {
            Stack[top++]=p;
            p=p->lchild;
        }
        if (top!=0)
        {
            p=Stack[--top];
            cout<<p->data;
            p=p->rchild;
        }
    }
}

3)后序遍历

//后序遍历 递归
void Postorder(Bitree t)
{
    if (t)
    {
        Postorder(t->lchild);
        Postorder(t->rchild);
        cout<<t->data;
    }
}

//后序遍历 非递归
void Postorder2(Bitree t)
{
    Bitree Stack[Max];
    int top=0;
    Bitnode *p=NULL,*q=NULL;
    p=t;

    while (p||top!=0)
    {
        while (p)
        {
            Stack[top++]=p;
            p=p->lchild;
        }
        if (top!=0)
        {
            p=Stack[top-1];
            if (p->rchild==NULL||p->rchild==q)                    //p的右子树为空或已访问过
            {
                cout<<p->data;
                q=p;
                p=NULL;
                top--;
            }
            else
                p=p->rchild;
        }
    }
}

4)层次遍历

//层次遍历
void Levelorder(Bitree t)
{
    Bitree Q[Max];                //运用队列存储结点指针
    int front=0,rear=0;
    Bitree p=t;
    if (t)
        Q[rear++]=t;
    while (front!=rear)
    {
        p=Q[front++];
        cout<<p->data;

        if (p->lchild)
            Q[rear++]=p->lchild;
        if (p->rchild)
            Q[rear++]=p->rchild;
    }
}

 

4.搜索二叉树(返回结点地址)

Bitree Search(Bitree T,char e)                        //队列查找
{
    Bitnode *p = NULL;
    Bitree Q[Max];                                    //定义一个队列,用于存放二叉树中结点的指针
    int front=0,rear=0;

    if (T)
    {
        Q[rear]=T;
        rear++;

        while (front!=rear)
        {
            p=Q[front];
            front++;
            if (p->data==e)
                return p;
            if (p->lchild)
            {
                Q[rear]=p->lchild;
                rear++;
            }
            if (p->rchild)
            {
                Q[rear]=p->rchild;
                rear++;
            }
            
        }
    }
    return p;
}

 

5.删除子叶

int DeleteChild(Bitree p,int LR)
{
    if (LR==0)                                        //删除左子树
    {
        if (!p)
        {
            DeleteChild(p->lchild,LR);
            return 1;
        }
        return 0;
    }

    if (LR==1)                                        //删除右子树
    {
        if (!p)
        {
            DeleteChild(p->rchild,LR);
            return 1;
        }
        return 0;
    }
}

 

6.插入子叶

int insertChild(Bitree p,Bitree c,int LR)
{
    if (LR==0)                                        //插入左子树
    {
        if (!p)
        {
            c->rchild=p->lchild;                    //将p的左子树作为c的右子树
            p->lchild=c;                            //将c作为p的左子树
            return 1;
        }
    return 0;
    }

    if (LR==1)                                        //插入右子树
    {
        if (!p)
        {
            c->lchild=p->rchild;                    //将p的右子树作为c的左子树
            p->rchild=c;                            //将c作为p的右子树
            return 1;
        }
        return 0;
    }
}

 

7.获取左/右子叶的值

//返回e的左子树的值
char LeftChild(Bitree T,char e)
{
    Bitnode *p=NULL;
    if (T)
    {
        p=Search(T,e);
        if (p&&p->lchild)
            return p->lchild->data;
    }
    return 0;
}

//返回e的右子树的值
char RightChild(Bitree T,char e)
{
    Bitnode *p=NULL;
    if (T)
    {
        p=Search(T,e);
        if (p&&p->rchild)
            return p->rchild->data;
    }
    return 0;
}

 

8.获取树的深度

int Getheight(Bitree t)
{    
    if (t==NULL)
        return 0;
    int lh=Getheight(t->lchild);
    int rh=Getheight(t->rchild);

    return lh>rh?lh+1:rh+1;
}

 

9.获取叶子结点数

注意:叶子结点指的是没有左右子叶的结点

int CountLeaves(Bitree t)
{
    static int count=0;                        //定义静态变量
    if(t!=NULL)  
    {  
        count=CountLeaves(t->lchild);  
        if((t->lchild==NULL)&&(t->rchild==NULL))  
            count=count+1;  
        count=CountLeaves(t->rchild);  
    }  
    return count;  
}

 

posted @ 2016-10-28 18:33  HugoNgai  阅读(541)  评论(0编辑  收藏  举报