-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsorted-list-to-bst.cpp
120 lines (103 loc) · 2.39 KB
/
sorted-list-to-bst.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// http://www.geeksforgeeks.org/sorted-linked-list-to-balanced-bst/
// code for method 2 in gfg
#include <iostream>
using namespace std;
class lnode {
public:
int data;
lnode *next;
lnode(int data) {
this->data = data;
this->next = NULL;
}
};
class tnode {
public:
int data;
tnode *left, *right;
tnode(int data) {
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
tnode* sortedListToBst(lnode *head);
tnode* sortedListToBstAux(lnode **head, int n);
int countNodes(lnode *head);
lnode* addnode(lnode *head, int data);
void printList(lnode *head);
void preorder(tnode *root);
void inorder(tnode *root);
tnode* sortedListToBst(lnode *head) {
int n = countNodes(head);
return sortedListToBstAux(&head, n);
}
tnode* sortedListToBstAux(lnode **head, int n) {
if (n <= 0)
return NULL;
tnode *left = sortedListToBstAux(head, n/2);
tnode *root = new tnode((*head)->data);
root->left = left;
(*head) = (*head)->next;
root->right = sortedListToBstAux(head, n-n/2-1);
return root;
}
int countNodes(lnode *head) {
int count = 0;
lnode *ptr = head;
while (ptr != NULL) {
count++;
ptr = ptr->next;
}
return count;
}
lnode* addnode(lnode *head, int data) {
if (head == NULL)
return new lnode(data);
lnode *ptr = head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new lnode(data);
return head;
}
void printList(lnode *head) {
if (head == NULL)
return;
while (head != NULL) {
cout<<head->data<<" ";
head = head->next;
}
}
void preorder(tnode *root) {
if (root == NULL)
return;
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
void inorder(tnode *root) {
if (root == NULL)
return;
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
int main() {
lnode *head = NULL;
head = addnode(head, 1);
head = addnode(head, 2);
head = addnode(head, 3);
head = addnode(head, 4);
head = addnode(head, 5);
head = addnode(head, 6);
head = addnode(head, 7);
cout<<"list: ";
printList(head);
cout<<endl<<"preorder of BST: ";
tnode *root = sortedListToBst(head);
preorder(root);
cout<<endl<<"inorder of BST: ";
inorder(root);
cout<<endl;
return 0;
}