Click here to Skip to main content
15,885,980 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, what Im trying to do is to create a class AVL which will inherite from a BST class. The BST may look like :
C++
class BST
{
private:
    struct Node
    {
        int val;
        Node* left;
        Node* right;
    };
public:
    Node* insert(Node* node) {/* do stuff using BST::Node */}

    // ...
};

So, there will be a AVL class which will inherite from BST. So far we known, AVL Node is different from BST Node, so AVL Node will be like this :
C++
struct Node // AVL
{
    int val, height;
    Node* parent;
    Node* left;
    Node* right;
};

So now, AVL class inherite from BST class, but when I insert in a AVL, all the insertion stuff will still using BST::Node. What I want is that I can use BST as a generic algorithm to do the common stuff(since AVL and BST are very similar). But I dont known how to make BST use AVL::Node to do the common insertion stuff. I'm learning about CRTP but I dont known how to solve the problem in a elegant way. Can someone help ? (I know CRTP would be overkill for this problem, but Im trying to better understand this pattern of problem).

attachment : http://stackoverflow.com/questions/33919516/inheritance-and-avl-bst-trees/33920331#33920331[^]
Posted
Updated 7-Dec-15 14:21pm
v2
Comments
Richard MacCutchan 8-Dec-15 4:35am    
Why not ask at SO where you found the information in the first place?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900