C++ How to Read a Text File Into a Binary Serach Tree

#1

  • D.I.C Head

Reputation: 4

  • View blog
  • Posts: 68
  • Joined: 22-Apr 14

Help on text to binary search tree

Posted xxx June 2014 - 09:07 AM

Hey guys I need help on this program I'yard working on. In the textbook the problem is
"Develop a programme to read from a text file a list of words. As these words are read store them into a dynamically allocated binary search tree. Later on they are stored do a �pre-club� traversal of the tree to show the style in which they are stored from meridian to bottom. Then impress the words out in alphabetical order by doing an �in-order� traversal." and the text file is
polymorphism
object
templates
structure
grade
arrow
reference
traversal
inheritance
exceptions
recursive
overloading
Which I accept already created and is waiting to exist implemented in.

My main problem is I'm not also sure how to make this happen(Plow that text into a dynamically allocated binary search tree). I could be over thinking this past a LOT or missing ane small matter I'm not too sure, all the same here is my code:

#include "stdafx.h" #include <stdlib.h>  struct treeNode {     int data;     struct treeNode *left;     struct treeNode *right; };  void insert(struct treeNode **node, int n); void preOrder(struct treeNode *root); void inOrder(struct treeNode *root); void postOrder(struct treeNode *root);  int _tmain(int argc, _TCHAR* argv[]) {     struct treeNode *root = Null;      FILE *ptr_file;         int x;          ptr_file =fopen("tree.txt", "westward");          if (!ptr_file)             render 1;          for (x=1; x<=10; x++)             fprintf(ptr_file,"%d\n", 10);          fclose(ptr_file);      preOrder(root);     printf("\northward");     inOrder(root);     printf("\northward");     postOrder(root);     printf("\n");       return 0; }   void insert(struct treeNode **node, int northward) {     if (*node == Zip)     {         struct treeNode *newNode;         newNode = (struct treeNode *)malloc(sizeof(struct treeNode));         newNode->data = northward;         newNode->left = NULL;         newNode->correct = Zilch;         *node = newNode;     }      else if (north < (*node)->information)         insert(&((*node)->left), n);     else if (n > (*node)->data)         insert(&((*node)->correct), n); }  void preOrder(struct treeNode *root) {     if (root != Nil)     {         printf("%d ", root->information);         preOrder(root->left);         preOrder(root->right);     } }  void inOrder(struct treeNode *root) {     if (root != Zippo)     {         inOrder(root->left);         printf("%d ", root->information);         inOrder(root->right);     } }  void postOrder(struct treeNode *root) {     if (root != Nil)     {         postOrder(root->left);         postOrder(root->right);         printf("%d ", root->information);     } }            

I'thou pretty sure I need something like "fscanf..." but I'1000 not also certain what I put inside of that...at all.


Is This A Proficient Question/Topic? 0

  • +

#2 mojo666 User is offline

Reputation: 409

  • View blog
  • Posts: 885
  • Joined: 27-June 09

Re: Aid on text to binary search tree

Posted 30 June 2014 - 09:24 AM

Alter your tree node struct so data holds a string instead of an int. Change all your other functions to account for this data type change. Then equally you lot loop through the list of words in the file yous can only call insert.

#three aku10 User is offline

  • D.I.C Head

Reputation: 4

  • View blog
  • Posts: 68
  • Joined: 22-April fourteen

Re: Help on text to binary search tree

Posted xxx June 2014 - 09:31 AM

Should I arrive "char data" or "char [25]" or something similar to that?

#4 aku10 User is offline

  • D.I.C Head

Reputation: iv

  • View blog
  • Posts: 68
  • Joined: 22-April fourteen

Re: Help on text to binary search tree

Posted 30 June 2014 - 09:37 AM

I changed the "File Open" office of the lawmaking to this:

FILE *ptr_file;     		char buf[k];      		ptr_file =fopen("tree.txt","r");     		if (!ptr_file)         		return one;      		while (fgets(buf,1000, ptr_file)!=Zero)         		printf("%s",buf);            

So now it opens and reads the tree.txt instead of opens it. However, when I run it, it gives me numbers instead of words.

#v Skydiver User is offline

Reputation: 7916

  • View blog
  • Posts: 26,433
  • Joined: 05-May 12

Re: Help on text to binary search tree

