[1] Enter a two-dollar lookup tree to convert the two-dollar lookup tree into a sorted doubly linked list

Source: Internet
Author: User

Enter a two-dollar lookup tree to convert the two-dollar lookup tree into a sorted doubly linked list.
Requires that no new nodes be created, only the pointer is adjusted.
10
/     \
6 14
/ \     /  \
4 8 12 16
Convert to doubly linked list
4=6=8=10=12=14=16

Solution:

Binary lookup tree: First it is a two-dollar tree, on this basis it is either an empty tree or a two-dollar tree with the following properties:

(1) The Joz tree is not empty, then the value of all nodes on the left subtree is less than the value of its root node;

(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;

(3) The left and right sub-trees are also two yuan to find the tree

/** * 1: Constructs a two-fork lookup tree; * 2: The middle sequence traverses the binary lookup tree, so the nodes are accessed from small to large sequentially, * assuming that the previously visited nodes have been adjusted to a doubly linked list, then * simply connect the current node to the last node of the doubly linked list, and then the bidirectional list adjusts Finished **/#include<stdio.h>//Ten//    /   //6//  / \  /  //4 8structbstreenode{intM_nvalue; Bstreenode*M_pleft; Bstreenode*m_pright;};  typedef bstreenode Doublelist; Doublelist*Phead; Doublelist*Plistindex;Static inti;/*set up a two-fork sorting tree*/voidAddbstreenode (Bstreenode *&pcurrent,intvalue) {    if(Pcurrent = =NULL) {Bstreenode*pbstree =NewBstreenode (); Pbstree->m_nvalue =value; Pbstree->m_pleft =NULL; Pbstree->m_pright =NULL; Pcurrent=Pbstree; }    Else if(Pcurrent->m_nvalue <value) {Addbstreenode (pcurrent-m_pright, value); }    Else if(Pcurrent->m_nvalue >value) {Addbstreenode (pcurrent-m_pleft, value); }    Else{printf ("Node repeated.\n"); }}voidMid_walk_tree (Bstreenode *proot) {    if(Proot! =NULL) {        if(NULL! = proot->m_pleft) {Mid_walk_tree (Proot-m_pleft); } printf ("%d", proot->m_nvalue); if(NULL! = proot->m_pright) {Mid_walk_tree (Proot-m_pright); }    }}voidConverttodoublelist (Bstreenode *pcurrent) {pcurrent->m_pleft =Plistindex; if(NULL! =Plistindex) {Plistindex->m_pright =pcurrent; }    Else{Phead=pcurrent; } Plistindex=pcurrent; printf ("Pcurrent->m_nvalue =%d\n", pcurrent->m_nvalue);}/*the middle sequence traverses the binary tree while adjusting the node pointer .*/voidInorderbstree (bstreenode*Pbstree) {    if(NULL = =Pbstree) {        return; }        if(NULL! = pbstree->m_pleft) {Inorderbstree (Pbstree-m_pleft); } printf ("i =%d\n", i);        Converttodoublelist (Pbstree); if(NULL! = pbstree->m_pright) {Inorderbstree (Pbstree-m_pright); } I++;}intMain () {Bstreenode*proot =NULL; Addbstreenode (Proot,Ten); Addbstreenode (Proot,6); Addbstreenode (Proot, -); Addbstreenode (Proot,4); Addbstreenode (Proot,8); Addbstreenode (Proot, A); Addbstreenode (Proot, -); //Mid_walk_tree (proot);Inorderbstree (Proot); return 0;}

[1] Enter a two-dollar lookup tree to convert the two-dollar lookup tree into a sorted doubly linked list

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.