排序二叉树变为双向链表

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

struct Tree
{
	int nValue;
	Tree* pLeft;
	Tree* pRight;
};


void InsertTree(Tree** tree,int nInsertValue)
{
	if (tree == NULL)
	{
		return;
	}
	if (*tree == NULL)
	{
		(*tree) = (Tree*)malloc(sizeof(Tree));
		(*tree)->nValue = nInsertValue;
		(*tree)->pLeft = NULL;
		(*tree)->pRight = NULL;
		return;
	}

	//  从 根节点比较   找  存放 nInsertValue 的位置
	while(1)
	{
		if (nInsertValue > (*tree)->nValue)   //  看 和 根节点  谁大
		{
			//  放在右
			if((*tree)->pRight == NULL)
			{
				//  放上  结束
				(*tree)->pRight = (Tree*)malloc(sizeof(Tree));
				(*tree)->pRight->nValue = nInsertValue;
				(*tree)->pRight->pLeft = NULL;
				(*tree)->pRight->pRight = NULL;
				return;
			}
			tree = &((*tree)->pRight);
		}
		else
		{
			//  放在左
			if((*tree)->pLeft == NULL)
			{
				//  放上  结束
				(*tree)->pLeft = (Tree*)malloc(sizeof(Tree));
				(*tree)->pLeft->nValue = nInsertValue;
				(*tree)->pLeft->pLeft = NULL;
				(*tree)->pLeft->pRight = NULL;
				return;
			}
			tree = &((*tree)->pLeft);
		}
	}
}


Tree* CreateTree(int* arr,int nLength)
{
	Tree* root = NULL;

	for(int i=0;i<nLength;i++)
		InsertTree(&root,arr[i]);

	return root;
}


void MidPrint(Tree* tree)
{
	if (tree == NULL)
	{
		return;
	}
	MidPrint(tree->pLeft);
	printf("%d ",tree->nValue);
	MidPrint(tree->pRight);
}


void TreeToList(Tree* tree,Tree** pHead,Tree** pEnd)
{
	if (tree == NULL)
	{
		return;
	}

	TreeToList(tree->pLeft,pHead,pEnd);
	//   ---------把这个节点 放到  链表上-------------------
	if ((*pHead)==NULL)
	{
		(*pHead) = tree;
	}
	else
	{
		(*pEnd)->pRight = tree;
		tree->pLeft = (*pEnd);
	}
	(*pEnd) = tree;
	//   ---------------------------------------------------
	TreeToList(tree->pRight,pHead,pEnd);
}


int main()
{

	int arr[10] = {3,2,5,6,7,8,1,0,4,9};

	Tree* tree = CreateTree(arr,10);

	MidPrint(tree);
	

	Tree* pHead = NULL;
	Tree* pEnd = NULL;
	
	TreeToList(tree,&pHead,&pEnd);
	


	printf("\n");
	while(pHead)
	{
		printf("%d ",pHead->nValue);
		pHead = pHead->pRight;
	}	
	printf("\n");
	while(pEnd)
	{
		printf("%d ",pEnd->nValue);
		pEnd = pEnd->pLeft;
	}




	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值