SlideShare a Scribd company logo
1 of 7
Download to read offline
Hi,
BST question
I was wondering why my insert function works perfectly to insert file data in the tree but doesnt
when i insert single one
here is my function
int BSTInsert(BST *tree, BSTKey key, BSTValue value)
{
BSTNode *prev = NULL;
BSTNode *cur = tree->Root;
//
// first we search the tree to see if it already contains key:
//
while (cur != NULL)
{
if (BSTCompareKeys(key, cur->Key) == 0) // already in tree, failed:
return 0;
else if (BSTCompareKeys(key, cur->Key) < 0) // smaller, go left:
{
prev = cur;
cur = cur->Left;
}
else // larger, go right:
{
prev = cur;
cur = cur->Right;
}
}
//
// If we get here, tree does not contain key, so insert new node
// where we fell out of tree:
//
BSTNode *T = (BSTNode *)malloc(sizeof(BSTNode));
T->Key = key;
T->Value = value;
T->Left = NULL;
T->Right = NULL;
//
// link T where we fell out of tree -- after prev:
//
if (prev == NULL) // tree is empty, insert @ root:
{
tree->Root = T;
}
else if (BSTCompareKeys(key, prev->Key) < 0) // smaller, insert to left:
{
prev->Left = T;
}
else // larger, insert to right:
{
prev->Right = T;
}
tree->Count++;
return 1; // success:
}
here i insert data from file
fgets(buf, sizeof(buf), fp);
buf[strcspn(buf, "  ")] = '0';
token = strtok(buf, "t");
y=atoi(token);
value.Y = y;
token=strtok(NULL, " ");
strcpy(x,token);
value.X = (char *)malloc((strlen(x) + 1) * sizeof(char));
strcpy(value.X, x);
BSTInsert(tree, value.X, value);
while (fgets(buf, sizeof(buf), fp) != NULL)
{
buf[strcspn(buf, "  ")] = '0';
token = strtok(buf, "t");
y=atoi(token);
value.Y = y;
token=strtok(NULL, " ");
strcpy(x,token);
value.X = (char *)malloc((strlen(x) + 1) * sizeof(char));
strcpy(value.X, x);
BSTInsert(tree, value.X, value);
}
here i insert data from keyboard
BSTValue value1;
long long int weight;
char part2[512];
int part2size = sizeof(part2) / sizeof(part2[0]);
//
// add weight text
//
scanf("%lld %s", &weight, phrase);
fgets(part2, part2size, stdin);
part2[strcspn(part2, "  ")] = '0'; // strip EOL char(s):
strcat(phrase, part2);
value1.Y = weight;
value1.X = (char *)malloc((strlen(phrase) + 1) * sizeof(char));
strcpy(value1.X, phrase);
BSTInsert(tree, value1.X, value1);
if(BSTInsert(tree, value1.X, value1)==1){
printf("**Added ");
}
else
{
printf("**Not dded ");
}
thanks in advance
Solution
int BSTInsert(BST *tree, BSTKey key, BSTValue value)
{
BSTNode *prev = NULL;
BSTNode *cur = tree->Root;
//
// first we search the tree to see if it already contains key:
//
while (cur != NULL)
{
if (BSTCompareKeys(key, cur->Key) == 0) // already in tree, failed:
return 0;
else if (BSTCompareKeys(key, cur->Key) < 0) // smaller, go left:
{
prev = cur;
cur = cur->Left;
}
else // larger, go right:
{
prev = cur;
cur = cur->Right;
}
}
//
// If we get here, tree does not contain key, so insert new node
// where we fell out of tree:
//
BSTNode *T = (BSTNode *)malloc(sizeof(BSTNode));
T->Key = key;
T->Value = value;
T->Left = NULL;
T->Right = NULL;
//
// link T where we fell out of tree -- after prev:
//
if (prev == NULL) // tree is empty, insert @ root:
{
tree->Root = T;
}
else if (BSTCompareKeys(key, prev->Key) < 0) // smaller, insert to left:
{
prev->Left = T;
}
else // larger, insert to right:
{
prev->Right = T;
}
tree->Count++;
return 1; // success:
}
here i insert data from file
fgets(buf, sizeof(buf), fp);
buf[strcspn(buf, "  ")] = '0';
token = strtok(buf, "t");
y=atoi(token);
value.Y = y;
token=strtok(NULL, " ");
strcpy(x,token);
value.X = (char *)malloc((strlen(x) + 1) * sizeof(char));
strcpy(value.X, x);
BSTInsert(tree, value.X, value);
while (fgets(buf, sizeof(buf), fp) != NULL)
{
buf[strcspn(buf, "  ")] = '0';
token = strtok(buf, "t");
y=atoi(token);
value.Y = y;
token=strtok(NULL, " ");
strcpy(x,token);
value.X = (char *)malloc((strlen(x) + 1) * sizeof(char));
strcpy(value.X, x);
BSTInsert(tree, value.X, value);
}
here i insert data from keyboard
BSTValue value1;
long long int weight;
char part2[512];
int part2size = sizeof(part2) / sizeof(part2[0]);
//
// add weight text
//
scanf("%lld %s", &weight, phrase);
fgets(part2, part2size, stdin);
part2[strcspn(part2, "  ")] = '0'; // strip EOL char(s):
strcat(phrase, part2);
value1.Y = weight;
value1.X = (char *)malloc((strlen(phrase) + 1) * sizeof(char));
strcpy(value1.X, phrase);
BSTInsert(tree, value1.X, value1);
if(BSTInsert(tree, value1.X, value1)==1){
printf("**Added ");
}
else
{
printf("**Not dded ");
}

