Binary search tree (search binary tree) converted into a doubly linked list

Source: Internet
Author: User

1. Title Description:

A binary search tree is transformed into a doubly linked list;

2. Two fork search tree, directly look at the picture:


is a binary search tree model, that is, the transformation function of the entry data, is also the following function is about to use the example, since there is input, there must be output, the first face in the view of a picture (third):

3. Input and Output model:


The right side is the final output, 5 behind is empty, below to analyze:

1. In the binary search tree, each node has two children, namely left and right, and in the doubly linked list, each node also has two pointers, precursor and successor pointers, binary tree and doubly linked list are very similar;

2. Change the binary search tree to the left and right child pointer, you can complete the two-fork search tree to the two-way linked list conversion;

3. As the result of the traversal of the final doubly linked list is the traversal result of the binary search tree sequence;

4. Start the sequence of clues to the binary tree (that is, change the binary tree pointer pointing)

If you have questions about the middle-order threaded binary tree, see:


The left and right child pointer of the leaf node is changed by the way of the middle sequence clue;

4. Look at the code to speak:

The first part:

First, you need to save the head of the last doubly linked list, the leftmost node of the binary tree:

Node* _binarytodoublelist (node* root) {//1. Find the head of a doubly linked list; node* head = Root;while (head->_left! = nullptr) {head = Head->_ Left;} node* prev = Nullptr;_change (root,prev);    conversion function return head;}
Part II:

Conversion function: A recursive process, according to the sequence of clues to go

void _change (node* cur,node*& prev) {if (cur = = nullptr) return;//1. Find the leftmost _change (Cur->_left,prev); cur->_left = prev;  At this point prev is empty if (prev! = nullptr) Prev->_right = Cur;prev = Cur;_change (Cur->_right, prev);}
Complete test Code:

#pragma oncetemplate<class K, class v>struct sbtnode{k key; V value; Sbtnode<k, v> *_left; Sbtnode<k, v> *_right; Sbtnode (const k& key, const v& value): Key (Key), value (value), _left (nullptr), _right (nullptr) {}};template< Class K, class V>class sbtree{typedef sbtnode<k, v> node;public:sbtree (): _root (nullptr) {}~sbtree () {}public:/ /non-recursive inserts bool Insert (const k& key, const v& value) {return _insert (key, value);} Recursively inserts bool Insert_r (const k& key, const v& value);//non-recursive lookup node sbtnode<k, v>* find (const k& key) {if (_root = = nullptr) {return nullptr;} Sbtnode<k, v> *cur = _root;while (Cur->_left | | cur->_right) {if (Cur->key = = key) {return cur;} else if (Cur->key > key) {cur = cur->_left;} else if (Cur->key < key) {cur = cur->_right;} Else{return nullptr;}}} BOOL _insert (const k& key, const v& value) {if (_root = = nullptr) {_root = new sbtnode<k, v> (key, value); retur n true;} Sbtnode<k, v> *parent = nullptr; Precursor sbtnode<k to cur, v> *cur = _root;while (cur) {if (Cur->key > key)//Insert Left {parent = Cur;cur = Cur->_left ;} else if (Cur->key < key) {parent = Cur;cur = Cur->_right;} Else{return false;}} if (Parent->key < key) {sbtnode<k, v> *node = new sbtnode<k, v> (key, value);p arent->_right = Node;retu RN true;} else if (Parent->key > key) {sbtnode<k, v> *node = new sbtnode<k, v> (key, value);p arent->_left = node; return true;} Else{return false;}} node* binarytodoublelist () {return _binarytodoublelist (_root);} Node* _binarytodoublelist (node* root) {//1. Find the head of a doubly linked list; node* head = Root;while (head->_left! = nullptr) {head = Head->_ Left;}    node* prev = Nullptr;_change (Root,prev); conversion function return head;} void _change (node* cur,node*& prev) {if (cur = = nullptr) return;//1. Find the leftmost _change (Cur->_left,prev); cur->_left  = prev; At this point prev is empty if (prev! = nullptr) Prev->_right = Cur;prev = Cur;_change (Cur->_right, prev);} Middle sequence traversal void inorder (sbtnode<k, V>* root) {if (root = nullptr) {return;//recursive end exit}sbtnode<k, v> *cur = Root;inorder (cur->_left); cout << cu R->key << ""; Inorder (cur->_right);} Sequential traversal of the doubly linked list void Treavelist () {node* cur = binarytodoublelist (); while (cur) {cout << cur->key<< ""; cur = cur- >_right;} cout << Endl;} Public:sbtnode<k, v> *_root;};
Drawing is not easy, help the top, enlighten!
























Binary search tree (search binary tree) converted into a 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.