【C++】【十一】二叉树递归遍历与非递归遍历的实现及思路

非递归遍历实现思路:
 

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

typedef struct LINKNODE {
	struct LINKNODE* next;
}linknode;

typedef struct LINKLIST {
	linknode head;
	int size;
}stack_list;

#define MY_TRUE 1
#define MY_FALSE 0

typedef struct BinaryNode {
	char ch;
	struct BinaryNode* lchild;
	struct BinaryNode* rchild;
}binarynode;

typedef struct BITREESTACKNODE {
	linknode node;
	BinaryNode* root;
	int flag;
}bitreestacknode;

stack_list* Init_stack_list()
{
	stack_list* stack = (stack_list*)malloc(sizeof(stack_list));
	stack->head.next = NULL;
	stack->size = 0;
	return stack;
}

void Push_stack_list(stack_list* stack, linknode* data)
{
	if (stack == NULL) {
		return;
	}
	if (data == NULL) {
		return;
	}
	data->next = stack->head.next;
	stack->head.next = data;
	stack->size++;
}

void Pop_stack_list(stack_list* stack)
{
	if (stack == NULL) {
		return;
	}
	if (stack->size == 0) {
		return;
	}
	linknode* pnext = stack->head.next;
	stack->head.next = pnext->next;
	stack->size--;
}

linknode* Top_stack_list(stack_list* stack)
{
	if (stack == NULL) {
		return NULL;
	}
	if (stack->size == 0) {
		return NULL;
	}
	return stack->head.next;
}

int Size_stack_list(stack_list* stack)
{
	if (stack == NULL) {
		return -1;
	}
	return stack->size;
}

void Clear_stack_list(stack_list* stack)
{
	if (stack == NULL) {
		return;
	}
	stack->head.next = NULL;
	stack->size = 0;
}

void Free_stack_list(stack_list* stack)
{
	if (stack == NULL) {
		return;
	}
	free(stack);
}



//创建栈中的节点
BITREESTACKNODE* CreatBitreeStackNode(BinaryNode* node,int flag ) {
	BITREESTACKNODE* newnode = (BITREESTACKNODE*)malloc(sizeof(BITREESTACKNODE));
	newnode->root = node;
	newnode->flag = flag;
	return newnode;
}
//递归遍历

void Recursion(BinaryNode* root) {
	if (root == NULL) {

		return;
	}
	printf("%c", root->ch);
	//打印左子树
	Recursion(root->lchild);
	//打印右子树
	Recursion(root->rchild);
}
//非递归遍历
void NonRecursion(BinaryNode* root) {
	stack_list* stack = Init_stack_list();
	Push_stack_list(stack, (linknode*)CreatBitreeStackNode(root, MY_FALSE));

	while (Size_stack_list(stack) > 0) {
	
		//弹出栈顶元素
		bitreestacknode* node = (bitreestacknode*)Top_stack_list(stack);
		Pop_stack_list(stack);

		//判断弹出节点是否为空
		if (node->root == NULL) {
			continue;
		}

		if (node->flag == MY_TRUE) {
			printf("%c", node->root->ch);
		}
		else {
			//当前节点的右节点入栈
			Push_stack_list(stack, (LINKNODE*)CreatBitreeStackNode(node->root->rchild, MY_FALSE));
			//当前节点的左节点入栈
			Push_stack_list(stack, (LINKNODE*)CreatBitreeStackNode(node->root->lchild, MY_FALSE));
			//当前节点的节点入栈
			node->flag = MY_TRUE;
			Push_stack_list(stack, (LINKNODE*)node);
		}
	}


}

void CreatBinaryTree() {
	binarynode node1 = { 'A',NULL,NULL };
	binarynode node2 = { 'B',NULL,NULL };
	binarynode node3 = { 'C',NULL,NULL };
	binarynode node4 = { 'D',NULL,NULL };
	binarynode node5 = { 'E',NULL,NULL };
	binarynode node6 = { 'F',NULL,NULL };
	binarynode node7 = { 'G',NULL,NULL };
	binarynode 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;
	printf("Recursion traverse:\n");
	//递归遍历
	Recursion(&node1);
	printf("\n");
	printf("Non_Recursion traverse:\n");
	//非递归遍历
	NonRecursion(&node1);

	printf("\n");
}

int main()
{
	CreatBinaryTree();
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大江东去浪淘尽千古风流人物

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

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

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

打赏作者

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

抵扣说明:

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

余额充值