Replace a tree node binary search

0

I need to make a replace function (tree node, tree_new node) replace tree with tree_new.

But the tree structure does not have an element to keep which is its father.

The structure is like this:

struct ABB {

   int elem;
   ABB *left, *right;

};

typedef ABB *arbol;

Any ideas on how to face it?

    
asked by Edu 10.04.2018 в 19:57
source

1 answer

0

Nodes are not trees.

I have seen this confusion several times in StackOverflow in Spanish, and I find it very curious that so many users make that mistake.

In the code you have provided, you are doing a pointer to node alias called arbol . And that is as wrong as saying that a step is a ladder, sincerely Do you think the same ?:

  

I need to make a function [...] that replaces tree with tree_new.

Create an object of type arbol :

class arbol {
    ABB *raiz { nullptr };
}

Now you can create a replacement function, which will make a simple exchange of pointers:

class arbol {
    ABB *raiz { nullptr };
public:
    void reemplazo(arbol &a) {
        std::swap(raiz, a.raiz);
    }
};
    
answered by 11.04.2018 в 11:15