Hi guys,
I have this coding that I wanted to try out.
Basically this is meant to be done in Java as practice for the Topic trees in data structures and algorithms.
I have recently learned C++ on my own so I wanted to test my skill in C++ by tryin to solve this problem using C++.
so yea here it goes...
i have a text file as follows... -
H,E,L
-
E,B,F
-
B,A,C
-
A,null,null
-
c,null,D
-
D,null,null
-
F,null,G
-
G,null,null
-
L,J,M
-
J,I,K
-
I,null,null
-
K,null,null
-
M,null,null
-
i want to write the code that would read in the text file and store the 1st value gotten as a root, the 2nd value as the left value of the root and 3rd value as the right value of the root..
i have already given a shot at this but i aint that successful with it..
the following are the 2 other files... Tree.h and TreeNode.h
this one is TreeNode.h -
#pragma once
-
#include <iostream>
-
#include <fstream>
-
#include <string>
-
#include <deque>
-
#include <queue>
-
using namespace std;
-
-
template <class T>
-
class TreeNode
-
{
-
public:
-
TreeNode<T>(T newItem);
-
~TreeNode<T>(void);
-
void setItem(T newItem);
-
void setLeft(TreeNode newLeft);
-
void setRight(TreeNode newRight);
-
T getItem();
-
T getLeft();
-
T getRight();
-
-
private:
-
T item;
-
TreeNode left;
-
TreeNode right;
-
};
-
-
-
template <class T>
-
TreeNode<T>::TreeNode(T newItem)
-
{
-
item = newItem;
-
left = null;
-
right = null;
-
}
-
-
template <class T>
-
TreeNode<T>::~TreeNode(void)
-
{
-
}
-
-
-
template <class T>
-
void TreeNode<T>::setItem(T newItem)
-
{
-
// set methods
-
item = newItem;
-
}
-
-
template <class T>
-
void TreeNode<T>::setLeft(TreeNode newLeft)
-
{
-
left = newLeft;
-
}
-
-
template <class T>
-
void TreeNode<T>::setRight(TreeNode newRight)
-
{
-
right = newRight;
-
}
-
-
template <class T>
-
T TreeNode<T>::getItem()
-
{
-
// get methods
-
return item;
-
}
-
-
template <class T>
-
T TreeNode<T>::getLeft()
-
{
-
return left;
-
}
-
-
template <class T>
-
T TreeNode<T>::getRight()
-
{
-
return right;
-
}
-
this one is Tree.h -
#pragma once
-
#include <iostream>
-
#include <fstream>
-
#include <string>
-
#include <deque>
-
#include <queue>
-
using namespace std;
-
-
template <class T>
-
class Tree
-
{
-
public:
-
Tree<T>(T rootItem);
-
~Tree<T>(void);
-
bool isEmpty();
-
T getRoot();
-
-
private:
-
TreeNode root;
-
};
-
-
-
template <class T>
-
Tree<T>::Tree(T rootItem)
-
{
-
root = new TreeNode(rootItem);
-
}
-
-
template <class T>
-
Tree<T>::~Tree(void)
-
{
-
}
-
-
-
template <class T>
-
bool Tree<T>::isEmpty()
-
{
-
// check emtpy
-
return root == null;
-
}
-
-
template <class T>
-
T Tree<T>::getRoot()
-
{
-
// get tree root
-
return root;
-
}
-
the following code is from my main method to try and read the file... but it isnt correct.. -
typedef queue<TreeNode> TreeLinkedList;
-
TreeLinkedList myQueue;
-
-
template <class T>
-
T readOneObject(string string1)
-
{
-
char * cstr, *p;
-
int counter;
-
string str (string1);
-
string data1, data2, data3;
-
-
cstr = new char [str.size()+1];
-
strcpy (cstr, str.c_str());
-
-
// cstr now contains a c-string copy of str
-
-
int count = 0;
-
p=strtok (cstr,",");
-
count++;
-
while (p!=NULL)
-
{
-
p=strtok(NULL,",");
-
if( count == 1 )
-
{
-
data1.append(p);
-
root = data1;
-
}
-
else if( count == 2 )
-
{
-
data2.append(p);
-
}
-
else if( count == 3 )
-
{
-
data3.append(p);
-
}
-
count++;
-
if( count == 3 )
-
break;
-
}
-
-
delete[] cstr;
-
return treeObj;
-
}
-
-
void readFile(string filename1)
-
{
-
char oneline[256];
-
ifstream infile(filename1.c_str());
-
Tree * treeObj = NULL;
-
-
while(infile.good())
-
{
-
infile.getline(oneline, 256);
-
-
if(strlen(oneline) == 0)
-
{
-
continue;
-
}
-
treeObj = readOneObject(oneline);
-
-
//if( treeObj != NULL )
-
//myQueue.push(treeObj); //linkedListObj.InsertNode(shapeObj);
-
}
-
infile.close();
-
}
-
every help is appreciated.. please help me to solve this...
thanks in advance...:D
i will check for replies every 30 mins or lesser.. so plz come back and check if u had left any comments or qns for me to answer.. thanks :D
8 2602
I didn't read your code, but can you be more specific about what problems you are having?
You 'left' and 'right' items in your TreeNode class should be pointers (or references)
to a TreeNode; your Java background is shining through ;-) You're also missing
some <T> template tags here and there ...
kind regards,
Jos
I didn't read your code, but can you be more specific about what problems you are having?
well basically i want to create a method for reading in my text file and saving it to the respective right, left, root of the tree node
problems faced are that my method is wrong so it aint working.. it/the method for readin in a file isnt correct...
You 'left' and 'right' items in your TreeNode class should be pointers (or references)
to a TreeNode; your Java background is shining through ;-) You're also missing
some <T> template tags here and there ...
kind regards,
Jos
haha yea i did do a full course in Java in my 1st yr in uni.. goin to enter 2nd yr this sept.. was hopin to get some practice for a small headstart.. ;)
well gimme a few mins i will edit my code again and post it..
could ya tell me how i shd go abt implementing/writing the code to read in my text file? its definitely wrong but sort of in the right track..
You 'left' and 'right' items in your TreeNode class should be pointers (or references)
to a TreeNode; your Java background is shining through ;-) You're also missing
some <T> template tags here and there ...
kind regards,
Jos
by the way
u see anything wrong with the 1st line?
typedef queue<TreeNode> TreeLinkedList;
TreeLinkedList myQueue;
error C3203: 'TreeNode' : unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type
by the way
u see anything wrong with the 1st line?
typedef queue<TreeNode> TreeLinkedList;
TreeLinkedList myQueue;
error C3203: 'TreeNode' : unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type
Is the type TreeNode known at that location? You do have to #include it before
you want to use it; C++ is definitely not Java.
kind regards,
Jos
You've also declared TreeNode as a template class, thus, your declaration should be - typedef queue<TreeNode<some type)> > TreeLinkedList;
. The space is necessary, otherwise C++ interprets it as the >> operator and you'll get errors.
I assume this is for a course you are taking otherwise you should be using a map.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: aa |
last post by:
How do I read a value written in the first tab of the last line of a text
file where values are separated by "\t"?
|
by: Kaustubh |
last post by:
Well i have txt file with abt 1000 Sentence i want that each sentence
appear on my website each time diff sentence on each visit on website
! i have done this
<%
set...
|
by: fabrice |
last post by:
Hello,
I've got trouble reading a text file (event viewer dump) by using the
getline() function...
After 200 - 300 lines that are read correctly, it suddenly stops
reading the rest of the...
|
by: Phil Slater |
last post by:
I'm trying to process a collection of text files, reading word by word. The
program run hangs whenever it encounters a word with an accented letter
(like rôle or passé) - ie something that's not a...
|
by: J.S. |
last post by:
Let's say I have a text file with parameters like the following embedded in
the text:
@@Textbox1@@, @@Textbox2@@, etc.
Is it possible to replace the parameters in the text file with values...
|
by: Drew Berkemeyer |
last post by:
Hello,
I'm using the following code to read a text file in VB.NET.
Dim sr As StreamReader = File.OpenText(strFilePath)
Dim input As String = sr.ReadLine()
While Not input Is Nothing...
|
by: Amit Maheshwari |
last post by:
I need to read text file having data either comma seperated or tab seperated
or any custom seperator and convert into a DataSet in C# .
I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
|
by: jasvinder singh |
last post by:
Respected Sir/madam,
Can you help in providing code in 'C' for Reading text file with n number of rows and columns and putting the result in arrays.The sample file is as follows:
rim_label =...
|
by: Amkcoder |
last post by:
I am trying to read a file a character at a time,
I want to read in the new line character.
I am printing out each character to the screen.
For some reason it will not print out the new line.
I...
|
by: Hamayun Khan |
last post by:
Hi All
I have text files having queries like below.
INSERT INTO tblJobScrap (,,,,,,,,,) VALUES...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |