Code finished the first compile execution actually succeeded ... Happy ~
description of the problem:
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, just the pointer is adjusted. Like what:
10
/ \
6 14
/ \ / \
4 8 12 16
Convert to doubly linked list
4=6=8=10=12=14=16
algorithm:
Assuming there are no "no new nodes to create" limit, just one time to do a mid-order traversal, the data value of each node to construct a new node.
Because of the constraints, now we can only use the existing nodes, adjust their pointer point, the search tree into a doubly linked list. After the algorithm is complete, the original search tree does not exist.
Similar to the middle sequence traversal, we use recursion: to convert the left and right subtree of a node t into a chain link to the left and the opposite of T.
It should be noted that the value of thelinked list of the left and right sub-tree of T is different , T->left should return the tail node of the list (maximum), T->right should return the head node of the list (the smallest), This requires that we set up a singular flag to distinguish whether the node currently being processed is a left child or a right child.
Code implementation:
#include <iostream>using namespace std;//node structure struct bstreenode{int data; Bstreenode* left; bstreenode* right;};/ The return pointer of the left and right subtree of the parent node is one end, distinguished by flag bstreenode* Transform (bstreenode* t,int flag) {if (t->left)//Turn left subtree {t->left = Transform (t->left, 0); t->left->right = t;} if (t->right)//Turn right subtree {t->right = Transform (t->right, 1); t->right->left = t;} if (flag = = 0)//This is the left subtree of the parent node, returning the right node {while (t->right) t = T->right;return t;} if (flag = = 1)//This is the right subtree of the parent node, returning the leftmost node {while (t->left) t = T->left;return t;}} void Main () {bstreenode* N4 = new Bstreenode; bstreenode* N6 = new Bstreenode; bstreenode* n8 = new Bstreenode; bstreenode* N10 = new Bstreenode; bstreenode* n12 = new Bstreenode; bstreenode* n14 = new Bstreenode; bstreenode* N16 = new bstreenode;//Construction two fork tree N4->data = 4;n4->left = N4->right = NULL; N8->data = 8;n8->left = N8->right = NULL; N12->data = 12;n12->left = N12->right = Null;n16->data = 16;n16->left = N16->right = NULL; N6->data = 6;n6->left = N4;n6->right = N8;n14->data = 14;n14->left = N12;n14->right = N16;n10->data = 10;n10->left = N6;n 10->right = N14; bstreenode* Head=transform (n10,1);//returns to the left node while (head->right)//Right output test {cout << head->data << '; head = Head->right;} cout << head->data;//Right-most node datacout << endl;while (head->left)//left output inspection {cout << head->data << "; head = Head->left;} cout << head->data;//leftmost datacout << endl;system ("pause");}
Output:
4 6 8 10 12 14 16
16 14 12 10 8 6 4
Binary lookup tree is transformed into a sorted doubly linked list--requires no new nodes to be created