Convert BST to an ordered doubly linked list!

Source: Internet
Author: User

In a binary tree, each node has two pointers to the child nodes. In a doubly linked list, each node also has two pointers, which point to the previous node and the latter node, respectively. Because of the similarity of the two structures, the binary search tree is also a sort of data structure, so it is theoretically possible to realize the conversion between the two-fork search tree and the ordered bidirectional linked list.


The following file Bst_to_dl.cpp converts BST to a sorted doubly linked list, take the code:


#include "binary_tree.h" void Convertnode (binarytreenode* pnode, binarytreenode** plastnodeinlist) {if (PNode = = NULL)

    Return

    binarytreenode* pcurrent = Pnode;

    The left subtree is processed first if (pcurrent->pleft) Convertnode (Pnode->pleft, plastnodeinlist);
    Completion of Pcurrent and plastnodeinlist splicing and replacement pcurrent->pleft = *plastnodeinlist;
    if (*plastnodeinlist! = NULL) (*plastnodeinlist)->pright = pcurrent;

    *plastnodeinlist = pcurrent;
Traverse to right subtree if (pcurrent->pright) Convertnode (pcurrent->pright,plastnodeinlist);
    } binarytreenode* Convert (binarytreenode* proot) {binarytreenode* plastnodeinlist = NULL;

    Convertnode (Proot, &plastnodeinlist);
    Plastnodeinlist points to the tail node of the doubly linked list, we return its head node in reverse order binarytreenode* pheadoflist = plastnodeinlist;

    while (pheadoflist = null && pheadoflist->pleft! = null) Pheadoflist = pheadoflist->pleft;
return pheadoflist; }//======================== Test Code ============================= void Printdoublelinkedlist (binarytreenode* pheadoflist) {binarytreenode* pNode = pHeadOfList;
    printf ("The nodes from left to right are:\n");

        while (pnode! = NULL) {printf ("%d\t", Pnode->value);
        if (pnode->pright = = NULL) break;
    Pnode = pnode->pright;
    } printf ("\nthe nodes from the right to the left are:\n");

        while (pnode! = NULL) {printf ("%d\t", Pnode->value);
        if (Pnode->pleft = = NULL) break;
    Pnode = pnode->pleft;
} printf ("\ n");
    } void Destroylist (binarytreenode* pheadoflist) {binarytreenode* pnode = pheadoflist;
        while (pnode! = NULL) {binarytreenode* Pnext = pnode->pright;
        Delete Pnode;
    Pnode = Pnext; }} void Test (const char* testname, binarytreenode* proot) {if (testname! = NULL) printf ("%s begins: \ n", tes

    Tname);

    Printtree (Proot);
    binarytreenode* pheadoflist = Convert (proot); PrintdoublelInkedlist (pheadoflist);                         }//10///\//6 14/// /\/\//4 8 void Test1 () {binarytreenode* PNode10 = Createbinarytreenode
    (10);
    binarytreenode* PNode6 = Createbinarytreenode (6);
    binarytreenode* pNode14 = Createbinarytreenode (14);
    binarytreenode* pNode4 = Createbinarytreenode (4);
    binarytreenode* PNode8 = Createbinarytreenode (8);
    binarytreenode* pNode12 = Createbinarytreenode (12);

    binarytreenode* PNode16 = Createbinarytreenode (16);
    Connecttreenodes (PNode10, PNode6, PNODE14);
    Connecttreenodes (PNode6, PNode4, PNode8);

    Connecttreenodes (PNode14, PNode12, PNODE16);

    Test ("Test1", PNode10);
Destroylist (PNODE4);                   }//5/////4/////    3/////2/////           1 void Test2 () {binarytreenode* PNODE5 = Createbinarytreenode (5);
     binarytreenode* pNode4 = Createbinarytreenode (4);
     binarytreenode* pNode3 = Createbinarytreenode (3);
     binarytreenode* PNode2 = Createbinarytreenode (2);

     binarytreenode* pNode1 = Createbinarytreenode (1);
     Connecttreenodes (PNODE5, PNode4, NULL);
     Connecttreenodes (PNode4, PNode3, NULL);
     Connecttreenodes (PNode3, PNode2, NULL);

     Connecttreenodes (PNode2, PNode1, NULL);

     Test ("Test2", PNODE5);
Destroylist (PNODE1); }//1//\//2//\//3//\//4//\//5 void Test3 () {binarytreenode* Pnod
    e1 = Createbinarytreenode (1);
    binarytreenode* PNode2 = Createbinarytreenode (2);
    binarytreenode* pNode3 = Createbinarytreenode (3);
    binarytreenode* pNode4 = Createbinarytreenode (4);

    binarytreenode* PNODE5 = Createbinarytreenode (5);
    Connecttreenodes (PNODE1,NULL,PNODE2);
    Connecttreenodes (PNODE2,NULL,PNODE3); ConnEcttreenodes (PNODE3,NULL,PNODE4);

    Connecttreenodes (PNODE4,NULL,PNODE5);

    Test ("Test3", pNode1);
Destroylist (PNODE1);
    }//The tree has only 1 nodes void Test4 () {binarytreenode* pNode1 = Createbinarytreenode (1);

    Test ("Test4", pNode1);
Destroylist (PNODE1);

}//tree does not have node void Test5 () {Test ("Test5", NULL);}
    int main (int argc, char** argv) {Test1 ();
    Test2 ();
    Test3 ();
    Test4 ();

    Test5 ();
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.