Posted 30 June 2014 - 09:50 AM

Did you re-enter the words into the "tree.txt" file? If non, what you have are the numbers leftover from having run your old lawmaking equally posted in your original post -- that code would write numbers into "tree.txt".

#6 aku10 User is offline

  • D.I.C Head

Reputation: 4

  • View blog
  • Posts: 68
  • Joined: 22-April fourteen

Re: Assist on text to binary search tree

Posted 30 June 2014 - 09:54 AM

Yeah that'southward the reverse of what I need to happen haha, I demand the copse.txt to open and salvage it into a binary search tree. When I tried to go open up information technology, nothing was saved but the original text, so no nothing saved into it afterward I ran information technology. I also changed the "int data" to "char information[25]".

#7 aku10 User is offline

  • D.I.C Caput

Reputation: four

  • View blog
  • Posts: 68
  • Joined: 22-April 14

Re: Help on text to binary search tree

Posted 30 June 2014 - 10:07 AM

Just tried to run it over again, merely now I'm running into this problem I'm not likewise certain what I need to change hither, I tried changing the prototype and the office to

void insert(struct treeNode **node, char d)            

However at present I'm getting these compile errors:

1>------ Build started: Project: Trees, Configuration: Debug Win32 ------ 1>  Trees.cpp 1>f:\programs\copse\copse\trees.cpp(26): alert C4996: 'fopen': This function or variable may be dangerous. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. Meet online assist for details. 1>          c:\program files (x86)\microsoft visual studio ten.0\vc\include\stdio.h(234) : run into declaration of 'fopen' ane>f:\programs\trees\copse\trees.cpp(53): error C2440: '=' : cannot catechumen from 'int' to 'char [25]' 1>          In that location are no conversions to array types, although there are conversions to references or pointers to arrays 1>f:\programs\trees\copse\copse.cpp(59): error C2446: '<' : no conversion from 'char *' to 'int' 1>          At that place is no context in which this conversion is possible 1>f:\programs\trees\trees\trees.cpp(59): mistake C2040: '<' : 'int' differs in levels of indirection from 'char [25]' 1>f:\programs\trees\copse\trees.cpp(61): fault C2446: '>' : no conversion from 'char *' to 'int' 1>          There is no context in which this conversion is possible ane>f:\programs\trees\trees\trees.cpp(61): fault C2040: '>' : 'int' differs in levels of indirection from 'char [25]' ========== Build: 0 succeeded, 1 failed, 0 up-to-appointment, 0 skipped ==========            

This post has been edited by aku10: thirty June 2014 - 10:07 AM

#8 aku10 User is offline

  • D.I.C Head

Reputation: iv

  • View blog
  • Posts: 68
  • Joined: 22-April 14

Re: Help on text to binary search tree

Posted 30 June 2014 - 10:17 AM

I got past the compile errors, all but 1 actually. I changed the prototype and the function to:

void insert(struct treeNode **node, char *n)            

so now it will have it but I have i more bug and that's

1>f:\program\trees\trees\trees.cpp(53): error C2440: '=' : cannot catechumen from 'char *' to 'char [25]'            

#9 mojo666 User is offline

Reputation: 409

  • View blog
  • Posts: 885
  • Joined: 27-June 09

Re: Help on text to binary search tree

Posted 30 June 2014 - eleven:39 AM

Y'all are going to have to change the operations in your functions since = and comparison operators don't really brand sense in the context of character pointers. You might want to look up cord functions similar strcpy and strcmp.

#10 aku10 User is offline

  • D.I.C Caput

Reputation: iv

  • View blog
  • Posts: 68
  • Joined: 22-April 14

Re: Assist on text to binary search tree

Posted 30 June 2014 - 12:20 PM

I accept just removed it, took care of the problem all together. At present I'one thousand just trying to figure out why I'g however getting numbers instead of the text from the .txt I am trying to tell information technology to read and print in the tree form I'm supposed to print it in.

#11 mojo666 User is offline

Reputation: 409

  • View blog
  • Posts: 885
  • Joined: 27-June 09

Re: Help on text to binary search tree

Posted thirty June 2014 - 12:22 PM

please post your updated lawmaking.

#12 aku10 User is offline

  • D.I.C Caput

Reputation: 4

  • View blog
  • Posts: 68
  • Joined: 22-April 14

