-2

I want to create a Binary search tree which has special Nodes. There should be three classes of Nodes, Internal Node, External Node and Root node, each inheriting a common parent Node and each type of node will have some unique function. Is it possible to create such a BST. The problem I am facing is suppose the first node I insert into the tree becomes the root node. The next node I insert will become External Node. Now if I insert another node then the external node has to become a internal node and the new node will become the external node. Also I cannot find a way to navigate through the tree from one node to another as the nodes will be of different types. Can a tree of this type be created. If yes then give me some suggestions of how this can be done.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Lengdon
  • 79
  • 7
  • 1
    Can you show the program you've already written, and explain how exactly your program doesn't work or doesn't produce the expected results? You have to show your work first, and it must be a good-faith real attempt to implement your program and not a few token lines of code, before asking for help on stackoverflow.com. We don't write entire programs for other people, here. For more information, see [ask] questions, take the [tour], and read the [help]. – Sam Varshavchik Feb 15 '20 at 18:38
  • Wait, I think I understand his/her point. Lengdon: Please do post what you have so far. – einpoklum Feb 15 '20 at 18:39
  • So you are saying that all leaf nodes are external nodes, and any other nodes are internal (except for the root)? – PaulMcKenzie Feb 15 '20 at 18:41
  • Yes other nodes are internal. Except the root and external(leaf) nodes all are internal – Lengdon Feb 15 '20 at 18:57
  • Sam Varshavchik I never said I wanted codes for the problem. All I wanted was some suggestions in plain English and not codes. Anyways, einpoklum answered my question. – Lengdon Feb 15 '20 at 19:02
  • @Lengdon: Next time it's best to clarify that you're not interested in seeing code - because most "Can I do X in language Y?" do want to see code in answers. – einpoklum Feb 15 '20 at 19:13

2 Answers2

2

If I understand correctly, you're worried about how objects in one class - External - need to become objects of another class - Internal. This, when C++ is a statically-typed language: Types (including the classes of objects) are determined at compile-time. Right?

Well, you can achieve this in at least one of two ways:

  1. When an External node becomes Internal, delete the External node and replace it with an Internal node, properly initialized (e.g. to point at the new External node).
  2. Give up on External and Internal being discrete types, and just check for children and parents to determine the node type dynamically.

Some more relevant reading material on these matters:

einpoklum
  • 118,144
  • 57
  • 340
  • 684
1

You could use basic inheritance some type enum and recursive calls. This could be a starting point:

enum NodeType
{
    eRoot,
    eInternal,
    eExternal
};

class BinaryNode
{
public:
   virtual NodeType GetType() = 0;
   virtual void UpdateTree() = 0;

protected:
   BinaryNode* ChildLeft;
   BinaryNode* ChildRight;
   BinaryNode* Parent;
};

class ExternalNode : public BinaryNode 
{
   NodeType GetType() override { return eExternal; }
   void UpdateTree() override 
   { 
      //... Replace node instances here(e.g. delete this node and construct the correct new one given this sub tree. call new InternalNode(this) for example) 
      // Call this towards the parent node so the tree will be transformed accordingly
   }
}
class InternalNode : public BinaryNode 
{ 
   NodeType GetType() override { return eInternal; }
   void UpdateTree() override { //... }
}
class RootNode : public BinaryNode 
{ 
   NodeType GetType() override { return eRoot; }
   void UpdateTree() override { //... }
}

This is just to give you an idea where to start. You can check the node type at runtime via GetType() and do some checks based on that.

Be aware that this kind of transformation is not particularly fast. If you want this to be fast, don't use different types and virtual function calls and pointers.

Place your binary tree inside an array and use binary indexing, so at a given index "n" use 2*n+1 to get the left child and 2*n+2 to get the right child. Then use some flags (root, external, internal etc.) to determine which functions you want to call on the binary node. I wouldn't use inheritance like in my code example to be fast or more readable. In fact, deciding externally what functions to call on a node can be much more readable and less error-prone.

Bilaal Rashid
  • 828
  • 2
  • 13
  • 21
ecreif
  • 1,182
  • 1
  • 12
  • 25