arrays - Remove function for tree c++ -
so need function removes last node of tree. im finding online says use vector instead of array, assignment says use array. im thinking of using 2 arrays not sure how implement it. please have code far, think need remove function. in advance
#include <iostream> #include <string> using namespace std; template <class item> class tree { public: // typedef int value_type; typedef std::size_t size_type; static const size_type capacity = 30; tree() { used = 0; } void leftchild(int index) { if((2*index)+1 > used) { cout <<"no child" << endl; } else cout << "\nleft child of index " << index << ": " << data[(2*index)+1] << endl; } void rightchild (int index) { if((2*index)+2 >= used) { cout <<"no child" << endl; } else cout << "\nright child of index " << index << data[(2*index)+2] << endl; } void parent (int index) { if(((index-1)/2) < 0) { cout << "no parent" << endl; } else cout << "\nparent: " << data[(index-1)/2] <<endl; } void insert(item entry) { data[used] = entry; ++used; } void remove() { } void display() { for(int = 0; < used; i++) { cout << data[i]; } } private: item data[capacity]; size_type used; }; int main() { tree<char> test; test.insert('a'); //index [0] test.insert('l'); //index [1] test.insert('g'); //index [2] test.insert('o'); //index [3] test.insert('r'); //index [4] test.insert('i'); //index [5] test.insert('t'); //index [6] test.insert('h'); //index [7] test.insert('m'); //index [8] test.insert('s'); //index [9] test.display(); //test.remove(); test.display(); test.leftchild(4); system("pause"); return 0; }
simply decrease used 1:
void remove() { if (used > 0) used--; }
also fix leftchild():
if((2*index)+1 >= used)
Comments
Post a Comment