数据结构练习(01)把二元查找树转变成排序的双向链表
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; }
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------