-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-bst.c
110 lines (87 loc) · 2.6 KB
/
build-bst.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
110
/*
Problem: BST: Build the BST from pre-order sequence of nodes
Description
Given a binary search tree T in which the keys of nodes are distinct positive integers. The sequence of keys visited when traversing T in a pre-order principle is a1, a2, ..., an. Compute the sequence of keys visited when traversing T in a post-order principal.
Input
Line 1: contains a positive integer n (1 <= n <= 50000)
Line 2: contains a sequence of distinct positive integer a1, a2, ..., an (1 <= ai <= 1000000)
Output
Write the sequence of keys visited when traversing T in a post-order principal (elements are separated by a SPACE character) if the binary search tree exists, and write NULL, otherwise.
Example
Input
11
10 5 2 3 8 7 9 20 15 18 40
Output
3 2 7 9 8 5 18 15 40 20 10
Example
Input
11
10 5 2 3 8 7 9 20 15 18 4
Output
NULL
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int key;
struct node *left, *right;
} Node;
// Function to create a new node
Node* newNode(int key)
{
Node* temp = (Node*)malloc(sizeof(Node));
temp->key = key;
temp->left = temp->right = NULL;
return temp;
}
// Function to insert a node in the BST constructed from the pre-order traversal
Node* insert(Node* node, int pre[], int* preIndex, int min, int max, int size)
{
// Base case
if (*preIndex >= size)
return NULL;
Node* root = NULL;
// If the current element of pre[] is in range, then only it is part of the current subtree
int key = pre[*preIndex];
if (key > min && key < max) {
// Allocate memory for root of this subtree and increment *preIndex
root = newNode(key);
*preIndex = *preIndex + 1;
if (*preIndex < size) {
// Construct the subtree under root
root->left = insert(root, pre, preIndex, min, key, size);
root->right = insert(root, pre, preIndex, key, max, size);
}
}
return root;
}
// Function for post-order traversal of BST
void postOrder(Node* node)
{
if (node == NULL)
return;
postOrder(node->left);
postOrder(node->right);
printf("%d ", node->key);
}
// Main function to test above functions
int main()
{
int n;
scanf("%d", &n);
int pre[n];
for (int i = 0; i < n; i++) {
scanf("%d", &pre[i]);
}
int preIndex = 0;
Node* root = insert(NULL, pre, &preIndex, INT_MIN, INT_MAX, n);
// If the entire pre[] array hasn't been used, then the given sequence can't form a BST
if (preIndex != n) {
printf("NULL\n");
} else {
postOrder(root);
printf("\n");
}
return 0;
}