数据结构练习(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; }

 

posted @ 2012-12-11 14:23  kedebug  阅读(171)  评论(0编辑  收藏  举报