c++ - Deleting multiple nodes in a Binary Search Tree -


i'm not quite sure how remove nodes in binary search tree. i've been specified remove student objects grade value of less 50. remove function

bool remove(btnode<value_type>* cnode, value_type& data) {     if(cnode == null)     {         return false;     }      if(data.get_name() > cnode->get_data().get_name())     {         remove(cnode->get_right(), data);     }     else if(data.get_name() < cnode->get_data().get_name())     {         remove(cnode->get_left(), data);     }     else     {         if(cnode->is_leaf())         {             if(root->get_data().get_name() == data.get_name())             {                 root = null;             }             else             {                 if(cnode->is_right_child())                 {                     cnode->get_parent()->set_right(null);                 }                 else                 {                     cnode->get_parent()->set_right(null);                 }             }             delete cnode ;             size--;         }         else if(cnode->has_one_child())         {             if(root->get_data().get_name() == data.get_name())             {                 if(cnode->get_right()!= null)                 {                     cnode->get_right()->set_parent(null);                     root = cnode->get_right();                 }                 else                 {                     cnode->get_left()->set_parent(null);                     root = cnode->get_left();                 }             }             else if(cnode->get_right() != null)             {                 cnode->get_right()->set_parent(cnode->get_parent());                 if(cnode->is_right_child())                 {                     cnode->get_parent()->set_right(cnode->get_right());                 }                 else                 {                     cnode->get_parent()->set_left(cnode->get_left());                 }             }             else             {                 btnode<value_type>* tempnode = find_min(cnode->get_right());                 value_type* tempstudent = new value_type (tempnode->get_data());                 remove(cnode->get_right(), tempnode->get_data());                 cnode->set_data(*tempstudent);             }             return true;         }         return false;     } } 

and recursively calling

bool remove(value_type& data) {     return remove(root, data); } 

the student object i'm trying remove constructed so

student::student(std::string init_name) {     name = init_name;     grade = rand() % 101; } 

and in main function have

bstree<student>* student_tree = new bstree<student>; std::string studentname[]={"adam", "cameron", "jackson", "kisoon", "nicholas", "adrian", "chris", "jacob", "lance", "ryan",                            "alexander", "damian", "james", "liam", "sang", "andrew", "david", "jared", "madison", "shane", "ashley", "dillon",                            "jodi", "magdalena", "simon", "benjamin", "dylan", "jonathan",  "marcus", "thomas", "bradley", "ethan", "joshua", "mark",                            "timothy", "brobie", "frederik", "julius", "melanie", "trent", "callan", "hong", "kelly", "min", "troy", "callum", "hugh", "kenias", "mitchell", "zaanif"};  (int = 49; > 0; i--) {      int j = rand() % (i+1);     std::string tmp = studentname[j];     studentname[j] = studentname[i];     studentname[i] = tmp; }   (int = 0; < 50; i++) {     student student = student(studentname[i]);     student_tree->insert(student, i); }  cout << student_tree->printinorder() << endl; //cout << "test" << endl; cout << "hd's: " << student_tree->hdcount() << endl; cout << "average: " << student_tree->avg() << endl; student_tree->remove(); 

i have no idea pass in parameter remove. assume name of students grade of less 50, i'm not sure on how loop through , grab these names , put them function call.

edit: in remove, should checking name of node , data passed in or grade of node , data passed? i.e. if(data.get_name() > cnode->get_data().get_name()) or if(data.get_grade() > cnode->get_data().get_grade())

it looks function call , parameters okay.

however, looks choosing traverse paths of tree. if want remove nodes contain grade of less 50, have iterate through entire tree, so: return remove(cnode->get_left(), data) || remove(cnode->get_right(), data);

you have base case, have correct: if(cnode == null) return false;

you check if grade of cnode less 50. if so, remove it, , return true.

-i assuming have student data/grade info in each node


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -