您的位置:首页 > 理论基础 > 数据结构算法

1.把二元查找树转变成排序的双向链表

2012-11-20 21:18 375 查看
声明:此题目来自 v_JULY_v的blog,出处http://blog.csdn.net/v_july_v/article/details/6004660

代码经过本人调试,不敢保证是最优的,但应该是正确的,如果错误,希望指正,改进也可以。谢谢!!!

题目:

输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。

要求不能创建任何新的结点,只调整指针的指向。

  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

};

答案

//20121120
//中序遍历解决问题
#include <iostream>
#include <queue>

#define LAYERTRAVEL 1//是否输出逐层遍历结果
#define LAYERSTRUCT 1//是否输出层结构,曾结构表示为x_y_z,所有都是从0开始,x表示在第几层,y表示位于第几层的第几个位置上,z表示他的数字是多少,
//如 1_0_23 表示他位于第1层,第0个位置上,他的数字是23,如z=0,那么表示该位置上没有点
//输出层结构的目的在于清晰的看到自己的输入的二叉查找树的结构,从而更明白整个过程
using namespace std;
typedef struct BSTreeNode
{
int m_nValue;//value of node
BSTreeNode *m_pLeft;//left child of node
BSTreeNode *m_pRight;//right child of node
}BSTreeNode,*pBSTreeNode;
void insertNum(BSTreeNode **root,int num);//插入数字
void showStruct(queue<pBSTreeNode> popQueue);//显示曾遍历结果
void showLayerStruct(BSTreeNode *root);//输出层状结构
void turnToDLink(BSTreeNode *root);//转换链表

BSTreeNode *head=NULL;
BSTreeNode *index=NULL;
int main()
{
cout<<"Please type in the number one bye one"<<endl;
cout<<"all the number should be greater than 0"<<endl;
cout<<"If you have finish the input, please type in 0"<<endl;
int num;
BSTreeNode *rt=NULL;
while (1)
{
cin>>num;
if (num==0)
{
break;
}
insertNum(&rt,num);
}
cout<<endl<<"------------------------"<<endl;//
cout<<"layer traveling result"<<endl;
#if LAYERTRAVEL
queue<pBSTreeNode> popQueue;
popQueue.push(rt);
showStruct(popQueue);
#endif
cout<<endl<<"--------------"<<endl;
cout<<"layer struct"<<endl;
#if LAYERSTRUCT
showLayerStruct(rt);
#endif
cout<<endl<<"--------------"<<endl;
cout<<"turn to DoubleLink result"<<endl;
turnToDLink(rt);
while (index!=NULL)
{
cout<<index->m_nValue<<"=";
index=index->m_pRight;
}
cout<<endl;
return 0;
}
void insertNum(BSTreeNode **root,int num)
{
if (*root==NULL)
{
*root=new BSTreeNode;
(*root)->m_nValue=num;
(*root)->m_pLeft=NULL;
(*root)->m_pRight=NULL;
return;
}
if (num>=(*root)->m_nValue)
{
insertNum(&(*root)->m_pRight,num);
}
else
{
insertNum(&(*root)->m_pLeft,num);
}
}
void showStruct(queue<pBSTreeNode> popQueue)
{
//traversing the tree layer by layer
//then cout them
while (1)
{
if (!popQueue.empty())
{
cout<<popQueue.front()->m_nValue<<"  ";
if (popQueue.front()->m_pLeft!=NULL)
{
popQueue.push(popQueue.front()->m_pLeft);
}

if (popQueue.front()->m_pRight!=NULL)
{
popQueue.push(popQueue.front()->m_pRight);
}
popQueue.pop();
}
else
{
break;
}
}
}
void showLayerStruct(BSTreeNode *root)
{
vector<pBSTreeNode> container1,container2;
container1.push_back(root);
int num=1;
int layerNum=0;
int emptyNum=0;
while (1)
{
for (int i=0;i<num;i++)
{
if (container1[i]!=NULL)
{
//eg,0_0_12,is the first layer the first position,the number is 12
cout<<layerNum<<"_"<<i<<"_"<<container1[i]->m_nValue<<"  ";
if (container1[i]->m_pLeft!=NULL)
{
container2.push_back(container1[i]->m_pLeft);
}
else
{
emptyNum++;
container2.push_back(NULL);
}
if (container1[i]->m_pRight!=NULL)
{
container2.push_back(container1[i]->m_pRight);
}
else
{
emptyNum++;
container2.push_back(NULL);
}
}
else
{
cout<<layerNum<<"_"<<i<<"_"<<0<<"  ";
emptyNum+=2;
container2.push_back(NULL);
container2.push_back(NULL);
}
}
if (emptyNum==num*2)
{
emptyNum=0;
cout<<endl;
break;
}
else
{
emptyNum=0;
num=num*2;
layerNum++;
}
cout<<endl;
container1=container2;
container2.clear();
}
}

void convertPoint(BSTreeNode *roo)
{
if (head==NULL)
{
index=roo;
head=roo;
}
else
{
head->m_pRight=roo;
roo->m_pLeft=head;
head=roo;
}
}
void turnToDLink(BSTreeNode *root)
{
//turn to double link list
//中序遍历
if (root==NULL)
{
return;
}
turnToDLink(root->m_pLeft);
convertPoint(root);
turnToDLink(root->m_pRight);
}


参考文章:http://www.cnblogs.com/caidaxia/archive/2011/10/14/2212369.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息