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

数据结构练习(01)把二元查找树转变成排序的双向链表

2012-12-11 14:23 337 查看
http://zhedahht.blog.163.com/blog/static/254111742007127104759245/

struct tree {
int value;
tree *left, *right;
};

void convert(tree *&head, tree *&tail, tree *root)
{
if (!root)  {
head = tail = NULL;
return ;
}

tree *templeft, *tempright;

templeft = tempright = NULL;

convert(head, templeft, root->left);
convert(tempright, tail, root->right);

if (templeft) {
templeft->right = root;
root->left = templeft;
}
else {
head = root;
}

if (tempright) {
tempright->left = root;
root->right = tempright;
}
else {
tail = root;
}
}

tree* tree2link(tree *root)
{
tree *head, *tail;
convert(head, tail, root);
   return head;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