More Related Content

Similar to Hi,BST question I was wondering why my insert function works p.pdf

I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
forladies
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
ajayadinathcomputers
 
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
petercoiffeur18
 
Add to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdfAdd to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdf
flashfashioncasualwe
 
1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf
arjuncp10
 

Similar to Hi,BST question I was wondering why my insert function works p.pdf (7)

I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
 
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
 
Hash table and heaps
Hash table and heapsHash table and heaps
Hash table and heaps
 
Add to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdfAdd to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdf
 
1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf1. Add a breadth-first (level-order) traversal function to the binar.pdf
1. Add a breadth-first (level-order) traversal function to the binar.pdf
 
(C++ exercise) 1.Implement a circular, doubly linked list with a has.docx
(C++ exercise) 1.Implement a circular, doubly linked list with a has.docx(C++ exercise) 1.Implement a circular, doubly linked list with a has.docx
(C++ exercise) 1.Implement a circular, doubly linked list with a has.docx
 

More from venkatesh24685

java programming 5th edition chapter 8 user defined classes and ADTs.pdf
java programming 5th edition chapter 8 user defined classes and ADTs.pdfjava programming 5th edition chapter 8 user defined classes and ADTs.pdf
java programming 5th edition chapter 8 user defined classes and ADTs.pdf
venkatesh24685
 
Explain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdfExplain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdf
venkatesh24685
 
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdfC# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
venkatesh24685
 
“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf
venkatesh24685
 
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdfYou have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
venkatesh24685
 
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdfWhy are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
venkatesh24685
 
When we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdfWhen we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdf
venkatesh24685
 

More from venkatesh24685 (20)

java programming 5th edition chapter 8 user defined classes and ADTs.pdf
java programming 5th edition chapter 8 user defined classes and ADTs.pdfjava programming 5th edition chapter 8 user defined classes and ADTs.pdf
java programming 5th edition chapter 8 user defined classes and ADTs.pdf
 
I need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdfI need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdf
 
How each of the main themes of The Fifth Discipline are related to e.pdf
How each of the main themes of The Fifth Discipline are related to e.pdfHow each of the main themes of The Fifth Discipline are related to e.pdf
How each of the main themes of The Fifth Discipline are related to e.pdf
 
Favor Composition does what and howSolutionWe know what is in.pdf
Favor Composition does what and howSolutionWe know what is in.pdfFavor Composition does what and howSolutionWe know what is in.pdf
Favor Composition does what and howSolutionWe know what is in.pdf
 
