Sei sulla pagina 1di 7

17.

Print All Pairs from two BST

A
MINI PROJECT

DATA STRUCTURES
(CS1303)
III SEMESTER

Submitted
By

KARTIKEYA CHAUHAN KASAM NAGA NIDHITH


179301095 179301096
CSE-3B
Department of Computer Science and Engineering
SCHOOL OF COMPUTING AND INFORMATION TECHNOLOGY
MANIPAL UNIVERSITY JAIPUR
2018-19
Problem Statement: Given two BSTs containing N1 and N2 distinct nodes
respectively and given a value x, find the count of all pairs from both the BSTs whose
sum is equal to x.

Program Code (rtf pasted from text-editor) :


1 #include <stdio.h>
2 #include <stdlib.h>
3 #define MAX 20
4
5 struct tree{
6 int data;
7 struct tree* left, *right;
8 };
9
10 struct stack{
11 int size;
12 int top;
13 struct tree* *s;
14 };
15
16 typedef struct stack stack;
17 typedef struct tree tree;
18
19
20 tree* insert(tree* r, int n){
21 if(r == NULL){
22 r=(tree*)malloc(sizeof(tree));
23 r->left = r->right = NULL;
24 r->data = n;
25 }
26 else if(n < r->data)
27 r->left = insert (r->left,n);
28 else if(n > r->data)
29 r->right = insert (r->right,n);
30 else if(n == r->data)
31 printf("Duplicate entry.");
32 return r;
33 }
34
35 stack* createStack(int size){
36 stack* STACK = (stack*) malloc(sizeof(stack));
37 STACK->size = size;
38 STACK->top = -1;
39 STACK->s = (tree**) malloc(STACK->size * sizeof(tree*));
40 return STACK;
41 }
42
43 int isFull(stack* stack){
44 return stack->top - 1 == stack->size;
45 }
46
47 int isEmpty(stack* stack){
48 return stack->top == -1;
49 }
50
51 void push(stack* stack, tree* tree){
52 if (isFull(stack))
53 return;
54 stack->s[++stack->top] = tree;
55 }
56
57 tree* pop(stack* stack){
58 if (isEmpty(stack))
59 return NULL;
60 return stack->s[stack->top--];
61 }
62
63 int countPairs(tree* root1, tree* root2, int x){
64 if (root1 == NULL || root2 == NULL)
65 return 0;
66
67 stack* s1 = createStack(MAX);
68 stack* s2 = createStack(MAX);
69
70 tree *top1, *top2;
71
72 int count = 0;
73 while (1) {
74
75 while (root1 != NULL) {
76 push(s1,root1);
77 root1 = root1->left;
78 }
79
80 while (root2 != NULL) {
81 push(s2,root2);
82 root2 = root2->right;
83 }
84
85 if (isEmpty(s1) || isEmpty(s2))
86 break;
87
88 top1 = s1->s[s1->top];
89 top2 = s2->s[s2->top];
90
91 if ((top1->data + top2->data) == x) {
92 count++;
93 pop(s1);
94 pop(s2);
95 root1 = top1->right;
96 root2 = top2->left;
97 }
98
99 else if ((top1->data + top2->data) < x) {
100 pop(s1);
101 root1 = top1->right;
102 }
103
104 else {
105 pop(s2);
106 root2 = top2->left;
107 }
108 }
109 return count;
110 }
111
112 void main(){
113 int n1,n2,ch,x,r1,r2,num1,num2;
114 tree* root1 = NULL;
115 tree* root2 = NULL;
116
117 printf("Enter no. of elements in Tree 1: ");
118 scanf("%d",&num1);
119 while(num1>0){
120 printf("Enter Tree 1: ");
121 scanf("%d",&r1);
122 root1=insert(root1,r1);
123 num1--;
124 }
125 printf("Enter no. of elements in Tree 2: ");
126 scanf("%d",&num2);
127 while(num2>0){
128 printf("Enter Tree 2: ");
129 scanf("%d",&r2);
130 root2=insert(root2,r2);
131 num2--;
132 }
133 printf("Enter value to count the pairs who have the same sum as the
value: ");
134 scanf("%d",&x);
135 printf("Number of pairs: %d\n",countPairs(root1,root2,x));
136 }
Input Method:
Enter no. of elements in Tree 1:
Enter Tree 1:
Enter no. of elements in Tree 2:
Enter Tree 2:
Enter value to count the pairs who have the same sum as the value:

Test Cases:

1. Input:
4 //No. Of elements in Tree 1
1
2
3
4
4 //No. Of elements in Tree 2
1
2
3
4
7 //sum to find number of pairs which add up to this value
Output:
Number of pairs: 2

2. Input:
3 //No. Of elements in Tree 1
1
4
5
3 //No. Of elements in Tree 2
8
9
10
8 //sum to find number of pairs which add up to this value
Output:
Number of pairs: 0

3. Input:
4 //No. Of elements in Tree 1
9
8
7
6
3 //No. Of elements in Tree 2
6
3
4
9 //sum to find number of pairs which add up to this value
Output:
Number of pairs: 1

4. Input:
7 //No. Of elements in Tree 1
5
3
7
2
4
6
8
7 //No. Of elements in Tree 2
10
6
15
3
8
11
18
16 //sum to find number of pairs which add up to this value
Output:
Number of pairs: 3

Potrebbero piacerti anche