Question:
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.
Based on the provided ideas, I wrote a C ++ code that reads an integer from any sequence, creates a binary search tree, and then changes it to a linked list:
#include <iostream>#include <fstream>#include <string>using namespace std;// binary search tree nodestruct BSTreeNode{ int value; struct BSTreeNode *leftChild; struct BSTreeNode *rightChild;};void addBSTreeNode(struct BSTreeNode **ppRoot/*double pointer to the root*/, int new_value);void printBSTree(ofstream *fout, struct BSTreeNode *root, int level);void treeToLinkedList(struct BSTreeNode **ppHead, struct BSTreeNode **ppTail, struct BSTreeNode *root);int main() { ifstream fin ("tree_to_linked_list.in"); ofstream fout ("tree_to_linked_list.out"); int in; struct BSTreeNode *root = NULL, *head = NULL, *tail = NULL, *node = NULL; if(fin.good() == false) { cerr << "file open failed" << endl; return 0; } while(true) { if(fin.eof()) break; fin >> in; addBSTreeNode(&root, in); } printBSTree(&fout, root, 0); fout << endl; treeToLinkedList(&head, &tail, root); for(node = head; node != NULL; node = node->rightChild) { fout << node->value << " "; } fout << endl; for(node = tail; node != NULL; node = node->leftChild) { fout << node->value << " "; } fout << endl; return 0;}void addBSTreeNode(struct BSTreeNode **ppRoot/*double pointer to the root*/, int new_value){ if(*ppRoot == NULL) { *ppRoot = new struct BSTreeNode(); (*ppRoot)->value = new_value; return; } if(new_value == (*ppRoot)->value) { return; } else if(new_value < (*ppRoot)->value) { addBSTreeNode(&(*ppRoot)->leftChild, new_value); } else { addBSTreeNode(&(*ppRoot)->rightChild, new_value); }}void printBSTree(ofstream *pFout, struct BSTreeNode *root, int level){ if(root == NULL) return; printBSTree(pFout, root->leftChild, level + 1); int i; for(i = 0; i < level; i ++) (*pFout) << "\t"; (*pFout) << root->value << endl; printBSTree(pFout, root->rightChild, level + 1);}void treeToLinkedList(struct BSTreeNode **ppHead, struct BSTreeNode **ppTail, struct BSTreeNode *root){ struct BSTreeNode *ltemp, *rtemp; if(root == NULL) { (*ppHead) = NULL; (*ppTail) = NULL; return; } treeToLinkedList(ppHead, <emp, root->leftChild); treeToLinkedList(&rtemp, ppTail, root->rightChild); if(*ppHead == NULL) { (*ppHead) = root; root->leftChild = NULL; } else { root->leftChild = ltemp; ltemp->rightChild = root; } if(*ppTail == NULL) { (*ppTail) = root; root->rightChild = NULL; } else { root->rightChild = rtemp; rtemp->leftChild = root; }}
Sample input:
10 5 14 45 64 3 7 12 11 33 52 63 23 46 73 44 66 22 45 67 78 83
Sample output:
3 5 710 11 12 14 22 23 33 44 45 46 52 63 64 66 67 73 78 833 5 7 10 11 12 14 22 23 33 44 45 46 52 63 64 66 67 73 78 83 83 78 73 67 66 64 63 52 46 45 44 33 23 22 14 12 11 10 7 5 3