Explain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdfExplain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdf
 
astronomy One Nineteenth Century observation which convinced many.pdf
astronomy One Nineteenth Century observation which convinced many.pdfastronomy One Nineteenth Century observation which convinced many.pdf
astronomy One Nineteenth Century observation which convinced many.pdf
 
A) For bipyridine (bipy) determine the maximum number of coordinatio.pdf
A) For bipyridine (bipy) determine the maximum number of coordinatio.pdfA) For bipyridine (bipy) determine the maximum number of coordinatio.pdf
A) For bipyridine (bipy) determine the maximum number of coordinatio.pdf
 
Describe two (2) attributes of a globally aware EngineerEngineering.pdf
Describe two (2) attributes of a globally aware EngineerEngineering.pdfDescribe two (2) attributes of a globally aware EngineerEngineering.pdf
Describe two (2) attributes of a globally aware EngineerEngineering.pdf
 
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdfC# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
 
“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf
 
18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf
18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf
18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf
 
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdfYou have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
 
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdfWhy are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
 
Which of the following are parts of a bond Choose one or more A. t.pdf
Which of the following are parts of a bond Choose one or more A. t.pdfWhich of the following are parts of a bond Choose one or more A. t.pdf
Which of the following are parts of a bond Choose one or more A. t.pdf
 
When we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdfWhen we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdf
 
What is the role of ports for global commerce and why is that roe im.pdf
What is the role of ports for global commerce and why is that roe im.pdfWhat is the role of ports for global commerce and why is that roe im.pdf
What is the role of ports for global commerce and why is that roe im.pdf
 
What are the stages of team development Why is it important for a s.pdf
What are the stages of team development Why is it important for a s.pdfWhat are the stages of team development Why is it important for a s.pdf
What are the stages of team development Why is it important for a s.pdf
 
Two numbers together add to 300. One number is twice the size of the.pdf
Two numbers together add to 300. One number is twice the size of the.pdfTwo numbers together add to 300. One number is twice the size of the.pdf
Two numbers together add to 300. One number is twice the size of the.pdf
 
This Question 2 Pts The primary goal of financial accounting is to p.pdf
This Question 2 Pts The primary goal of financial accounting is to p.pdfThis Question 2 Pts The primary goal of financial accounting is to p.pdf
This Question 2 Pts The primary goal of financial accounting is to p.pdf
 
The irregularity of innovations and the variability of business prof.pdf
The irregularity of innovations and the variability of business prof.pdfThe irregularity of innovations and the variability of business prof.pdf
The irregularity of innovations and the variability of business prof.pdf
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 

Recently uploaded (20)

How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 

