非递归实现二叉树遍历.c

#include "BTree.h"
#include <stdio.h>
#include "LinkQueue.h"
#include "LinkStack.h"

void printData(BTreeNode* node)
{
	printf ("%c\n", node->data);
}

void pre_order(BTreeNode *root)
{
	if (root == NULL)
		return;
	
	printf ("%4c", root->data);
	pre_order (root->lchild);
	pre_order (root->rchild);
}

// 非递归方式实现前序遍历
void pre1(BTreeNode *root)
{
	if(root == NULL)
		return;
	
	LinkStack *stack =  CreateStack();
	BTreeNode* p = root;
	
	while (p != NULL || !StackEmpty(stack))
	{
		while(p)
		{
			printf ("%4c", p->data);
			Push(stack, p);
			p = p->lchild;
		}
		
		if (!StackEmpty(stack))
		{
			Pop (stack, &p);
			p = p->rchild;
		}
	}
}

void mid_order(BTreeNode *root)
{
	if (root == NULL)
		return;
	
	mid_order (root->lchild);
	printf ("%4c", root->data);
	mid_order (root->rchild);
}

// 非递归方式实现中序遍历
void mid1(BTreeNode *root)
{
	if(root == NULL)
		return;
	
	LinkStack *stack =  CreateStack();
	BTreeNode* p = root;
	
	while (p != NULL || !StackEmpty(stack))
	{
		while(p)
		{
			Push(stack, p);
			p = p->lchild;
		}
		
		if (!StackEmpty(stack))
		{
			Pop (stack, &p);
			printf ("%4c", p->data);
			p = p->rchild;
		}
	}
}

void last_order(BTreeNode *root)
{
	if (root == NULL)
		return;
	
	last_order (root->lchild);
	
	last_order (root->rchild);
	printf ("%4c", root->data);
}

void level_order(BTreeNode *root)
{
	if (root == NULL)
		return;
	
	LinkQueue * queue = CreateQueue();
	EnQueue (queue, root);  // 根节点进队
	
	while (!QueueEmpty(queue))
	{
		// 队头元素出队
		BTreeNode* p = NULL;
		DeQueue(queue, &p);
		printf ("%4c", p->data);
		if (p->lchild != NULL)
			EnQueue(queue, p->lchild);  // 左节点入队
		
		if (p->rchild != NULL)
			EnQueue(queue, p->rchild);  // 右节点入队
	}
	
	free(queue);
}

int main()
{
	BTree * btree = Create_Btree();
	if (btree == NULL)
		return -1;
	
	printf ("二叉树创建成功\n");
	
	Btree_Insert(btree, 'A', 0, 0, 0);
	Btree_Insert(btree, 'B', 0, 1, 0);
	Btree_Insert(btree, 'C', 1, 1, 0);
	Btree_Insert(btree, 'D', 0, 2, 0);
	Btree_Insert(btree, 'E', 2, 2, 0);
	Btree_Insert(btree, 'H', 3, 2, 0);
	Btree_Insert(btree, 'G', 0, 3, 0);
	Btree_Insert(btree, 'F', 6, 3, 0);
	
	DisPlay(btree, printData, 4, '-');
	printf ("前序遍历:\n");
	pre_order (btree->root);
	printf ("\n");
	pre1(btree->root);
	printf ("\n");
	
	printf ("中序遍历:\n");
	mid_order (btree->root);
	printf ("\n");
	mid1(btree->root);
	printf ("\n");
	
	printf ("后序遍历:\n");
	last_order (btree->root);
	printf ("\n");
	
	printf ("层次遍历:\n");
	level_order (btree->root);
	printf ("\n");
	
#if 0	
	//Delete(btree, 0, 1);
	printf ("--------------------------\n");
	//DisPlay(btree, printData, 4, '-');
	BTreeNode* node  = BTree_Get(btree, 0, 1);
	printf ("%c\n", node->data);
	printf ("height : %d\n", BTree_Height(btree));
	printf ("degree : %d\n", BTree_Degree(btree));

	BTree_Clear(btree);
	DisPlay(btree, printData, 4, '-');
#endif
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值