线索化二叉树

typedef struct node 
{
	/*ElemType*/int m_Data;
	
	int m_LTag;
	int m_RTag;

	struct node* m_LChild;
	struct node* m_RChild;

} ThreadTreeNode;

ThreadTreeNode* pre = NULL;

ThreadTreeNode* CreateThread(ThreadTreeNode* Tree)
{
	ThreadTreeNode* root = (ThreadTreeNode*) malloc(sizeof(ThreadTreeNode));
	
	root->m_LTag = 0;
	root->m_RTag = 1;
	root->m_RChild = Tree;

	if (Tree == NULL)
	{
		root->LChild = root;
	}
	else
	{
		root->LChild = Tree;
		pre = root;
		
		Thread(Tree);
		
		pre->m_RTag = 1;
		pre->m_RChild = root;
		root->m_RChild = pre;
	}

	return root;
}

void Thread(ThreadTreeNode* &tree)
{
	Thread(tree->m_LChild);

	if (tree->m_LChild != NULL)
	{
		tree->m_LTag = 0;
	}
	else
	{
		tree->m_LTag = 1;
		tree->LChild = pre;
	}

	if (pre->m_RChild != NULL)
	{
		pre->m_RTag = 0;
	}
	else
	{
		pre->m_RTag = 1;
		pre->m_RChild = p;
	}

	pre = p;

	Thread(tree->m_RChild);
}
void InOrderThreadBT(ThreadTreeNode* Tree)
{
	ThreadTreeNode* p = Tree->LChild;

	while (p != Tree)
	{
		while (p->m_LChild != NULL)
		{
			p = p->m_LChild;
		}

		cout << p->m_Data << endl;

		if (p->m_RTag == 1 && p->m_RChild != Tree)
		{
			p = p->m_RChild;
			cout << p->Data;
		}

		p = p->m_RChild;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值