数据结构 第七章 实验题2 实现二叉树的各种遍历算法


实验题目:
    实现二叉树各种遍历算法
实验目的:
     领会二叉树的各种遍历过程以及遍历算法设计
实验内容:
      设计程序,实现二叉树的先序遍历、中序遍历和后序遍历的
      递归和非递归算法,以及层次遍历的算法。
 

 

二叉树各种基本运算的代码btree.h

//
// Created by liushihao on 19-4-26.
//
#include "iostream"
#define Maxsize 100
using namespace std;
typedef char ElemType;

typedef struct node
{
    ElemType data;
    struct node *lchild;
    struct node *rchild;
}BTNode;



void CreateBTree(BTNode * &b, char *str) //创造二叉树
{
    BTNode *St[Maxsize], *p;
    int top=-1,k,j=0;
    char ch;
    b=NULL;
    ch=str[j];
    while(ch!='\0')
    {
        switch (ch)
        {
            case '(':top++;St[top]=p;k=1;break;
            case ')':top--;              break;
            case ',':k=2;                break;
            default:
                p=(BTNode*)malloc(sizeof(BTNode));
                p->data=ch;
                p->lchild=p->rchild=NULL;
                if(b==NULL)
                    b=p;
                else
                {
                    switch(k)
                    {
                        case 1:St[top]->lchild=p;break;
                        case 2:St[top]->rchild=p;break;
                    }
                }
        }
        j++;
        ch=str[j];
    }
}

void DestroyBtree(BTNode *&b)  //销毁二叉树
{
    if(b!=NULL)
    {
        DestroyBtree(b->lchild);
        DestroyBtree(b->rchild);
        free(b);
    }
}

BTNode *FindNode(BTNode *b,ElemType x)
{
    BTNode *p;
    if(b==NULL)
    {
        return NULL;
    }
    else if(b->data==x)
    {
        return b;
    }
    else
    {
        p=FindNode(b->lchild, x);
        if(p!=NULL)
        {
            return p;
        }
        else
        {
            return FindNode(b->rchild, x);
        }
    }
}

BTNode *LchildNode(BTNode *p)
{
    return p->lchild;
}

BTNode *RchildNode(BTNode *p)
{
    return p->rchild;
}

int BTHeight(BTNode *b)
{
    int lchildh,rchildh;
    if(b==NULL) return 0;
    else
    {
        lchildh=BTHeight(b->lchild);
        rchildh=BTHeight(b->rchild);
        return (lchildh>rchildh)? (lchildh+1):(rchildh+1);
    }
}

void DispBTree(BTNode *b)
{
    if(b!=NULL)
    {
        cout<<b->data;
        if(b->lchild!=NULL || b->rchild!=NULL)
        {
            cout<<"(";
            DispBTree(b->lchild);
            if(b->rchild!= NULL) cout<<",";
            DispBTree(b->rchild);
            cout<<")";
        }
    }
}

 

以二叉树结点为元素的栈的定义代码stack.h

//
// Created by liushihao on 19-4-26.
//

#include <iostream>
#include "btree.h"
using namespace std;

#define Maxsize 50
typedef struct
{
    BTNode *data[Maxsize];
    int top;
}SqStack;

void InitStack(SqStack *&s)
{
    s=(SqStack*)malloc(sizeof(SqStack));
    s->top=-1;
}

void DestroyStack(SqStack *&s)
{
    free(s);
}

bool StackEmpty(SqStack *s)
{
    return (s->top==-1);
}

bool Push(SqStack *&s,BTNode *e)
{
    if(s->top==Maxsize-1){
        return false;
    }
    s->top++;
    s->data[s->top]=e;
    return true;
}

bool Pop(SqStack *&s,BTNode* &e)
{
    if(s->top==-1)
        return false;
    e=s->data[s->top];
    s->top--;
    return true;
}

bool GetTop(SqStack *s,BTNode* &e)
{
    if(s->top==-1)
        return false;
    e=s->data[s->top];
    return true;
}

 

exp7-2.cpp

//
// Created by liushihao on 19-4-26.
//
#include "stack.h"
void PreOrder_recursion(BTNode *b)   //递归先序遍历
{
    if(b!=NULL)
    {
        cout<<b->data;
        PreOrder_recursion(b->lchild);
        PreOrder_recursion(b->rchild);
    }
}

void PreOrder_not_recursion(BTNode *b) //非递归先序遍历
{
    BTNode *p;
    SqStack *st;
    InitStack(st);
    if(b!=NULL)
    {
        Push(st,b);
        while(!StackEmpty(st))
        {
            Pop(st,p);
            cout<<p->data;
            if(p->rchild!=NULL)
            {
                Push(st,p->rchild);
            }
            if(p->lchild!=NULL)
            {
                Push(st,p->lchild);
            }
        }
        cout<<endl;
    }
    DestroyStack(st);
}

void InOrder_recursion(BTNode *b)   //递归中序遍历
{
    if(b!=NULL)
    {
        InOrder_recursion(b->lchild);
        cout<<b->data;
        InOrder_recursion(b->rchild);
    }
}

void InOrder_not_recursion(BTNode *b) //非递归中序遍历
{
    BTNode *p;
    SqStack *st;
    InitStack(st);
    p=b;
    while(!StackEmpty(st) || p!=NULL)
    {
        while(p!=NULL)
        {
            Push(st,p);
            p=p->lchild;
        }
        if(!StackEmpty(st))
        {
            Pop(st,p);
            cout<<p->data;
            p=p->rchild;
        }
    }
    cout<<endl;
    DestroyStack(st);
}

void PostOrder_recursion(BTNode *b)   //递归后序遍历
{
    if(b!=NULL)
    {
        PostOrder_recursion(b->lchild);
        PostOrder_recursion(b->rchild);
        cout<<b->data;
    }
}

void PostOrder_not_recursion(BTNode *b) //非递归后序遍历
{
    BTNode *p,*r;
    bool flag;
    SqStack *st;
    InitStack(st);
    p=b;
    do{
        while(p!=NULL)
        {
            Push(st,p);
            p=p->lchild;
        }
        r=NULL;
        flag=true;
        while(!StackEmpty(st)&&flag)
        {
            GetTop(st,p);
            if(p->rchild==r)
            {
                cout<<p->data;
                Pop(st,p);
                r=p;
            }
            else
            {
                p=p->rchild;
                flag=false;
            }
        }
    }while (!StackEmpty(st));
    cout<<endl;
    DestroyStack(st);
}



//void LevelOrder(BTNode *b)  层次遍历。。。还在更新
//{
//    BTNode *p;
//    SqQueue *qu;
//    InitQueue(qu);
//    enQueue(qu,b);
//    while(!QueueEmpty(qu))
//    {
//        deQueue(qu,p);
//        cout<<p->data;
//        if(p->lchild!=NULL)
//        {
//            enQueue(qu,p->lchild);
//        }
//        if(p->rchild!=NULL)
//        {
//            enQueue(qu,p->rchild);
//        }
//    }
//}


int main()
{
    BTNode *b;
    char str[]="A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))";
    CreateBTree(b, str);
    cout<<"初始化二叉树为:"<<endl;
    DispBTree(b);
    cout<<endl;

    cout<<"递归先序遍历";PreOrder_recursion(b);
    cout<<endl;
    cout<<"非递归先序遍历";PreOrder_not_recursion(b);

    cout<<"递归中序遍历";InOrder_recursion(b);
    cout<<endl;
    cout<<"非递归中序遍历";InOrder_not_recursion(b);

    cout<<"递归先序遍历";PostOrder_recursion(b);
    cout<<endl;
    cout<<"非递归先序遍历";PostOrder_not_recursion(b);

//    cout<<"层次遍历:";LevelOrder(b);
    return 0;
}

 

上一篇:二叉树的实现(c++实现)


下一篇:python 从深度相机realsense生成pcl点云