自己已经决定向程序员方向发展了,于是开始学习计算机专业的一些基础知识。在牛人博客中发现了大作,http://blog.youkuaiyun.com/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();
}
如果有什么问题,欢迎各位交流。