自己动手“敲”微软100题系列一

  自己已经决定向程序员方向发展了,于是开始学习计算机专业的一些基础知识。在牛人博客中发现了大作,http://blog.csdn.net/v_JULY_v/article/details/6126406

都说要提高编程能力,那么就需要自己多动手。于是下定决心想做完这100道题。

  废话少说,开始第一道题:


并且已经为我们定义好了树节点的结构体。


struct BSTreeNode
{
	int m_nValue; // value of node
	BSTreeNode *m_pLeft; // left child of node
	BSTreeNode *m_pRight; // right child of node
};


接着参考了博客:http://www.cnblogs.com/caidaxia/archive/2011/10/14/2212369.html


自己敲了一遍:


#include <iostream>
#include <iomanip>//不要忘记包含此头文件,当格式化输出的时候

using namespace std;
//二叉树定义
struct BSTreeNode
{
	BSTreeNode *m_pLeft; // left child of node
	BSTreeNode *m_pRight; // right child of node
	int m_nValue;
};

//双向链表定义
typedef BSTreeNode DoubleLinkList;
DoubleLinkList * dHead; //定义链表的头
DoubleLinkList * dLastIndex;


//建立查找二叉树(所有左子树上节点小于根节点,根节点小于所有右子树节点)
void buildBSTree(BSTreeNode * &root,int val)
{
	//如果为空树
	if (NULL == root)
	{
		root =new BSTreeNode; //必须开避空间
		root->m_pLeft= NULL;
		root->m_pRight= NULL;
		root->m_nValue=val;
		return ;
	}else  if (val < root->m_nValue)
	{
		buildBSTree(root->m_pLeft,val);
	}else if (val > root->m_nValue)
	{
		buildBSTree(root->m_pRight,val);
	}else {
		cout << "error: number repeat" << endl;
	}
	
}

//查找二叉树节点加到双向链表中
void addToDoubleLinkList(BSTreeNode * pCurrent)
{
	pCurrent->m_pLeft = dLastIndex;
	if (NULL != dLastIndex)
	{
		dLastIndex->m_pRight = pCurrent;
	}else
	{
		dHead = pCurrent;
	}
	dLastIndex = pCurrent;

}

//中序遍历树     //这里不需要引用传递
void printfBSTree(BSTreeNode* root)
{

	if (NULL == root)
	{
		return;
	}

	printfBSTree(root->m_pLeft);
		
	addToDoubleLinkList(root);
	cout << setfill(' ') << setw(5) << root->m_nValue;

	printfBSTree(root->m_pRight);
	return;	
}

//打印建立的双向链表
void printfDoubleLinkList(DoubleLinkList* pDHead)
{
	if (pDHead != NULL)
	{
		cout << setfill(' ') << setw(5) << pDHead->m_nValue;
		printfDoubleLinkList(pDHead->m_pRight);
	}
}



void main()
{
	//创建树
	BSTreeNode* root=NULL;
	buildBSTree(root,10);
	buildBSTree(root,6);
	buildBSTree(root,14);
	buildBSTree(root,4);
	buildBSTree(root,8);
	buildBSTree(root,12);
	buildBSTree(root,16);
	printfBSTree(root);
	cout << endl;

	printfDoubleLinkList(dHead);

	getchar();
}





结果如下:


然后,自己在参考july博客中下载来的pdf,有另一种方法:

下面是自己敲出来的代码:


/***************************************************************************************
参考:微软面试100题系列by_July.pdf 38页
此方法没加注释,看得比较头疼,后来经过了自己的理解,终于懂了点
建立双向链表,其中head为链表的头,tail为链表的尾主要函数如下:
void connect(BSTreeNode* &head,BSTreeNode* &tail ,BSTreeNode* root)
把树的节点的左右树看做两段链表,左边链表头尾分别是head和lt,右边链表头尾分别是rt,tail
然后把lt与root及root与rt连接起来即可
***************************************************************************************/


#include <iostream>
#include <iomanip>//不要忘记包含此头文件,当格式化输出的时候
using namespace std;
struct BSTreeNode
{
	int m_nValue; // value of node
	BSTreeNode *m_pLeft; // left child of node
	BSTreeNode *m_pRight; // right child of node
};
//双向链表定义
typedef BSTreeNode DoubleLinkList;

//建立双向链表,其中head为链表的头,tail为链表的尾
void connect(BSTreeNode* &head,BSTreeNode* &tail ,BSTreeNode* root)
{
	BSTreeNode *lt, *rt;
	if (NULL == root)
	{
		head=NULL;
		tail=NULL;
		return;
	}
	connect(head,lt,root->m_pLeft);
	connect(rt,tail,root->m_pRight);

	if (lt!=NULL)
	{
		
		lt->m_pRight=root;
		root->m_pLeft=lt;
	}else{
		head=root;
	}

	if (rt != NULL)
	{		
		rt->m_pLeft=root;
		root->m_pRight=rt;
		
	}else{
		tail=root;
	}
}

//建立查找二叉树(所有左子树上节点小于根节点,根节点小于所有右子树节点)
	void buildBSTree(BSTreeNode * &root,int val)
{
	//如果为空树
	if (NULL == root)
	{
		root =new BSTreeNode; //必须开避空间
		root->m_pLeft= NULL;
		root->m_pRight= NULL;
		root->m_nValue=val;
		return ;
	}else  if (val < root->m_nValue)
	{
		buildBSTree(root->m_pLeft,val);
	}else if (val > root->m_nValue)
	{
		buildBSTree(root->m_pRight,val);
	}else {
		cout << "error: number repeat" << endl;
	}

}

	//中序遍历树  打印二叉树节点
	void printfBSTree(BSTreeNode* root)
	{

		if (NULL == root)
		{
			return;
		}

		printfBSTree(root->m_pLeft);

		cout << setfill(' ') << setw(5) << root->m_nValue;

		printfBSTree(root->m_pRight);
		return;	
	}

//打印建立的双向链表
void printfDoubleLinkList(DoubleLinkList* pDHead)
{
	if (pDHead != NULL)
	{
		cout << setfill(' ') << setw(5) << pDHead->m_nValue;
		printfDoubleLinkList(pDHead->m_pRight);
	}
}
void main()
{
	//创建树
	BSTreeNode* pRoot=NULL;
	BSTreeNode* pHead=NULL;
	BSTreeNode* pTail=NULL;

	buildBSTree(pRoot,10);
	buildBSTree(pRoot,6);
	buildBSTree(pRoot,14);
	buildBSTree(pRoot,4);
	buildBSTree(pRoot,8);
	buildBSTree(pRoot,12);
	buildBSTree(pRoot,16);
	printfBSTree(pRoot);
	cout << endl;

	connect(pHead,pTail,pRoot);
	
	printfDoubleLinkList(pHead);

	getchar();

}


如果有什么问题,欢迎各位交流。








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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值