473,327 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Binary Tree Help (C++)

46
Hey everyone, I need a little help with my binary tree program. The purpose of this program is just an exercise for myself, but for some reason it doesn't work. It's supposed to input words "One" through "Ten" into the binary tree(left side is alphabetically less than the top node, right is more) and I believe I've done that(but probably not). The problem is that with the read() function, it only displays three words, when they were supposed to be ten. Any help on what im doing wrong would be really really really appreciated. You would earn my respect of the highest level ^.^
Here is my current code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct Tnode{
  7.       string word;
  8.       int counter;
  9.       Tnode *right;
  10.       Tnode *left;
  11. };
  12.  
  13. void insert( Tnode *leaf, char *words[] );
  14. void read( Tnode *leaf );
  15.  
  16. int main(){
  17.     char *words[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};
  18.     Tnode *leaf;
  19.     leaf = new Tnode;
  20.     leaf->word = words[0];
  21.     insert( leaf, words );
  22.     read( leaf );
  23.     system( "pause" );
  24.     return 0;
  25. }
  26.  
  27. void insert( Tnode *leaf, char *words[] ){
  28.  
  29.      int i;
  30.      for(i=1;i<=9;i++){
  31.        //cout << "In for" << endl;
  32.        if(leaf->word.compare(words[i]) > 0){
  33.          //cout << "start Left" << endl;
  34.  
  35.          /*if(leaf != NULL){
  36.            cout << "Left null" << endl;
  37.            insert( leaf->left, words );
  38.          }else{*/
  39.  
  40.            cout << "putting " << words[i] << " into left node" << endl;
  41.            leaf->left = new Tnode;
  42.            leaf->left->word = words[i];
  43.            leaf->left->right = NULL;
  44.            leaf->left->left = NULL;
  45.  
  46.        }else if(leaf->word.compare(words[i]) <= 0 ){
  47.          //cout << "start Right" << endl;
  48.  
  49.          /*if(leaf != NULL){
  50.            cout << "Right null" << endl;
  51.            insert( leaf->right, words );
  52.          }else{*/
  53.  
  54.            cout << "putting " << words[i] << " into right node" << endl;
  55.            leaf->right = new Tnode;
  56.            leaf->right->word = words[i];
  57.            leaf->right->right = NULL;
  58.            leaf->right->left = NULL;
  59.  
  60.        }
  61.      }
  62. }
  63.  
  64. void read( Tnode *leaf ){
  65.    if(leaf->left!=NULL)
  66.      read(leaf->left);
  67.    cout << leaf->word << "->";
  68.    if(leaf->right!=NULL)
  69.      read(leaf->right);
  70.  
  71. }
  72.  
Thanks,
Austen
Mar 2 '07 #1
4 3095
sake
46
Wow, looking at my code now, I can safely say I'm an idiot. Im not propelling through the tree at all. When I run into a problem, i'll post, but for now, just disregard the post above.

Sorry,
Austen
Mar 2 '07 #2
sake
46
Sorry for triple posting, but I felt no need to start a new thread.
So now I have the same objective and everything, but a more sophisticated approach with some possible potential ;). I have no idea what to do right now though. I'm completely stuck. I know my problem is when I compare my word in the character array to the (nonexistent) word in the tree. I get an error during the program run because I'm trying to access data that doesn't exist. So here's what I want to do:
  • Find the alphabetical relevance of the word in the character array to the parent node's word.
  • Insert the character array word appropriately
  • Continue with the list
You'll notice that the last bullet was kinda vague. That's because I don't know how to go about doing it. I know this is probably a pretty easy concept, but I just can't grasp it.

Simply put: How do I go about continuing in my tree without trying to access nonexistent memory?

