Академический Документы
Профессиональный Документы
Культура Документы
p=1139
Write a function which will accept a Binary Search Tree and convert it to a sorted Doubly linked List. For Example: If we have a
Binary Search Tree as shown below
Then the corresponding Doubly Linked List will be the Inorder traversal of the above tree
The Node of a Tree has a data and two pointers which points to the left and right subtrees
The Node of a Doubly Linked List has a data and two pointers which point to the previous and next node
If you notice the structure of above two nodes is exactly the same (except for the nomenclature of pointers, which is immaterial). The
Structure is as below:
1 struct Node{
2 int data;
3 struct Node *left;
4 struct Node *right;
5 }
I have named the two pointers as left and right, but they can be named pre/next or anything.
If we are codding in C++, then we can take the advantage of Object Oriented Programming and define the Node as a Class, The
benefit we will get is that we can also define the constructor and/or other operations, like below, I have also defined a constructor
which initializes the pointers to NULL in the member initialization list of constructor:
1 of 5 12/4/2010 7:15 PM
Converting a Tree to a Doubly Linked List « RawKam (beta) http://www.rawkam.com/?p=1139
08 int data;
09 Node* left;
10 Node* right;
11
12 // Constructor
13 Node(int v):data(v), left(NULL), right(NULL){; }
14 };
Solution:
Since the structure of Nodes is same in both the cases, the conversion of BST (Binary Search Tree) to Doubly Linked List will
only require rearrangement of pointers. We use recursive algorithm to convert Tree to Doubly linked list
Algorithm:
Code:
The above function uses function append which will be as shown below:
2 of 5 12/4/2010 7:15 PM
Converting a Tree to a Doubly Linked List « RawKam (beta) http://www.rawkam.com/?p=1139
References: http://cslibrary.stanford.edu/109/TreeListRecursion.html
—————————————-
For a change let me writing the entire program for your Convenience below :
3 of 5 12/4/2010 7:15 PM
Converting a Tree to a Doubly Linked List « RawKam (beta) http://www.rawkam.com/?p=1139
064 */
065 Node* append(Node *a, Node *b) {
066
067 // If any of the two lists is null return the 2nd list.
068 if (a == NULL) return b;
069 if (b == NULL) return a;
070
071 Node* head = a;
072
073 while(a‐>right != NULL){ a = a‐>right; }
074
075 a‐>right = b;
076 b‐>left = a;
077
078 return(head);
079 }
080
081 /** Recursively convert a Binary Tree to a Doubly Linked List
082 */
083 Node* treeToList(Node* r) {
084
085 // Terminating Condition of Recursion
086 if (r == NULL){ return NULL; }
087
088 // Convert to list the left and right subtrees recursively
089 Node *prev = treeToList(r‐>left);
090 Node *next = treeToList(r‐>right);
091
092 // Making the root Node a separate list
093 r‐>left = NULL;
094 r‐>right = NULL;
095
096 /* Append everything together in sorted order */
097 prev = append(prev, r);
098 prev = append(prev, next);
099
100 return(prev);
101 }
102
103 int main()
104 {
105 Node* root = createSampleTree();
106 cout<<"In order Tracersal :";
107 printInorder(root);
108
109 root = treeToList(root);
110 cout<<"\nList :";
111 printDoublyList(root);
112
113 getchar();
114 return 0;
115 }
Output:
4 of 5 12/4/2010 7:15 PM
Converting a Tree to a Doubly Linked List « RawKam (beta) http://www.rawkam.com/?p=1139
5 of 5 12/4/2010 7:15 PM