data structures - Adding to a Binary Tree C++ -
i need in inserting nodes binary tree. program reads .cpp , .h file information on baseball player. information contained within 1 node of binary tree. when 1 node read in need insert function read information , sort accordingly. need basic code me along process. have insert function far:
bool tree::insert(player player) { node *pnew; node *root, *next; pnew = new node(player); if(pnew == null) { return false; } root = proot; next = null; while (root != null && next -> item.lessthan(player)) { root = next; next = next -> pcurrent; if(next < root) { } } }
this full thing main.cpp #include <iostream> #include <fstream> #include <string> #include "binarytree.h" using namespace std; int main(void) { const int file = 100; char input[file]; char output[file]; ifstream infile; ofstream outfile; player currp; tree mytree; int i; cout << "please enter name of input data file: "; cin >> input; infile.open(input); if(infile.fail()) { cout << endl << "we're sorry, file specificed not opened!" << endl; return 0; } while(!infile.eof()) { currp.read(infile); mytree -> proot; mytree.insert(currp); } infile.close(); } player.cpp #include <iostream> #include <string> #include <iomanip> #include "player.h" using namespace std; player::player() { initialize("unknown", "unknown", "tbd", 0.0); } void player::initialize(string first, string last, string pos, double avg) { firstn = first; lastn = last; position = pos; if (avg < 0) avg = -avg; batave = avg; } void player::read(istream &input) { char temp[100]; input >> temp; firstn = temp; input >> temp; lastn = temp; input >> temp; position = temp; input >> batave; } void player::write(ostream &out) { out << lastn << ", " << firstn << ": " << position; out << fixed; out << setprecision(3); out << " (" << batave << ") "; } bool player::lessthan(const player &player) const { bool less; if(lastn.compare(player.lastn) < 0) less = true; else if (lastn.compare(player.lastn) == 0 && firstn.compare(player.firstn) < 0) less = true; else less = false; return less; } player.h #include <iostream> using namespace std; class player { private: string firstn; string lastn; string position; double batave; public: player(); void initialize(string, string, string, double); void read(istream&); void write(ostream&); double getbatave(); string getfirstname(); string getlastname(); string getfullname(); string getposition(); bool lessthan(const player &player) const; bool equals (const player &player) const; }; binarytree.cpp #include <iostream> #include <fstream> #include "binarytree.h" using namespace std; node::node() { } tree::tree() { } bool tree::insert(player player) //key player , leaf parent node { node *leaf; if(player < leaf -> item) { if(leaf -> left != null) { insert(player, leaf -> left); return false; } else { leaf -> left = new node; leaf -> left -> item = player; leaf -> left -> left = null; //sets left child of child node null leaf -> left -> right = null; //sets right child of child node null return true; } } else if(player > leaf -> item) { if(leaf -> right != null) { insert(player, leaf -> right); return false; } else { leaf -> right = new node; leaf -> right -> item = player; leaf -> right -> left = null; //sets left child of child node null leaf -> right -> right = null; //sets right child of child node null return true; } } } tree::~tree() { } binarytree.h #include <iostream> #include <fstream> #include <string> #include <iomanip> #include "player.h" using namespace std; class node { public: node(); player item; // storage space player node *left; // pointer left node *right; // pointer right node *pcurrent; node *leaf; node(const player &player) // used copy item newly created nodes our list { item = player; left = null; // set link valid non-node value right = null; leaf = null; } }; class tree { private: int length; // current length of list (number of items stored) node *proot; // node created put root value node *pl; // node created put value less root value node *pr; // node created put value greater root value public: tree(); // constructor ~tree(); // destructor bool insert(player player); // adds new player tree void free(); // cleans tree , free of nodes in tree void count(); // returns count of players in tree void display(); // displays player records, in appropriate order. void updateavg(); // updates particular player's batting average void find(); // finds player name }; need have these: default constructor , destructor insert - add new player tree free - clean tree , free of nodes in tree count - return count of players in tree display - display player records, in appropriate order. updateavg - update particular player's batting average find - find player name input file this: chipper jones 3b 0.303 rafael furcal ss 0.281 hank aaron rf 0.305 andrew jones cf 0.322 mckenna wilson g3 0.245 finnigen rengen 5f 0.245 landen savage 2f 0.252 raschoie sacoire 4f 0.632 joughne seong h3 0.744 halmens flamens g3 0.245 enter code here
Comments
Post a Comment