| | |
Reading a file into a binary search tree
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 91
Reputation:
Solved Threads: 1
i know how to get a file and all but how do u go about putting that file into the tree?
my get file
do i just go key = inFile
here is my key struct
my BST Class
my get file
C++ Syntax (Toggle Plain Text)
void BST::loadFile() { cout << "Enter the the file location" << endl; cin >> inFileName; inFile.open(inFileName.c_str()); if (!inFile.is_open()) //test for file { cerr << "Cannot open file: " << inFileName << endl; getche(); } while(!inFile.eof()) //loop through untill end of file { inFile >> lastName >> firstName; cout << lastName << ", " << firstName << endl; }// end obtain info inFile.close();//close infile }
do i just go key = inFile
here is my key struct
C++ Syntax (Toggle Plain Text)
struct Key { char* data; // string Key(); Key(char* data) { this->data = new char[strlen(data)]; strcpy(this->data,data);} ~Key() {delete data;}; Key(Key& key); void print(); Key& operator= (Key& key); bool operator== (Key& key) { return 0 == strcmp(this->data,key.data);} // is this == to that key bool operator< (Key& key) { return -1 == strcmp(this->data,key.data);}// is this < that key bool operator> (Key& key) { return 1 == strcmp(this->data,key.data);} // is this > that key }; Key::Key() { data = NULL; } Key::Key(Key& key) { data = key.data; } Key& Key::operator= (Key& key) { data = key.data; return *this; } void Key::print() { cout << data << endl; }
my BST Class
C++ Syntax (Toggle Plain Text)
class BST_Node { private: Key key; // key holds the data BST_Node* left; // ptr to left subtree BST_Node* right; // ptr to right subtree public: // Managers BST_Node(); BST_Node(Key key); // Construct given key-data BST_Node(BST_Node& node); // Copy Constructor ~BST_Node(); // Destruct node // Operators BST_Node& operator= (BST_Node& node); // Assignment // Accessors Key getKey() {return key;}; // get Key Data BST_Node* getLeft() {return left;}; // get root of left subtree BST_Node* getRight() {return right;}; // get root of right subtree void setLeft(BST_Node* node); void setRight(BST_Node* node); }; BST_Node::BST_Node() { key.data = NULL; left = NULL; right = NULL; } BST_Node::BST_Node(Key key) { cout << "enter key" << endl; cin >> key.data; } BST_Node::BST_Node(BST_Node& node) { right = node.right; left = node.left; //key = node.key; } BST_Node::~BST_Node() { delete left; delete right; } BST_Node& BST_Node::operator= (BST_Node& node) { right = node.right; left = node.left; //key = node.key; return *this; } void BST_Node::setLeft(BST_Node* node) { this->left = node; } void BST_Node::setRight(BST_Node* node) { this->right = node; }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Mar 2005
Posts: 91
Reputation:
Solved Threads: 1
yea i have an insert function already
C++ Syntax (Toggle Plain Text)
bool BST::insert(BST_Node* &subRoot, Key key) { BST_Node* node = new BST_Node(key); if(!subRoot) { subRoot = node; return true; } if(key == root->getKey()) { return false; delete node; } if(key < subRoot->getKey()) { if(subRoot->getLeft() == NULL) { root->setLeft(new BST_Node(key)); } else { insert(subRoot->getRight(), key.data); } } }
So just use the insert function in main.
The only thing I would do different is to use ofstream and ifstream for file i/o. And avoid anything with (EOF) like the plague.
Depending how your file is structured, will determine how you read it in. I'm assuming it looks something like this:
file.txt
Therefore you could read in each line into the binary search tree, using the getline command?
The only thing I would do different is to use ofstream and ifstream for file i/o. And avoid anything with (EOF) like the plague.
Depending how your file is structured, will determine how you read it in. I'm assuming it looks something like this:
file.txt
C++ Syntax (Toggle Plain Text)
Smith Chris Jones Antony Anderson Clive
Therefore you could read in each line into the binary search tree, using the getline command?
*Voted best profile in the world*
![]() |
Similar Threads
- Help with binary search tree insertion (C++)
- Binary Search Tree (C++)
- searching and inserting node in a binary search tree (C)
- recursive findaverage function for Binary search tree (C)
- Binary search tree removal (C++)
- Insertion in a binary search tree (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help in files
- Next Thread: plz help(Airline Reservation System)
Views: 3570 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






