二叉树遍历--二叉链表借助工作栈非递归实现

本文介绍了一种使用栈实现二叉树的先序、中序及后序遍历的方法。通过具体的C语言代码示例,详细展示了如何利用栈结构辅助完成不同类型的遍历过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>

typedef struct treeNode* Node;
typedef struct treeNode Elem;
struct treeNode{
Node lChild,rChild;
int data;
};
typedef Node BTree;

typedef struct stack* StackElem;
struct stack{
Node data;
StackElem next;
};
typedef StackElem Stack;

void push(Stack* stack , Node node){
StackElem elem = (StackElem)malloc(sizeof(StackElem));
elem->data = node;
elem->next = *stack;
*stack = elem;
}

void getTop(Stack *stack,Node *node){
*node = NULL;
if(*stack == NULL){return;}
StackElem elem = *stack;
*node = elem->data;

}

void pop(Stack* stack , Node* node){
*node = NULL;
if(*stack == NULL){return;}
StackElem elem = *stack;
*stack = elem->next;
*node = elem->data;
}

void visitNode(Node node){
if(node == NULL){
printf("node is null , data not exists");
}
printf("%d \t\t",node->data);
}

void createNode(Node* node,int data){
*node = (Node)malloc(sizeof(Elem));
(*node)->data= data;
(*node)->lChild = NULL;
(*node)->rChild = NULL;
}

void preOrderBTree(Stack* stack){
while((*stack)!=NULL){
Node node = NULL;
pop(stack,&node);
while(node != NULL){
visitNode(node); //访问根节点
push(stack,node->rChild); //防止丢失右子树,将右子树入栈
node = node->lChild; //继续访问左子树
}
}
}

void inOrderBTree(Stack* stack){
Node node = NULL;
pop(stack,&node);
while(*stack != NULL || node != NULL){
while(node != NULL){
push(stack,node);
node = node ->lChild;
}
pop(stack,&node);
if(node != NULL){
printf("%d \t\t",node->data);
node = node->rChild;
}
}
/**
1.先左子树到底。逐步压栈
2.出栈获取节点数据。
3.如果节点有右子树,重复1. 2. 步
*/
}

void postOrderBTree(Stack* stack){
/**
Node node = NULL;
Node temp = NULL;
pop(stack,&node);
while(*stack != NULL || node != NULL){
getTop(stack,&temp);
if(temp != node){
while(node != NULL){
push(stack,node);
node = node ->lChild;
}
}else{
pop(stack,&node);
if(node != NULL){
printf("%d \t\t",node->data);
}
}
getTop(stack,&node);
if(node->rChild != NULL ){
node= node ->rChild;
}else{
pop(stack,&node);
if(node != NULL){
printf("%d \t\t",node->data);
//node = node->rChild;
getTop(stack,&node);
//node = node ->rChild;
}
}
}
*/
}


void main(){
//构建二叉树
BTree tree = NULL;
Node node1 = NULL;
Node node2 = NULL;
Node node3 = NULL;
Node node4 = NULL;
Node node5 = NULL;
Node node6 = NULL;
Node node7 = NULL;
Node node8 = NULL;
Node node9 = NULL;
Node node10 = NULL;
//初始化节点
createNode(&node1,2);
createNode(&node2,1);
createNode(&node3,3);
createNode(&node4,5);
createNode(&node5,8);
createNode(&node6,10);
createNode(&node7,2);
createNode(&node8,100);
createNode(&node9,20);
createNode(&node10,25);

//按照预定结构组装成书
node6->lChild = node9;
node6->rChild = node10;
node4->lChild = node7;
node5->rChild = node8;
node2->lChild = node4;
node2->rChild = node5;
node3->rChild = node6;
node1->lChild = node2;
node1->rChild = node3;
tree = node1;
Stack stack = NULL;
// initial(&stack);
push(&stack,tree); // 统一处理,将根节点保存到栈中
//先序遍历
preOrderBTree(&stack);
printf("\n===============================================================\n");
//后序遍历
stack = NULL;
push(&stack,tree);
postOrderBTree(&stack);
printf("\n===============================================================\n");
//中序遍历
inOrderBTree(&stack);
//层级遍历
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值