This is a question we can see from a friend. The question is as follows: enter a binary search tree and convert it into a sorted two-way linked list.
You must not create any new node. You only need to adjust the pointer point.
10
/\
6 14
/\/\
4 8 12 16
Convert to a two-way linked list
4 = 6 = 8 = 10 = 12 = 14 = 16.
First, we define the data structure of the Binary Search Tree node as follows:
Struct bstreenode
{
Int m_nvalue; // value of Node
Bstreenode * m_pleft; // left child of Node
Bstreenode * m_pright; // right child of Node
};
To see this question, a natural idea is recursion. The left and right Subtrees of a tree are respectively recursive to generate two sorted two-way linked lists, insert the root and right subtree into the left subtree. The Code is as follows:
# Include "BSTree2lb. h "# include <stdio. h> struct BSTreeNode * Sort (struct BSTreeNode * tree) {if (tree = NULL) return NULL; struct BSTreeNode * left = Sort (tree-> m_pLeft ); // left-sorted struct BSTreeNode * right = Sort (tree-> m_pRight); // right-sorted tree-> m_pLeft = NULL; tree-> m_pRight = NULL; left = Insert (left, tree); // Insert the root to the left linked list left = Insert (left, right); // Insert the right linked list to the left linked list return left ;}
// Insert beinserted into insert: struct bstreenode * insert (struct bstreenode * Insert, struct bstreenode * beinserted) {If (beinserted = NULL) return insert; if (insert = NULL) return beinserted; struct bstreenode * temp = NULL; struct bstreenode * head = NULL; // The merged header struct bstreenode * IP = NULL; // If the IP address is the first item of insert, if it is null, insert the first item if (insert-> m_nvalue <beinserted-> m_nvalue) Head = insert; else head = beinserted; head-> m_pleft = NULL; whi Le (1) {If (beinserted = NULL) // exit break if no entry is inserted; If (IP! = NULL & Insert = NULL) // If the IP address points to the last {IP-> m_pright = beinserted; beinserted-> m_pleft = IP; break ;} if (beinserted-> m_nvalue <= insert-> m_nvalue) // If the insert condition is {If (IP = NULL) // If inser is the first entry {IP = beinserted; temp = beinserted-> m_pright; beinserted-> m_pright = insert; beinserted-> m_pleft = NULL; insert-> m_pleft = beinserted; beinserted = temp ;} else {temp = beinserted-> m_pright; // beinserted insert IP address, insert between IP addresses-> m_pright = beinserted; B Einserted-> m_pleft = IP; beinserted-> m_pright = insert; insert-> m_pleft = beinserted; IP = beinserted; // beinserted = temp in the next cycle ;}} else {IP = insert; insert = insert-> m_pright;} return head;} void test_bst () {struct bstreenode data [7] = {10, Data + 1, data + 2}, {6, Data + 3, null}, {14, Data + 5, null}, {4, null, Data + 4}, {8, null, data + 6 },{ 12, null, null },{ 16, null, null }}; struct bstreenode * r = sort (data); While (R-> m_pright! = NULL) {printf ("% d =", R-> m_nvalue); r = r-> m_pright;} printf ("% d \ n ", r-> m_nvalue );}
As shown above, Sort () is the sorting function, Insert () is to Insert a linked list into another linked list, And test_bst () is the test function. No problem with the program so far. Please point out the bug.