Hi,BST question I was wondering why my insert function works p.pdf

  • 1. Hi, BST question I was wondering why my insert function works perfectly to insert file data in the tree but doesnt when i insert single one here is my function int BSTInsert(BST *tree, BSTKey key, BSTValue value) { BSTNode *prev = NULL; BSTNode *cur = tree->Root; // // first we search the tree to see if it already contains key: // while (cur != NULL) { if (BSTCompareKeys(key, cur->Key) == 0) // already in tree, failed: return 0; else if (BSTCompareKeys(key, cur->Key) < 0) // smaller, go left: { prev = cur; cur = cur->Left; } else // larger, go right: { prev = cur; cur = cur->Right; } } // // If we get here, tree does not contain key, so insert new node // where we fell out of tree: // BSTNode *T = (BSTNode *)malloc(sizeof(BSTNode)); T->Key = key; T->Value = value;
  • 2. T->Left = NULL; T->Right = NULL; // // link T where we fell out of tree -- after prev: // if (prev == NULL) // tree is empty, insert @ root: { tree->Root = T; } else if (BSTCompareKeys(key, prev->Key) < 0) // smaller, insert to left: { prev->Left = T; } else // larger, insert to right: { prev->Right = T; } tree->Count++; return 1; // success: } here i insert data from file fgets(buf, sizeof(buf), fp); buf[strcspn(buf, " ")] = '0'; token = strtok(buf, "t"); y=atoi(token); value.Y = y; token=strtok(NULL, " "); strcpy(x,token); value.X = (char *)malloc((strlen(x) + 1) * sizeof(char)); strcpy(value.X, x); BSTInsert(tree, value.X, value); while (fgets(buf, sizeof(buf), fp) != NULL) {
  • 3. buf[strcspn(buf, " ")] = '0'; token = strtok(buf, "t"); y=atoi(token); value.Y = y; token=strtok(NULL, " "); strcpy(x,token); value.X = (char *)malloc((strlen(x) + 1) * sizeof(char)); strcpy(value.X, x); BSTInsert(tree, value.X, value); } here i insert data from keyboard BSTValue value1; long long int weight; char part2[512]; int part2size = sizeof(part2) / sizeof(part2[0]); // // add weight text // scanf("%lld %s", &weight, phrase); fgets(part2, part2size, stdin); part2[strcspn(part2, " ")] = '0'; // strip EOL char(s): strcat(phrase, part2); value1.Y = weight; value1.X = (char *)malloc((strlen(phrase) + 1) * sizeof(char)); strcpy(value1.X, phrase); BSTInsert(tree, value1.X, value1); if(BSTInsert(tree, value1.X, value1)==1){ printf("**Added "); } else {
  • 4. printf("**Not dded "); } thanks in advance Solution int BSTInsert(BST *tree, BSTKey key, BSTValue value) { BSTNode *prev = NULL; BSTNode *cur = tree->Root; // // first we search the tree to see if it already contains key: // while (cur != NULL) { if (BSTCompareKeys(key, cur->Key) == 0) // already in tree, failed: return 0; else if (BSTCompareKeys(key, cur->Key) < 0) // smaller, go left: { prev = cur; cur = cur->Left; } else // larger, go right: { prev = cur; cur = cur->Right; } } // // If we get here, tree does not contain key, so insert new node // where we fell out of tree: // BSTNode *T = (BSTNode *)malloc(sizeof(BSTNode)); T->Key = key; T->Value = value; T->Left = NULL;
  • 5. T->Right = NULL; // // link T where we fell out of tree -- after prev: // if (prev == NULL) // tree is empty, insert @ root: { tree->Root = T; } else if (BSTCompareKeys(key, prev->Key) < 0) // smaller, insert to left: { prev->Left = T; } else // larger, insert to right: { prev->Right = T; } tree->Count++; return 1; // success: } here i insert data from file fgets(buf, sizeof(buf), fp); buf[strcspn(buf, " ")] = '0'; token = strtok(buf, "t"); y=atoi(token); value.Y = y; token=strtok(NULL, " "); strcpy(x,token); value.X = (char *)malloc((strlen(x) + 1) * sizeof(char)); strcpy(value.X, x); BSTInsert(tree, value.X, value); while (fgets(buf, sizeof(buf), fp) != NULL) { buf[strcspn(buf, " ")] = '0';
  • 6. token = strtok(buf, "t"); y=atoi(token); value.Y = y; token=strtok(NULL, " "); strcpy(x,token); value.X = (char *)malloc((strlen(x) + 1) * sizeof(char)); strcpy(value.X, x); BSTInsert(tree, value.X, value); } here i insert data from keyboard BSTValue value1; long long int weight; char part2[512]; int part2size = sizeof(part2) / sizeof(part2[0]); // // add weight text // scanf("%lld %s", &weight, phrase); fgets(part2, part2size, stdin); part2[strcspn(part2, " ")] = '0'; // strip EOL char(s): strcat(phrase, part2); value1.Y = weight; value1.X = (char *)malloc((strlen(phrase) + 1) * sizeof(char)); strcpy(value1.X, phrase); BSTInsert(tree, value1.X, value1); if(BSTInsert(tree, value1.X, value1)==1){ printf("**Added "); } else { printf("**Not dded ");
  • 7. }