-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst_to_ll.cpp
90 lines (76 loc) · 1.58 KB
/
bst_to_ll.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
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
struct node {
int key;
node *left;
node *right;
node *next;
};
// A utility function to create a new BST node
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
/* A utility function to insert a new node with given key in BST */
struct node* insert(struct node* root, int key)
{
/* If the tree is empty, return a new node */
if (root == NULL) return newNode(key);
/* Otherwise, recur down the tree */
if (key < root->key)
root->left = insert(root->left, key);
else if (key > root->key)
root->right = insert(root->right, key);
/* return the (unchanged) root pointer */
return root;
}
node * init_random_tree(int size){
printf("SIZE:%d\n",size);
node *root=NULL;
int key;
for (int i = 0; i < size; ++i)
{
key=7+rand()%147;
root=insert(root,key);
printf("%d Inserted\n",key);
}
return root;
}
node *minimum(node * root){
if(!root)
return NULL;
while(root->left)
root=root->left;
return root;
}
void infix(node *root){
if(!root)
return ;
static node *tail;
infix(root->left);
if(tail)
tail->next=root;
tail=root;
infix(root->right);
}
int main(){
int l,u;
cout<<"Min Size: ";
cin>>l;
cout<<"Max Size: ";
cin>>u;
node *root = init_random_tree(l+rand()%(u-l));
infix(root);
node *head = minimum(root);
cout<<endl;
while(head->next){
cout<<head->key<<"->";
head=head->next;
}
cout<<head->key;
}