-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgreat-tree-list-problem.cpp
104 lines (90 loc) · 2.19 KB
/
great-tree-list-problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// http://www.geeksforgeeks.org/the-great-tree-list-recursion-problem/
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *left, *right;
Node(int data) {
this->data = data;
left = right = NULL;
}
};
Node* treeToList(Node *root);
Node* joinList(Node *listA, Node *listB); // joins two circular linked lists
void joinNode(Node *a, Node *b); // joins a to b
void printCircularList(Node *head); // prints circular linked list
Node* treeInsert(Node* root, int data); // inserts elements in BST
Node* treeToList(Node *root) {
if (root == NULL)
return root;
Node *leftList = treeToList(root->left); // convert left sub-tree to list
Node *rightList = treeToList(root->right); // convert right sub-tree to list
// make root an independent list of single element
root->left = root;
root->right = root;
// join leftList & root and then the resultant list & rightList
leftList = joinList(leftList, root);
leftList = joinList(leftList, rightList);
return leftList;
}
Node* joinList(Node *listA, Node *listB) {
if (listA == NULL)
return listB;
if (listB == NULL)
return listA;
Node *aLast, *bLast;
aLast = listA->left;
bLast = listB->left;
joinNode(aLast, listB);
joinNode(bLast, listA);
// still have to figure out why this doesn't work
// joinNode(listA->left, listB);
// joinNode(listB->left, listA);
return listA;
}
void joinNode(Node *a, Node *b) {
a->right = b;
b->left = a;
}
void printCircularList(Node *head) {
if (head == NULL)
return;
Node *start = head;
while (true) {
cout<<head->data<<" ";
head = head->right;
if (head == start)
return;
}
}
void inorder(Node *root) {
if (root == NULL)
return;
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
Node* treeInsert(Node* root, int data) {
if (root == NULL)
return new Node(data);
else {
if (data <= root->data)
root->left = treeInsert(root->left, data);
else
root->right = treeInsert(root->right, data);
}
return root;
}
int main() {
Node *root = NULL;
root = treeInsert(root, 4);
treeInsert(root, 2);
treeInsert(root, 1);
treeInsert(root, 3);
treeInsert(root, 5);
Node *head = treeToList(root);
printCircularList(head);
cout<<endl;
return 0;
}