Heres my new code if relevant:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct Tnode{
  7.       string word;
  8.       int counter;
  9.       Tnode *right;
  10.       Tnode *left;
  11. };
  12.  
  13. void insert( Tnode *leaf, char *words[], int count );
  14. void read( Tnode *leaf );
  15.  
  16. int main(){
  17.     char *words[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};
  18.     Tnode *leaf;
  19.     insert( leaf, words, 1 );
  20.     read( leaf );
  21.     system( "pause" );
  22.     return 0;
  23. }
  24.  
  25. void insert( Tnode *leaf, char *words[], int count ){
  26.  
  27.   cout<<"In function"<<endl;
  28.  
  29.   if(leaf->word.compare(words[count]) > 0)
  30.   {
  31.     cout << "startLeft" << endl;
  32.     if(leaf->left!=NULL)
  33.      insert(leaf->left, words, count);
  34.     else
  35.     {
  36.       leaf->left=new Tnode;
  37.       leaf->left->word=words[count];
  38.       leaf->left->left=NULL;    
  39.       leaf->left->right=NULL;   
  40.       count++;
  41.     }  
  42.   }
  43.   else if(leaf->word.compare(words[count]) <= 0)
  44.   {
  45.     cout << "startRight" << endl;
  46.     if(leaf->right!=NULL)
  47.       insert(leaf->right, words, count);
  48.     else
  49.     {
  50.       leaf->right=new Tnode;
  51.       leaf->right->word=words[count];
  52.       leaf->right->left=NULL; 
  53.       leaf->right->right=NULL;
  54.       count++;
  55.     }
  56.   }
  57.  
  58. }
  59.  
  60. void read( Tnode *leaf ){
  61.    if(leaf->left!=NULL)
  62.      read(leaf->left);
  63.    cout << leaf->word << "->";
  64.    if(leaf->right!=NULL)
  65.      read(leaf->right);
  66.  
  67. }
  68.  
  69.  
Mar 2 '07 #3
sake
46
ok I got it working with this code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct Tnode{
  7.       string word;
  8.       int counter;
  9.       Tnode *right;
  10.       Tnode *left;
  11. };
  12.  
  13. void insert( Tnode *leaf, char *words[], int count );
  14. void read( Tnode *leaf );
  15.  
  16. int main(){
  17.     char *words[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};//word array
  18.     Tnode *leaf;//binary tree
  19.     int counter = 0;//keeps track of current word
  20.  
  21.     leaf = new Tnode;//sets up first few node for insertion
  22.     leaf->word = words[0];
  23.     leaf->right = NULL;
  24.     leaf->left = NULL;
  25.  
  26.     for(counter=1;counter<10;counter++){//counts once for each word
  27.       insert( leaf, words, counter );
  28.     }
  29.  
  30.     read( leaf );
  31.     system( "pause" );
  32.     return 0;
  33. }
  34.  
  35. void insert( Tnode *leaf, char *words[], int count ){
  36.  
  37.   //cout<<"In function"<<endl; //debugging feature
  38.  
  39.   if(leaf->word.compare(words[count]) > 0)//compare alphabetic relevance between the two words
  40.   {
  41.     //cout << "startLeft" << endl; //debugging feature
  42.     if(leaf->left!=NULL){
  43.      //cout << "Left not NULL" << endl; //debugging feature
  44.      insert(leaf->left, words, count);
  45.     }else if(leaf->left == NULL)
  46.     {
  47.       //cout << "Left NULL" << endl; //debugging feature
  48.       leaf->left=new Tnode;
  49.       leaf->left->word=words[count];
  50.       leaf->left->left=NULL;    
  51.       leaf->left->right=NULL;   
  52.  
  53.     }  
  54.   }
  55.   else if(leaf->word.compare(words[count]) <= 0)//compare alphabetic relevance between the two words
  56.   {
  57.     //cout << "startRight" << endl; //debugging feature
  58.     if(leaf->right!=NULL){
  59.       //cout << "Right not NULL(" << leaf->right->word << ")" << endl; //debugging feature
  60.       insert(leaf->right, words, count);
  61.     }else if(leaf->right == NULL)
  62.     {
  63.       //cout << "Right NULL" << endl; //debugging feature
  64.       leaf->right=new Tnode;
  65.       leaf->right->word=words[count];
  66.       leaf->right->left=NULL; 
  67.       leaf->right->right=NULL;
  68.  
  69.     }
  70.   }
  71.  
  72. }
  73.  
  74. void read( Tnode *leaf ){
  75.    if(leaf->left!=NULL)
  76.      read(leaf->left);
  77.    cout << leaf->word << " ";   
  78.    if(leaf->right!=NULL)
  79.      read(leaf->right);
  80.    return;
  81. }
  82.  
Mar 2 '07 #4
Ganon11
3,652 Expert 2GB
Wow, really sorry that you had to go through 4 posts with no help, but I'm glad you got it working on your own. Binary Trees confused me, too, when I first started out.
Mar 2 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am > cs students and are now facing difficult problems in understanding > what a binary tree is, how it works, and the algorithm...
8
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am cs students and are now facing difficult problems in understanding what a binary tree is, how it works, and the algorithm to...
11
by: jova | last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so can someone please explain. Thank You.
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
5
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
4
by: Ken | last post by:
I have a binary tree in VB NET and insertions seem to be slow. The program receives data from one source and inserts it into the tree. The program receives data from another source and...
3
by: piotrek | last post by:
Hi I would like to ask you a question. Ian creating app. that download from server directory structure ( whole tree ) and those data are placed in proper places into my treeview control. I...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
7
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.