473,404 Members | 2,195 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,404 software developers and data experts.

Add file data to tree

Ok, I feel like such an idiot with C++...gimme C# or Java any day of the week.

I have a tree program that (works) creates a tree, inorder traverses it, and then prints out messages before deleting the tree (and printing out more messages). That all works.

What doesn't work is the interface from the file to the tree. Uh, let me explain that. When I started this program, I just asked the user to input the tree from the console, easy peasy, right? Well, I actually need to take the data from a file that looks like this...

{ ROOT } //this is one tree
{ ROOT { FRED } } //this is one tree
{ ROOT { FRED { FRED_JUNIOR1 } { FRED_JUNIOR2} } }//this is one tree
{ ROOT
{ FRED
{ FRED_JUNIOR1}
{ FRED_JUNIOR2}
}
{ BARNEY
{ BARNEY_JUNIOR1}
{ BARNEY_JUNIOR2}
}
}// and this is the final tree

Since C++ is uhhh primative, I can't just use a file delimiter and a string tokenizer to strip out the whitespace and add to the tree properly.

I am getting some kind of error in " void dispose_of_tree(tree_node *current_node) " function.
Please help me.

Here is my code...

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string> 
  4.  
  5. using namespace std;
  6.  
  7. struct tree_node
  8.  {
  9.   char string[100];
  10.   tree_node *left;
  11.   tree_node *right;
  12.  };
  13.  
  14. tree_node *root;
  15.  
  16. void add_to_tree(char string[100]);
  17. void attach_node(tree_node *new_node_ptr);
  18.  
  19. void pre_order(tree_node *current_node);
  20.  
  21. void dispose_of_tree(tree_node *current_node);
  22.  
  23. int main()
  24. {
  25.  
  26.  
  27.     root = NULL; // initialize root of tree to NULL
  28.  
  29.     string line;
  30.      char string[100];
  31.      int length;
  32.     int oCount = 0;
  33.  
  34.     bool flag = true;
  35.      int target, target1, target2;
  36.     ifstream textFile ("tree.txt");
  37.      if (textFile.is_open())
  38.          {
  39.             while (! textFile.eof())
  40.                 {
  41.                     getline (textFile, line);
  42.                     for (int x = 0; x < line.length(); x++)
  43.                     {
  44.                         if (line.substr(x) == "R")
  45.                         {
  46.                         cout << line.substr(x) << endl;       
  47.                              }     
  48.  
  49.  
  50.                     }//close for
  51.  
  52.  
  53.                 }//close top while
  54.             } //close if 
  55.  
  56.     pre_order(root);
  57.     dispose_of_tree(root);
  58.     textFile.close();
  59.  
  60.     return 0;
  61. }
  62.  
  63.  
  64.  
  65. void add_to_tree(char string[20])
  66. {
  67.  tree_node *new_node_ptr;
  68.  
  69.  new_node_ptr = new tree_node;
  70.  
  71.  // Initialize new node.
  72.  strcpy(new_node_ptr->string, string);
  73.  new_node_ptr->left = NULL;
  74.  new_node_ptr->right = NULL;
  75.  
  76.  if(root == NULL)
  77.   {
  78.    root = new_node_ptr;
  79.   }
  80.  else
  81.   {
  82.    attach_node(new_node_ptr);
  83.   }
  84. }
  85.  
  86. void attach_node(tree_node *new_node_ptr)
  87. {
  88.  tree_node *search_ptr;
  89.  tree_node *follow_ptr;
  90.  
  91.  search_ptr = root;
  92.  follow_ptr = root;
  93.  
  94.  while(search_ptr != NULL)
  95.   {
  96.    follow_ptr = search_ptr;
  97.    if(strcmp(new_node_ptr->string, search_ptr->string) < 0)
  98.     {
  99.      search_ptr = search_ptr->left;
  100.     }
  101.    else
  102.     {
  103.      search_ptr = search_ptr->right;
  104.     }
  105.   }
  106.  if(strcmp(new_node_ptr->string, follow_ptr->string) < 0)
  107.   {
  108.    follow_ptr->left = new_node_ptr;
  109.   }
  110.  else
  111.   {
  112.    follow_ptr->right = new_node_ptr;
  113.   }
  114. }
  115.  
  116.  
  117.  
  118. void pre_order(tree_node *current_node)
  119. {
  120.  if(current_node != NULL)
  121.   {
  122.    cout << "HELLO" <<current_node->  string << endl;
  123.    pre_order(current_node->left);
  124.    pre_order(current_node->right);
  125.   }
  126. }
  127.  
  128.  
  129.  
  130. void dispose_of_tree(tree_node *current_node)
  131. {
  132.  if(!((current_node->left == NULL) && (current_node->right == NULL)))
  133.   {
  134.    if(current_node->right != NULL)
  135.     {
  136.      dispose_of_tree(current_node->right);
  137.     }
  138.    if(current_node->left != NULL)
  139.     {
  140.      dispose_of_tree(current_node->left);
  141.     }
  142.   }
  143.      cout << "GOODBYE" <<current_node->  string << endl;
  144.  delete current_node;
  145. }
  146.  
Oct 19 '11 #1
2 1864
weaknessforcats
9,208 Expert Mod 8TB
This is a good area to use a finite state machine.

For C++ you implement the State desgn pattern. There is an article on this in the C++ Insights forum.
Oct 19 '11 #2
when you "dispose_of_tree", the "current_node" should be checked , not use it directly
Oct 19 '11 #3

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

Similar topics

5
by: Henry Jordon | last post by:
hello I was wondering if someone could help me get a main going on this project I've completed the header file that the professor started us on but not really sure how to get the main going. If...
2
by: Merdelus | last post by:
I'm a new visual basic learner, I need some help with this program below: create an application that either sums or averagethe rows or columns of a two dimensional array depending on user...
9
by: goosen_cug | last post by:
This program is a "Sequential List" class I want to do the Union Operation,Intersection Operation of the Set.But this program have a problem: /////////////////////////// Compiling... Set.cpp...
2
by: shblack | last post by:
Please can someone help me with this program. I am in a JAVA programming class and I am having a heck of a time. I am still having a problem with the basic concepts of JAVA but the teacher continues...
6
by: khajeddin | last post by:
this program print its own source code without opening the source file. would some one please explain me how does it work ,and what what process takes place in this program???? #include <stdio.h>...
5
by: mohammaditraders | last post by:
Question # 1 Write a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions...
4
by: isabelle | last post by:
hi, every body.. help me in this program. please!! write a c++ program that converts hexadecimal digit to its corresponding decimal value. input/output: Enter a hexadecimal digit :A The...
15
by: johnnash | last post by:
#include<stdio.h> extern int a; call() { a = 3; }
2
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I just start to learn C#. Can I run this program from VS.net instead of from DOS. Thanks. Betty using System;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.