Re: Help on text to binary search tree

Posted 30 June 2014 - 12:27 PM

Updated Code:

// Trees.cpp : Defines the entry point for the panel application. //  #include "stdafx.h" #include <stdlib.h>  struct treeNode { 	char data[25]; 	struct treeNode *left; 	struct treeNode *right; };  void insert(struct treeNode **node, char *n); void preOrder(struct treeNode *root); void inOrder(struct treeNode *root); void postOrder(struct treeNode *root);  int _tmain(int argc, _TCHAR* argv[]) { 	struct treeNode *root = Zero;  		FILE *ptr_file;     		char data[25];      		ptr_file =fopen("tree.txt","r");     		if (!ptr_file)         		return 1;      		while (fgets(data,1000, ptr_file)!=Null)         		printf("%s",data);  	preOrder(root); 	printf("\northward"); 	inOrder(root); 	printf("\n"); 	postOrder(root); 	printf("\n");  	fclose(ptr_file);   	return 0; }   void insert(struct treeNode **node, char *n) {  	if (*node == Nix) 	{ 		struct treeNode *newNode; 		newNode = (struct treeNode *)malloc(sizeof(struct treeNode)); 		newNode->left = NULL; 		newNode->right = NULL; 		*node = newNode; 	}  	else if (n < (*node)->information) 		insert(&((*node)->left), due north); 	else if (north > (*node)->data) 		insert(&((*node)->right), n); }  void preOrder(struct treeNode *root) { 	if (root != Cypher) 	{ 		printf("%d ", root->data); 		preOrder(root->left); 		preOrder(root->right); 	} }  void inOrder(struct treeNode *root) { 	if (root != NULL) 	{ 		inOrder(root->left); 		printf("%d ", root->data); 		inOrder(root->right); 	} }  void postOrder(struct treeNode *root) { 	if (root != Nil) 	{ 		postOrder(root->left); 		postOrder(root->right); 		printf("%d ", root->data); 	} }            

#13 snoopy11 User is offline

Reputation: 1594

  • View blog
  • Posts: 5,010
  • Joined: twenty-March x

Re: Assist on text to binary search tree

Posted thirty June 2014 - 01:42 PM

Well,

Its closer I suppose but its a really poor attempt isn't it..

The advice has been skillful only y'all are not listening...

You say y'all are getting numbers...

void postOrder(struct treeNode *root) 	{ 	    if (root != Nil) 	    { 	        postOrder(root->left); 	        postOrder(root->right); 	        printf("%d ", root->data); 	    } 	}            

notice the %d at that place but you had been told to change all occurences to %due south or strings in other words.

Where exercise you use insert...

Do you lot maybe think you might need to insert the information...??

Your insert should besides look something like this...

void insert(struct treeNode **node, char *north) {  	if (*node == NULL) 	{ 		struct treeNode *newNode; 		newNode = malloc(sizeof(struct treeNode)); 		newNode->left = Cypher; 		newNode->right = Nothing; 		strcpy(newNode->information, due north); 		*node = newNode; 	}  	else if (0 < strcmp(((*node)->information), n)) 		insert(&((*node)->left), north); 	else if (0 > strcmp(((*node)->data),northward)) 		insert(&((*node)->right), due north); }            

Every bit you were told you lot will demand to use strcmp and strcpy....

I would hit the books again and endeavor information technology with a fresh forenoon caput on...

Snoopy.

#xiv mojo666 User is offline

Reputation: 409

  • View blog
  • Posts: 885
  • Joined: 27-June 09

Re: Assist on text to binary search tree

Posted 30 June 2014 - 01:43 PM

View Postaku10, on xxx June 2014 - 08:27 PM, said:

Updated Code:

                while (fgets(data,grand, ptr_file)!=NULL)         		printf("%s",data);              

Does this part print numbers or are y'all talking virtually the inorder, preorder parts? If the quoted function is press correctly, and so yous need to supplant the print with a call to insert. In order to practice that, y'all need to fix insert. So yous demand to alter your printing functions considering they are still set to impress numbers instead of strings.

wrightsoldre.blogspot.com

Source: https://www.dreamincode.net/forums/topic/349524-help-on-text-to-binary-search-tree/

0 Response to "C++ How to Read a Text File Into a Binary Serach Tree"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel