调试二叉树各种遍历算法,第七章,实验题2

1 篇文章 0 订阅
1 篇文章 0 订阅

二叉树的遍历

递归先序中序后序非递归先序中序后序
调试二叉树各种遍历算法,第七章,实验题2(JXUST)

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

/*
 * 二叉树的定义和各种运算部分
 */
#define MaxSize 100
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 << ")";
        }
    }
}

/*
 * 队列定义和各种算法部分
 */

typedef struct {
    BTNode* data[MaxSize];
    int front, rear;
}SqQueue;
void InitQueue(SqQueue*& q)
{
    q = (SqQueue*)malloc(sizeof(SqQueue));
    q->front = q->rear = 0;
}

void DestroyQueue(SqQueue*& q)
{
    free(q);
}

bool QueueEmpty(SqQueue* q)
{
    return (q->front == q->rear);
}

bool enQueue(SqQueue*& q, BTNode* e)
{
    if((q->rear + 1) % MaxSize == q->front)
    {
        return false;
    }
    q->rear = (q->rear + 1) % MaxSize;
    q->data[q->rear] = e;
    return true;
}

bool deQueue(SqQueue*& q, BTNode*& e)
{
    if(q->front == q->rear)
    {
        return false;
    }
    q->front = (q->front + 1) % MaxSize;
    e = q->data[q->front];
    return true;
}


/*
 * 栈定义部分
 */

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;
}




/*
 *   2.调试二叉树各种遍历算法,第七章,实验题2
 */
#include <iostream>
#include <stdio.h>
using namespace std;
#include "btreeadd.cpp"

/*
 * 遍历
 */

void PreOrder(BTNode* b)   //递归先序遍历
{
    if (b != NULL)
    {
        printf("%c ",b->data);
        PreOrder(b->lchild);
        PreOrder(b->rchild);
    }
}

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

void InOrder(BTNode* b)   //递归中序遍历
{
    if (b != NULL)
    {
        InOrder(b->lchild);
        printf("%c ",b->data);
        InOrder(b->rchild);
    }
}

void InOrder1(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);
            printf("%c ",p->data);
            p = p->rchild;
        }
    }
    cout << endl;
    DestroyStack(st);
}

void PostOrder(BTNode* b)   //递归后序遍历
{
    if (b != NULL)
    {
        PostOrder(b->lchild);
        PostOrder(b->rchild);
        printf("%c ",b->data);
    }
}

void PostOrder1(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)
            {
                printf("%c ",p->data);
                Pop(st, p);
                r = p;
            }
            else
            {
                p = p->rchild;
                flag = false;
            }
        }
    } while (!StackEmpty(st));
    printf("\n");
    DestroyStack(st);
}

/*
 * 层次遍历
 */

void LevelOrder(BTNode* b)
{
    BTNode* p;
    SqQueue* qu;
    InitQueue(qu);
    if (b != nullptr)
    {
        enQueue(qu, b);
        while (!QueueEmpty(qu))
        {
            deQueue(qu, p);
            printf("%c ",p->data);
            if (p->lchild != NULL)
                enQueue(qu, p->lchild);
            if (p->rchild != NULL)
                enQueue(qu, p->rchild);
        }
    }

}


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

    printf("递归先序遍历:\t");PreOrder(b);
    printf("\n");
    printf("非递归先序遍历:\t");PreOrder1(b);

    printf("递归中序遍历:\t");InOrder(b);
    printf("\n");
    printf("非递归中序遍历:\t");InOrder1(b);

    printf("递归后序遍历:\t");PostOrder(b);
    printf("\n");
    printf("非递归后序遍历:\t");PostOrder1(b);
    printf("层次遍历:\t");LevelOrder(b);
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值