【微软100面试题实现】第01题:把二元查找树转变成排序的双向链表

微软100面试题实现:第01题

题源自:http://topic.csdn.net/u/20101126/10/b4f12a00-6280-492f-b785-cb6835a63dc9.html


1.把二元查找树转变成排序的双向链表
题目:
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
要求不能创建任何新的结点,只调整指针的指向。

10
/ \
6 14
/ \ / \
4 8 12 16

转换成双向链表
4=6=8=10=12=14=16。

首先我们定义的二元查找树 节点的数据结构如下:
struct BSTreeNode
{
int m_nValue; // value of node
BSTreeNode *m_pLeft; // left child of node
BSTreeNode *m_pRight; // right child of node
};


参考答案并实现代码如下

#include "stdio.h"
#include "malloc.h"
#define LENGTH 7

//二叉查找树节点
struct BSTreeNode
{
	int m_nValue;			// value of node
	BSTreeNode *m_pLeft;	// left child of node
	BSTreeNode *m_pRight;	// right child of node
};

typedef BSTreeNode* DoubleList;  //双链表指针
DoubleList dHead;				 //头指针
DoubleList dTail;				 //尾指针

// 初始化二元查找树
void addBSTree(BSTreeNode *&pCurrent, int value) //参数为头节点和要添加的节点value
{
	 //为每一个value建立一个节点
	BSTreeNode * pBSTree =(BSTreeNode*)malloc((int)sizeof(BSTreeNode)); 
	pBSTree->m_nValue = value;
	pBSTree->m_pLeft = NULL;
	pBSTree->m_pRight = NULL;

	if (NULL == pCurrent){      //如果头节点为空,新建一个节点,并将其赋值给头节点
		pCurrent = pBSTree;
		return;
	} 
	if(pCurrent->m_nValue != value){		
		if ((pCurrent->m_nValue) > value ){           // 如果节点的左节点值小于value
			if(pCurrent->m_pLeft == NULL){
				pCurrent->m_pLeft = pBSTree;
			} else {
				addBSTree(pCurrent->m_pLeft,value);
			}
		} else if ((pCurrent->m_nValue) < value){    // 如果节点的左节点值大于value 
			if(pCurrent->m_pRight == NULL){
				pCurrent->m_pRight = pBSTree;
			} else {
				addBSTree(pCurrent->m_pRight,value);
			}
		} 
	} else { 
		printf("提示:%d 重复加入\n",value);		 //重复加入节点
		free(pBSTree);
	}

}

// 二叉树转换成list
void convertToDoubleList(BSTreeNode * pCurrent)
{

	pCurrent->m_pLeft = dTail;
	if (NULL != dTail){
		dTail->m_pRight = pCurrent;
	} else {
		dHead = pCurrent;
	} 
	dTail = pCurrent;
    printf("%4d",pCurrent->m_nValue);
}


// 遍历二元查找树 中序
void ergodicBSTree(BSTreeNode * pCurrent)
{
	if (NULL == pCurrent){					//节点为空,则返回
		return;
	}
	if (NULL != pCurrent->m_pLeft){			//左子树不为空
		ergodicBSTree(pCurrent->m_pLeft); 
	}

	convertToDoubleList(pCurrent);			// 节点接到链表尾部

	if (NULL != pCurrent->m_pRight){		//右子树不为空
		ergodicBSTree(pCurrent->m_pRight);
	}
}

int main()
{
	int i;
	int array[LENGTH] = {10,4,6,4,12,14,16}; //定义的数组
	printf("原始数组输出:\n");
	for(i = 0; i<LENGTH; ++i){
		printf("%4d",array[i]);
	}
	printf("\n");

	BSTreeNode *root = NULL;               //初始化
	dHead = NULL;
	dTail = NULL;
	for(i = 0; i<LENGTH; ++i){		       //建立一个BST树		
		addBSTree(root,array[i]);
	}

	printf("中序遍历结果:\n");
	ergodicBSTree(root);
	printf("\n");

	printf("输出双链表:\n");
	DoubleList temp = dHead;
	while(temp){
		printf("%4d",temp->m_nValue);
		temp = temp->m_pRight;
	}
	printf("\n");
	
	return 0;
}
运行结果:


总结:

void addBSTree(BSTreeNode *&pCurrent, int value)

这一行中的参数传递是引用传参,刚开始只是地址传参,出现没有输出的情况,后来恶补了一下C语言中的3中传参方式才弄明白了
为什么了。引用和传值真的难用,O(∩_∩)O~





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值