Construct BST From Given Traversals. Let Us Consider The Below Traversals: Inorder Sequence: D B E A F C, Preorder Sequence: A B D E C F

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ASSIGNEMENT 1

Q1. Construct BST from given traversals. Let us consider the below traversals:
Inorder sequence: D B E A F C , Preorder sequence: A B D E C F

#include <stdio.h>

struct node {
char data;
struct node* left;
struct node* right;
};

int search(char arr[], int strt, int end, char value);


struct node* newNode(char data);

struct node* buildTree(char in[], char pre[], int inStrt, int inEnd)
{
static int preIndex = 0;

if (inStrt > inEnd)


return NULL;

struct node* tNode = newNode(pre[preIndex++]);

if (inStrt == inEnd)
return tNode;

int inIndex = search(in, inStrt, inEnd, tNode->data);

tNode->left = buildTree(in, pre, inStrt, inIndex - 1);


tNode->right = buildTree(in, pre, inIndex + 1, inEnd);

return tNode;
}

int search(char arr[], int strt, int end, char value)


{
int i;
for (i = strt; i <= end; i++) {
if (arr[i] == value)
return i;
}
}
struct node* newNode(char data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return (node);
}
void printInorder(struct node* node)
{
if (node == NULL)
return;

printInorder(node->left);

printf("%c ", node->data);

printInorder(node->right);
}

int main()
{
char in[] = { 'D', 'B', 'E', 'A', 'F', 'C' };
char pre[] = { 'A', 'B', 'D', 'E', 'C', 'F' };
int len = sizeof(in) / sizeof(in[0]);
struct node* root = buildTree(in, pre, 0, len - 1);

printf("Inorder traversal of the tree is \n");


printInorder(root);
getchar();
}

Q2.
Construct BST from given preorder traversal
10, 5, 1, 7, 40, 50

#include <stdio.h>

struct node
{
int data;
struct node* left;
struct node* right;
};

struct node* newNode(int data)


{
struct node* temp
= (struct node*)malloc(sizeof(struct node));

temp->data = data;
temp->left = temp->right = NULL;

return temp;
}

struct node* constructTreeUtil(int pre[], int* preIndex,int low, int high, int size)
{
if (*preIndex >= size || low > high)
return NULL;

struct node* root = newNode(pre[*preIndex]);


*preIndex = *preIndex + 1;
if (low == high)
return root;
int i;
for (i = low; i <= high; ++i)
if (pre[i] > root->data)
break;
root->left = constructTreeUtil(pre, preIndex, *preIndex,i - 1, size);
root->right = constructTreeUtil(pre, preIndex, i, high, size);
return root;
}
struct node* constructTree(int pre[], int size)
{
int preIndex = 0;
return constructTreeUtil(pre, &preIndex, 0, size - 1,
size);
}

void printInorder(struct node* node)


{
if (node == NULL)
return;
printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}

int main()
{
int pre[] = { 10, 5, 1, 7, 40, 50 };
int size = sizeof(pre) / sizeof(pre[0]);

struct node* root = constructTree(pre, size);

printf("Inorder traversal of the BST tree is: \n");


printInorder(root);

return 0;
}

Q3.
Write a algorithm to find a pair with a given sum in BST

Step1: Create a forward and backward iterator for BST.


Step2: Now at step,
● If V1 + V2 = X, we found a pair.
● If V1 + V2 < X, We will make the forward iterator point to the next element.
● If V1 + V2 > X, We will make backward iterator point to the previous element.

Step 3: If we find no such pair, answer will be “No”


Input : 5

5
/ \ -------->>> Output: Yes
/ \ 2+3 = 5.
3 7
/ \ / \
2 4 6 8

You might also like