把二元查找树转变成排序的双向链表
用的比较笨的办法,思路是按中序递归得到顺序然后依次修改其指针域。希望得到更好的算法,望知道的赐教

//二叉排序树变为双向链表
#include<iostream>
#include<stack>
using namespace std;
struct treenode{ //树节点定义
int data;
treenode *l;
treenode *r;
};
void create(treenode *&t){ //建树
t->data=10;
t->l=new treenode();
t->l->data=6;
t->l->l=new treenode();
t->l->l->data=4;
t->l->l->l=NULL;
t->l->l->r=NULL;
t->l->r=new treenode();
t->l->r->data=8;
t->l->r->l=NULL;
t->l->r->r=NULL;
t->r=new treenode();
t->r->data=14;
t->r->l=new treenode();
t->r->l->data=12;
t->r->l->l=NULL;
t->r->l->r=NULL;
t->r->r=new treenode();
t->r->r->data=16;
t->r->r->l=NULL;
t->r->r->r=NULL;
}
void printtree(treenode *t){ //测试
if(t){
printtree(t->l);
cout<<t->data<<" ";
printtree(t->r);
}
}
void convert(treenode *&t){ //转换,主要思路为利用堆栈存储节点顺序然后逆序输出
stack<treenode *> s,save;
treenode *p=t;
while(p||!s.empty()){
if(p){s.push(p);p=p->l;}
else{
p=s.top();
s.pop();
save.push(p);
p=p->r;
}
}
treenode *pre,*next;
next=save.top();
save.pop();
next->r=NULL;
while(!save.empty()){
pre=save.top();
save.pop();
pre->r=next;
next->l=pre;
next=pre;
}
next->l=NULL;
t=next;
}
int main(void){
treenode *t=new treenode();
treenode *pre=NULL;
create(t);
printtree(t);
cout<<endl;
convert(t);
cout<<endl;
while(t){
cout<<t->data<<" ";
t=t->r;
}
system("pause");
return 0;
}