二叉树前序遍历栈和递归写法

具体实现看代码

这里采用的是模拟的二叉树 真正的二叉树要递归或者循环创建

链表栈和递归写法

 

#include <iostream>
#include <cstdio>
#include <malloc.h>

using namespace std;
/*
 * 前序遍历栈的实现逻辑
 * 根 左 右 节点入栈并且输出 入到叶子节点 出栈 父节点遍历右子树
 * */

struct treeNode {
    int a; // 数据成员
    struct treeNode *pFather; // 父节点
    struct treeNode *pLeft; // 左孩子
    struct treeNode *pRight; // 右孩子
};

// 链表结构的栈 尾添加尾删除 双向带空头链表
// 栈节点
struct stack {
    struct treeNode *node; // 指向树的节点
    struct stack *pre; // 指向前一个节点
    struct stack *next; // 指向下一个节点
};
struct stack head; // 空头
struct stack *stacktop = &head; // 栈顶指针

// 入栈 链表尾添加
void push(struct treeNode *node) {
    // 申请节点 并且成员赋值
    struct stack *temp = (struct stack *)malloc(sizeof(struct stack));
    if (NULL == temp) {
        return ;
    }
    // 赋初值
    temp->pre = NULL;
    temp->next = NULL;
    temp->node = node;
    // 尾巴连接
    stacktop->next = temp;
    temp->pre = stacktop;
    // 栈顶指针后移
    stacktop = stacktop->next;
}

// 出栈 双向链表尾删除
struct treeNode *pop(void) {
    if (stacktop == &head) {
        // 栈空
        return NULL;
    }
    struct treeNode *temp = stacktop->node; // 得到栈顶的树节点

    stacktop = stacktop->pre; // 节点前移
    free(stacktop->next); // 释放节点
    stacktop->next = NULL; // 新尾指针指向空
    return temp;
}

// 前序遍历 递归写法
void pre_look(struct treeNode *root) {
    if (root != NULL) {
        printf("%d ", root->a);
        pre_look(root->pLeft);
        pre_look(root->pRight);
    }
}

// 前序遍历 栈写法
void preLookByStack(struct treeNode *root) {
    if (NULL == root)
        return ;
    struct treeNode *tp = root;
    // 死循环 里面break
    while (NULL != tp || stacktop != &head) {
        // 左子树入栈并输出 一直到叶子
        while (tp != NULL) {
            printf("%d ", tp->a);
            push(tp);
            tp = tp->pLeft;
        }
        struct treeNode *t = pop(); // 出栈
        tp = t->pRight;
       // if (NULL == tp && stacktop == &head) // 栈为空并且最后一个节点没有孩子
         //   break;
    }
}

int main() {
    struct treeNode t1 = { 1 };
    struct treeNode t2 = { 2 };
    struct treeNode t3 = { 3 };
    struct treeNode t4 = { 4 };
    struct treeNode t5 = { 5 };
    struct treeNode t6 = { 6 };
    struct treeNode t7 = { 7 };
    struct treeNode t8 = { 8 };
    struct treeNode t9 = { 9 };
    struct treeNode t10 = { 10 };

    // 链接
    t1.pLeft = &t2;
    t1.pRight = &t3;
    t1.pFather = NULL;

    t2.pLeft = &t4;
    t2.pRight = &t5;
    t2.pFather = &t1;

    t3.pRight = &t6;
    t3.pLeft = NULL;
    t3.pFather = &t1;

    t4.pLeft = NULL;
    t4.pRight = NULL;
    t4.pFather = &t2;

    t5.pLeft = &t7;
    t5.pRight = &t8;
    t5.pFather = &t2;

    t6.pLeft = &t9;
    t6.pRight = &t10;
    t6.pFather = &t3;

    t7.pLeft = NULL;
    t7.pRight = NULL;
    t7.pFather = &t5;

    t8.pLeft = NULL;
    t8.pRight = NULL;
    t8.pFather = &t5;

    t9.pLeft = NULL;
    t9.pRight = NULL;
    t9.pFather = &t6;

    t10.pLeft = NULL;
    t10.pRight = NULL;
    t10.pFather = &t6;
    printf("递归前序遍历:\n");
    pre_look(&t1);
    printf("\n栈写法前序遍历(双向带空头链表):\n");
    preLookByStack(&t1);

    return 0;
}

