小说网 找小说 无限小说 烟雨红尘 幻想小说 酷文学 深夜书屋

基于visual Studio2013解决面试题之0201二叉树转链表




题目



解决代码及点评

/* 
	问题:将二叉树转换成链表

	总体框架:
	1)main函数获取用户输入
	2)通过CreatTree函数把数字放入二叉树
	3)通过pPutInLink函数,遍历树,讲节点通过insert函数放入链表
	4)打印输出
*/

#include <iostream>
using namespace std;

/*
	树节点结构体定义,包括保存的value和两个子树节点
*/
typedef struct BSTree
{
	int nValue;
	struct BSTree *pLChild;
	struct BSTree *pRChild;
}BSTree, *PBSTree;

/*
	CreateTree是创建树以及往树里添加节点
*/
PBSTree  CreatTree(PBSTree root,int a)
{
	// 如果root为空,则直接创建节点,往节点里增加
	if (root==NULL)
	{
		root = (PBSTree)malloc(sizeof(BSTree));
		root->nValue=a;
		root->pLChild=NULL;
		root->pRChild=NULL;
	}
	// 否则比较新插入的值和root节点的值,如果比较小,则插入左子树
	// 如果比较大,则插入右子树
	else if (a<=root->nValue)
	{
		root->pLChild = CreatTree(root->pLChild,a);
	}
	else if (a>root->nValue)
	{
		root->pRChild = CreatTree(root->pRChild,a);
	}
	// 最后返回root
	return root;
}

// 这个函数和CreateBTree函数功能差不多
// 区别处带代码中注释
PBSTree InsertBSTree(PBSTree pRoot, int nValue)
{
	if (pRoot == NULL)
	{
		pRoot = (PBSTree)malloc(sizeof(BSTree));

		pRoot->nValue = nValue;
		pRoot->pLChild = NULL;
		pRoot->pRChild = NULL;
	}
	else
	{
		// 这个函数和CreateBTree的区别在于,它是严格的大于或者小于,如果树中已经有相同节点,则插入失败
		if (pRoot->nValue > nValue)
		{
			pRoot->pLChild = InsertBSTree(pRoot->pLChild, nValue);
		}
		else if (pRoot->nValue < nValue)
		{
			pRoot->pRChild = InsertBSTree(pRoot->pRChild, nValue);
		}
	}

	return pRoot;
}

/*
	这个函数将pRoot放入head,
	此时树节点变成双链表节点,pLChild相当于prev, pRChild相当于next
*/
PBSTree insert(PBSTree head,PBSTree pRoot)   //链表前插,左指针相当于前指针,右指针相当于后指针
{
	if (head==NULL)
	{
		head=pRoot;
		return head;
	}
	else
	{
		pRoot->pRChild=head;  // pRoot->next = head
		head->pLChild=pRoot;  // head->prev = pRoot
		pRoot->pLChild = pRoot; // pRoot->prev = pRoot 这三句话相当于这样,把pRoot节点加入到head链表里
		return pRoot;
	}

}

/*
	中序遍历二叉树
*/
PBSTree pPutInLink(PBSTree pRoot,PBSTree &plink)    //!每次插入同一个链表,需要引用
{
	if (!pRoot)
	{
		return NULL;
	}

	/* 中序遍历左子树 */
	pPutInLink(pRoot->pLChild,plink);

	/* 处理根节点 */
	/* 处理根节点后,右子树根节点就没有东西指向它了,所以先定义临时变量保存 */
	PBSTree pTemp=pRoot->pRChild;
	/* 把根节点保存到链表中 */
	plink=insert(plink,pRoot);

	/* 中序遍历右子树 */
	pPutInLink(pTemp,plink);
	return plink;
}


/* 输出链表 */
PBSTree print(PBSTree head)
{
	PBSTree head2=head;
	while(head2->pRChild!=NULL)
	{
		printf("%d ",head2->nValue);
		head2=head2->pRChild;
	}
	printf("%d ",head2->nValue);
	printf("\npri");
	while(head2->pLChild!=head2)
	{
		printf("%d ",head2->nValue);
		head2=head2->pLChild;
	}
	printf("%d ",head2->nValue);

	return head;
}

int main()
{
	PBSTree root = NULL;
	int a=1;

	// 通过cin对象,从键盘获取用户输入,比如您依次输入 4 3 2 5 1 0
	while (a!=0)
	{
		cout<<"输入插入的数字(0停止插入):";
		cin>>a;
		cout<<endl;
		root=CreatTree(root,a); // 每次输入的数字被插入到树中,插入的数字为4 3 2 5 1 0
	}

	// 在这里作树转链表的操作
	PBSTree plink=NULL;
	plink=pPutInLink(root,plink);
	print(plink);

	// 提示press any key to continue ...
	system("pause");
	return 0;
}

代码下载及其运行

代码下载地址:http://download.csdn.net/detail/yincheng01/6704519

解压密码:c.itcast.cn


下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:

1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”


2)在下拉框中选择相应项目,项目名和博客编号一致

3)点击“本地Windows调试器”运行


程序运行结果









posted on 2013-12-12 19:17  牛栏山1  阅读(101)  评论(0编辑  收藏  举报

导航