码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-12-02 12:09:38      阅读:8      评论:0      收藏:0      [点我收藏+]

标签:term   while   null   元素   技术   pre   建立   clear   list()   

此文转载自:https://my.oschina.net/u/4260285/blog/4755128

非递归遍历实现思路:
 技术图片

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

技术图片

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

标签:term   while   null   元素   技术   pre   建立   clear   list()   

原文地址:https://www.cnblogs.com/phyger/p/14048146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!