前序遍历数组栈写法

//
// Created by Cauchyshy on 2023/5/23.
//

#include <iostream>
#include <cstdio>
using namespace std;

// 树的深度 4 就行 这里我们的例子是4 跟自己的层数一样即可
#define TREE_DEEP 5

struct treeNode {
    int a; // 数据成员
    struct treeNode *pFather; // 父节点
    struct treeNode *pLeft; // 左孩子
    struct treeNode *pRight; // 右孩子
};

// 由于不需要前后指针 直接用树叶指针类型即可 装树几点地址即可
struct treeNode *stack[TREE_DEEP] = {0};

// 用下标最栈顶指示符即可 栈顶
int stacktop = -1; // -1表示空栈 因为下标从0开始 0元素就是一个栈内元素了

// 入栈
void push(struct treeNode *node) {
    if (NULL == node)
        return ;
    stacktop++; // 栈顶标记先自加1
    stack[stacktop] = node; // 然后对栈顶赋值
}

//出栈
struct treeNode * pop(void) {
    if (stacktop == -1)
        return NULL;
    int pre = stacktop;
    stacktop--;
    return stack[pre];
}

// 前序遍历 递归写法
void pre_look(struct treeNode *root) {
    if (root != NULL) {
        printf("%d ", root->a);
        pre_look(root->pLeft);
        pre_look(root->pRight);
    }
}

// 前序遍历数组栈写法
void preLookByArray(struct treeNode *root) {
    if (NULL == root)
        return ;
    struct treeNode *tp = root;
    while (1) {
        // 左子树入栈 一直到叶子
        while (tp != NULL) {
            printf("%d " , tp->a);
            push(tp);
            tp = tp->pLeft;
        }
        if (stacktop == -1)
            break;
        struct treeNode *t = pop();
        tp = t->pRight;
    }
}

int main() {
    struct treeNode t1 = { 1 };
    struct treeNode t2 = { 2 };
    struct treeNode t3 = { 3 };
    struct treeNode t4 = { 4 };
    struct treeNode t5 = { 5 };
    struct treeNode t6 = { 6 };
    struct treeNode t7 = { 7 };
    struct treeNode t8 = { 8 };
    struct treeNode t9 = { 9 };
    struct treeNode t10 = { 10 };

    // 链接
    t1.pLeft = &t2;
    t1.pRight = &t3;
    t1.pFather = NULL;

    t2.pLeft = &t4;
    t2.pRight = &t5;
    t2.pFather = &t1;

    t3.pRight = &t6;
    t3.pLeft = NULL;
    t3.pFather = &t1;

    t4.pLeft = NULL;
    t4.pRight = NULL;
    t4.pFather = &t2;

    t5.pLeft = &t7;
    t5.pRight = &t8;
    t5.pFather = &t2;

    t6.pLeft = &t9;
    t6.pRight = &t10;
    t6.pFather = &t3;

    t7.pLeft = NULL;
    t7.pRight = NULL;
    t7.pFather = &t5;

    t8.pLeft = NULL;
    t8.pRight = NULL;
    t8.pFather = &t5;

    t9.pLeft = NULL;
    t9.pRight = NULL;
    t9.pFather = &t6;

    t10.pLeft = NULL;
    t10.pRight = NULL;
    t10.pFather = &t6;
    printf("递归前序遍历:\n");
    pre_look(&t1);
    printf("\n数组栈前序遍历写法:\n");
    preLookByArray(&t1);


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只微

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值