数据结构 二叉树遍历

二叉树非递归遍历
#include <stdio.h>
#include "link_stack.h"
/*
 *二叉树非递归实现:链式栈的应用
*/

#define BT_TRUE    1
#define BT_FALSE   0
//每个节点:存放数据,有左右孩子(没有孩子,为NULL)
typedef struct BinTree{
	char ch;
	struct BinTree* lchild;
	struct BinTree* rchild;
}STBinTree_def;

//自定义数据,每个节点需要有个标识,true(输出)或false(入栈)
typedef struct BTStackNode{
    STNode_def node;
	STBinTree_def* root;
	int flg;
}BTStackNode_def;

//栈中节点创建
BTStackNode_def* bintree_stacknode_create(STBinTree_def* node, int flg)
{
    BTStackNode_def* stack_node = (BTStackNode_def*)malloc(sizeof(BTStackNode_def));
    stack_node->root = node;
    stack_node->flg = flg;
    
    return stack_node;
}

//二叉树递归遍历
void bintree_recursion(STBinTree_def* root)
{
    
    if (root == NULL)
    {
        return;
    }
#if 0
    //先序遍历
    printf("%c", root->ch);
    bintree_recursion(root->lchild);
    bintree_recursion(root->rchild);
#endif

#if 0
    //中序遍历
    bintree_recursion(root->lchild);
    printf("%c", root->ch);
    bintree_recursion(root->rchild);
#endif


#if 1
    //后序遍历
    
    bintree_recursion(root->lchild);
    bintree_recursion(root->rchild);
    printf("%c", root->ch);
#endif
}
//二叉树非递归遍历
void bintree_norecursion(STBinTree_def* root)
{
    //栈初始化
    STLinkStack_def *stack = stack_init();
    
    //将根节点先入栈,并初始化为BT_FALSE
    stack_push(stack, (STNode_def*)bintree_stacknode_create(root, BT_FALSE));
    
    while (stack_size(stack) > 0)
    {
        //获取栈顶元素
        BTStackNode_def *top = (BTStackNode_def*)stack_top(stack);
        
        //弹出栈顶元素
        stack_pop(stack);
        if (top->root == NULL)
        {
            continue;
        }
        if (top->flg == BT_TRUE)
        {
            printf("%c", top->root->ch);
        }
        else 
        {   
#if 0            
            //先序遍历,入栈是逆操作
            //当前节点右节点入栈
            stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->rchild, BT_FALSE));

            //当前节点左节点入栈       
            stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->lchild, BT_FALSE));

            //当前节点入栈
            top->flg = BT_TRUE;
            stack_push(stack, (STNode_def*)top);
#endif

#if 0            
            //中序遍历,入栈是逆操作
            //当前节点右节点入栈
            stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->rchild, BT_FALSE));
            
            //当前节点入栈
            top->flg = BT_TRUE;
            stack_push(stack, (STNode_def*)top);
            
            //当前节点左节点入栈
            stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->lchild, BT_FALSE));
#endif
            
            
#if 1            
            //后序遍历,入栈是逆操作
            //当前节点入栈
            top->flg = BT_TRUE;
            stack_push(stack, (STNode_def*)top);
            //当前节点右节点入栈
            stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->rchild, BT_FALSE));

            //当前节点左节点入栈
            stack_push(stack, (STNode_def*)bintree_stacknode_create(top->root->lchild, BT_FALSE));
#endif
        }
    }
    
}
void bintree_create()
{
    STBinTree_def node1={'A', NULL, NULL};
	STBinTree_def node2={'B', NULL, NULL};
	STBinTree_def node3={'C', NULL, NULL};
	STBinTree_def node4={'D', NULL, NULL};
	STBinTree_def node5={'E', NULL, NULL};
	STBinTree_def node6={'F', NULL, NULL};
	STBinTree_def node7={'G', NULL, NULL};
	STBinTree_def node8={'H', NULL, NULL};
    
    node1.lchild = &node2;
    node1.rchild = &node6;
    
    node2.rchild = &node3;
    node3.lchild = &node4;
    node3.rchild = &node5;
    
    node6.rchild = &node7;
    node7.lchild = &node8;
    
    bintree_recursion(&node1);
    printf("\n");
    bintree_norecursion(&node1);
    printf("\n");
}

int main()
{
	bintree_create();
	return 0;
}
二叉树遍历特性主要包括三种遍历方式:先序遍历、中序遍历和后序遍历。先序遍历是指先访问根节点,然后按照先序遍历的顺序依次访问左子树和右子树。中序遍历是指先访问左子树,然后访问根节点,最后访问右子树。后序遍历是指先访问左子树,然后访问右子树,最后访问根节点。这三种遍历方式都是通过递归或者使用辅助数据结构(如栈或队列)来实现的。其中,递归是一种较为简洁的实现方式,但由于递归的栈帧消耗较大,所以使用非递归的方式来遍历二叉树也是非常有必要的。非递归遍历二叉树可以借助队列的先进先出的特性来实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [数据结构7:基本的二叉树遍历及题目](https://blog.youkuaiyun.com/m0_53607711/article/details/128331361)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [数据结构实验 二叉树遍历方法](https://download.youkuaiyun.com/download/yuan7376313/3174711)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值