Binary search Tree of C + + algorithm converted to doubly linked list

Source: Internet
Author: User

Topic:

Enter a binary search tree and convert the two-fork search tree into a sorted doubly linked list. Requires that no new nodes can be created, only the direction of the node pointers in the tree can be adjusted:

Analysis:

1: Because the chain list is ordered, you can use binary tree in order traversal, because the characteristics of the middle sequence traversal algorithm is small to large access nodes. When traversing access to the root node, assuming that the left side of the root node has been processed, simply connect the root node to the pointer of the last visited node (the largest node in the left subtree). The last node pointer of the current linked list is then updated.

2: recursive processing is possible because the sequence traversal process is exactly the process of converting to a linked list

Code:

BT.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <math.h> #include <queue>using namespace std;// The data structure of the node class Btree{public:int M_nvalue; Btree* M_nleft; btree* m_nright;public:btree (int value) {M_nvalue = value;}};/ /Two insert implementation of the fork Tree void Insert (int value, btree* &root) {if (root = NULL) {root = new BTree (value);} else if (value < Root->m_nvalue) Insert (value,root->m_nleft), else if (value > Root->m_nvalue) Insert ( Value,root->m_nright); else;} The search binary tree is traversed by the middle sequence traversal, and converted to a doubly linked list void Convertnode (btree* proot,btree* &plastnodeinlist) {if (Proot = NULL) return; btree* Pcurrentnode = proot;if (pcurrentnode->m_nleft! = NULL)//First traverse left subtree {convertnode (pcurrentnode->m_nleft, plastnodeinlist);} /* Handles the current node and links that have traversed the linked list node */pcurrentnode->m_nleft = plastnodeinlist;if (plastnodeinlist! = NULL)// If Plastnodeinlist is not already empty, then let his next node point to the current node (right equals next) Plastnodeinlist->m_nright = Pcurrentnode; Plastnodeinlist = pcurrentnode;//then points the plastnodeinlist to the current node IF (pcurrentnode->m_nright! = NULL)//re-traverse right subtree {convertnode (pcurrentnode->m_nright,plastnodeinlist);}}   btree* Convert (btree* proot) {btree* plastnodeinlist = null;//points to the tail node of the doubly linked list Convertnode (proot,plastnodeinlist); After the conversion is complete, plastnodeinlist points to the tail node of the doubly linked list//We need to return to the head node btree* pheadoflist = plastnodeinlist;//The head nodes point to the tail, start traversing while from the tail node ( pheadoflist!= null && Pheadoflist->m_nleft! = null) {pheadoflist = Pheadoflist->m_nleft;} Return pheadoflist;//returns the head node}void printinprev (btree* proot) {if (Proot = NULL) return; Printinprev (proot->m_nleft);cout<<proot->m_nvalue<< ""; Printinprev (proot->m_nright);} void Printlist (btree* &t) {if (t = = null) return;while (t! = null) {cout<<t->m_nvalue<< ""; t = t->m_ Nright;}} int _tmain (int argc, _tchar* argv[]) {btree* m_proot = new BTree (7); insert (3,m_proot); insert (6,m_proot); Insert (1,m_ Proot); insert (2,m_proot); insert (5,m_proot); insert (8,m_proot); insert (7,m_proot); insert (10,m_proot); Printinprev (M_proot); btree* p = Convert (M_proOT);cout<<endl<< "linked list:" <<endl; Printlist (P); GetChar (); return 0;}


Binary search Tree of C + + algorithm converted to doubly linked list

Related Article

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.