Binary lookup tree transforms 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.


One of the most intuitive ideas is to find the smallest number in the binary search tree and add it to the linked list.


</pre><pre name= "code" class= "CPP" >//BST2list.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> using namespace std; #define INFINITY 1000000struct binode{int ele; binode* Lnode; Binode* Rnode;}; int min; Binode*head, *tail; Binode*minnode; Binode*p; Binode*create_tree () {Binode * root = new Binode; Binode*node1 = new Binode; Binode*node2 = new Binode; Binode*node3 = new Binode; Binode*node4 = new Binode; BINODE*NODE5 = new Binode; Binode*node6 = new Binode; Binode*node7 = new Binode; Binode*node8 = new Binode; Binode*node9 = new Binode; Binode*node10 = new Binode; Binode*node11 = new Binode;root->ele = 45;node1->ele = 38;node2->ele = 55;node3->ele = 33;node4->ele = 43;n Ode5->ele = 19;node6->ele = 16;node7->ele = 52;node8->ele = 58;node9->ele = 50;node10->ele = 41;node11-& Gt;ele = 35;root->lnode = Node1;root->rnode = Node2;node1->lnode = Node3;node1->rnode = node4;node2-> Lnode = Node7;node2->rnode = Node8;node3->lnode = noDe5;node3->rnode = Node11;node4->lnode = Node10;node4->rnode = Null;node5->lnode = Node6;node5->rnode = Null;node6->lnode = Null;node6->rnode = Null;node7->lnode = Node9;node7->rnode = NULL;node8->lnode = NULL ; node8->rnode = Null;node9->lnode = Null;node9->rnode = Null;node10->lnode = NULL;node10->rnode = NULL;  Node11->lnode = Null;node11->rnode = Null;//binode*node12 = new Binode;  Node12->ele = 12;  Node12->lnode = NULL;  Node12->rnode = NULL;  Node6->lnode = Node11; return root;} Binode*find_min (Binode*node) {//minnode=node; this sentence causes a recursive error if (node = = NULL) return null;if (Node->ele < min) {min = node->ele;minnode = node;} if (node->lnode! = NULL) {if (Node->lnode->ele < min) {min = Node->lnode->ele;minnode = Node->lnode;} Find_min (Node->lnode);} if (node->rnode! = NULL) {if (Node->rnode->ele < min) {min = Node->rnode->ele;minnode = Node->rnode;} Find_min (Node->rnode);} Return Minnode;}  void Findparent (Binode*node, binode*parent) {if (parent = = NULL) return;if (Parent->lnode = = Node | | parent->rnode = = node) p = parent;findparent (node, parent->lnode); findparent (node, parent->rnode);} Binode*bst2list (binode*root) {min = Infinity;minnode = NULL; Binode*n = Find_min (root); while (n) {if (n = = root) {if (N->rnode! = NULL) root = N->rnode;else{tail->rnode = N;N-&G T;lnode = Tail;tail = N;return head;}} ElseIf (N->rnode! = null) {p = Null;findparent (n, root); if (P! = null) P->lnode = N->rnode;n->rnode = null;} Else{p = Null;findparent (n, root); if (P! = null) P->lnode = NULL;} if (head = = Null&&tail = = NULL) {head = tail = n;} else if (head = = tail) {Tail->rnode = N;n->lnode = Tail;tail = N;head->rnode = tail;} Else{tail->rnode = N;n->lnode = Tail;tail = n;} Min = Infinity;minnode = Null;n = Find_min (root);}} int _tmain (int argc, _tchar* argv[]) {head = tail = NULL; Binode*root = Create_tree (); head = bst2list (root); system ("pause");return 0;} 

Find_min recursive Minnode The position of the data is incorrect, resulting in the start of recursive failure. So pay special attention to the processing of variables that affect the recursive process.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Binary lookup tree transforms 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.