inheritance - Using Inheritence efficiently in C++ -


below code have written in inserting node simple binary search tree. trying implement red black tree inheriting same node class rbnode class.

void node::insert_node(tree *t) {     node *cur_node = t->get_root();     node *prev_node;     while(null != cur_node)     {         prev_node = cur_node;         if(this->data < cur_node->data)         {             cur_node = cur_node->left;         }         else         {             cur_node = cur_node->right;         }     }       if(null == t->get_root())     {         cur_node = this;         t->set_root(cur_node);     }     else     {         if(this->data < prev_node->data)         {             prev_node->left = this;         }         else         {             prev_node->right = this;         }         this->parent = prev_node;     } } 

this function remain same rbnode, except node* should replaced rbnode* , tree* replaced rbtree*. think it's futile write same function in rbnode class exact same thing. if use same function, can't access rbnode's members, since have inserted tree node.

what effiecient way achieve . new c++, if have missed obvious, please let me know.

you don't need use inheritance @ can. solution without inheritance use templates. follows:

template <class n, class t> void insert_node(n *node, t *tree); 

this code work both kinds of nodes. problem has located either globally or in third unrelated class. can have node inherit abstract class inode , tree inherit abstract class itree. function located in inode. derived nodes , trees have functionality unique them.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -