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...
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
this one is Tree.h
the following code is from my main method to try and read the file... but it isnt correct..
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
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...
Code:
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 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
Code:
#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; }
Code:
#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..
Code:
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(); }
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
Comment