-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst2dll.c
109 lines (97 loc) · 2.16 KB
/
bst2dll.c
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
/*
* This program transform a doubly-linked list(dll) to a
* binary search tree(bst) in place.
* In this program, left pointer is used to point to the previous node, and right
* pointer is used to point to the next node in the dll.
*/
#include <stdio.h>
#include <stdlib.h>
#include "bst_generator.h"
/* doubly-linked list. */
struct dll {
struct node* head;
struct node* tail;
};
void bst2dll(struct node* root, struct dll* list);
/* @param head_tail stores either head or tail. */
void _bst2dll(struct node* root, struct node** head, struct node** tail);
void print_dll(struct dll list);
void destruct_dll(struct dll list);
int
main(int argc, char** argv) {
random_bst();
struct node* bst_root = generate_bst(20);
printf("Pre-order\n");
pre_order_bst(bst_root);
printf("\n");
printf("level-order\n");
level_order_bst(bst_root);
printf("\n");
printf("Is a bst? %d\n", is_bst(bst_root));
in_order_bst(bst_root);
printf("\n");
struct dll list;
bst2dll(bst_root, &list);
print_dll(list);
destruct_dll(list);
return 0;
}
void
bst2dll(struct node* root, struct dll* list) {
if (root == NULL) {
list->head = NULL;
list->tail = NULL;
return;
}
_bst2dll(root, &(list->head), &(list->tail));
}
void
_bst2dll(struct node* root, struct node** head, struct node** tail) {
if (root == NULL) {
*head = NULL;
*tail = NULL;
return;
}
struct node* lhead;
struct node* ltail;
_bst2dll(root->left, &lhead, <ail);
struct node* rhead;
struct node* rtail;
_bst2dll(root->right, &rhead, &rtail);
root->left = ltail;
if (ltail != NULL) {
ltail->right = root;
}
root->right = rhead;
if (rhead != NULL) {
rhead->left = root;
}
if (lhead != NULL) {
*head = lhead;
} else {
*head = root;
}
if (rtail != NULL) {
*tail = rtail;
} else {
*tail = root;
}
}
void
print_dll(struct dll list) {
struct node* tmp = list.head;
while (tmp != NULL) {
printf("%d ", tmp->d);
tmp = tmp->right;
}
printf("\n");
}
void
destruct_dll(struct dll list) {
struct node* tmp = list.head;
while (list.head != NULL) {
tmp = list.head;
list.head = list.head->right;
free(tmp);
}
}