Creating a unified search function for a binary search tree












1















I'm trying to create a search function in a binary search tree that can be used by both the insert and search functions.



I tried passing my cursor as a reference



template<class key_type, class data_type>
bool binary_tree<key_type, data_type>::internal_search(node *& cursor, key_type query) {
if (cursor == NULL) {
return false;
}else if (cursor->key == query) {
return true;
}
if (cursor->key < query) {
internal_search(cursor->left, query);
}
else {
internal_search(cursor->right, query);
}
}


Here is the insert function I'm trying to use it in



template<class key_type, class data_type>
void binary_tree<key_type, data_type>::insert(key_type key_in, data_type data_in) {
node * local_cursor = start;
if (!internal_search(local_cursor, key_in)) {
local_cursor = new node;
local_cursor->key = key_in;
local_cursor->data = data_in;
local_cursor->left = NULL;
local_cursor->right = NULL;
size++;
}
else {
std::cout << "entry already present" << std::endl;
}
}


Here is the search function I'm trying to use it in



template<class key_type, class data_type>
data_type binary_tree<key_type, data_type>::search(key_type query) {
node * local_cursor = start;
if (internal_search(local_cursor, query)) {
return local_cursor->data;
}
std::cout << "search query not found" << std::endl;
}


Neither passing through as a reference or returning as a value have worked



I don't understand why when I run this code the start pointer is always NULL when inserting a new value into the binary search tree.



I also tried rewriting the code with the internal_search function returning a node pointer but that didn't work either.



Why does start point to NULL everytime instead of the new node I assigned it to?



here's the header if that might help



#pragma once

template <class key_type, class data_type>
class binary_tree
{
private:
struct node {
key_type key;
data_type data;
node * left;
node * right;
};
node * start;
int size;

bool internal_search(node *, key_type);

void print_preorder(node * cursor = start);
void file_preorder( std::ofstream&, node *);

void file_inorder(std::ofstream&, node *);
void print_inorder_pri(node *);

void print_postorder(node *);
void file_postorder(std::ofstream&, node *);

public:
binary_tree();
void insert(key_type);
void remove();
bool is_empty();
data_type search(key_type);
void print_preorder();
void file_preorder(std::ofstream&);
void print_inorder();
void file_inorder(std::ofstream&);
void print_postorder();
void file_postorder(std::ofstream&);
void print_level();
bool load_file(std::string);
void save_file(std::string);
~binary_tree();
};









share|improve this question

























  • Please, debug this step-wise for a small sample to find out what is going wrong where. Please, watch the warnings your compiler is probably emitting (or raise the warning level to get warnings). When I started with looking at binary_tree<key_type, data_type>::internal_search(), I realized that not every code path ends in return. (At this point, I stopped looking and started complaining. ;-)) If I compile such code in VS2013 (with my usual project settings) I get an error (and even cannot run that code).

    – Scheff
    Nov 16 '18 at 7:03













  • With gcc 7.3.0 (++17), I got other compilation errors. Moreover, some function implementations are missing, not only main()

    – Damien
    Nov 16 '18 at 13:33
















1















I'm trying to create a search function in a binary search tree that can be used by both the insert and search functions.



I tried passing my cursor as a reference



template<class key_type, class data_type>
bool binary_tree<key_type, data_type>::internal_search(node *& cursor, key_type query) {
if (cursor == NULL) {
return false;
}else if (cursor->key == query) {
return true;
}
if (cursor->key < query) {
internal_search(cursor->left, query);
}
else {
internal_search(cursor->right, query);
}
}


Here is the insert function I'm trying to use it in



template<class key_type, class data_type>
void binary_tree<key_type, data_type>::insert(key_type key_in, data_type data_in) {
node * local_cursor = start;
if (!internal_search(local_cursor, key_in)) {
local_cursor = new node;
local_cursor->key = key_in;
local_cursor->data = data_in;
local_cursor->left = NULL;
local_cursor->right = NULL;
size++;
}
else {
std::cout << "entry already present" << std::endl;
}
}


Here is the search function I'm trying to use it in



template<class key_type, class data_type>
data_type binary_tree<key_type, data_type>::search(key_type query) {
node * local_cursor = start;
if (internal_search(local_cursor, query)) {
return local_cursor->data;
}
std::cout << "search query not found" << std::endl;
}


Neither passing through as a reference or returning as a value have worked



I don't understand why when I run this code the start pointer is always NULL when inserting a new value into the binary search tree.



I also tried rewriting the code with the internal_search function returning a node pointer but that didn't work either.



Why does start point to NULL everytime instead of the new node I assigned it to?



here's the header if that might help



#pragma once

template <class key_type, class data_type>
class binary_tree
{
private:
struct node {
key_type key;
data_type data;
node * left;
node * right;
};
node * start;
int size;

bool internal_search(node *, key_type);

void print_preorder(node * cursor = start);
void file_preorder( std::ofstream&, node *);

void file_inorder(std::ofstream&, node *);
void print_inorder_pri(node *);

void print_postorder(node *);
void file_postorder(std::ofstream&, node *);

public:
binary_tree();
void insert(key_type);
void remove();
bool is_empty();
data_type search(key_type);
void print_preorder();
void file_preorder(std::ofstream&);
void print_inorder();
void file_inorder(std::ofstream&);
void print_postorder();
void file_postorder(std::ofstream&);
void print_level();
bool load_file(std::string);
void save_file(std::string);
~binary_tree();
};









share|improve this question

























  • Please, debug this step-wise for a small sample to find out what is going wrong where. Please, watch the warnings your compiler is probably emitting (or raise the warning level to get warnings). When I started with looking at binary_tree<key_type, data_type>::internal_search(), I realized that not every code path ends in return. (At this point, I stopped looking and started complaining. ;-)) If I compile such code in VS2013 (with my usual project settings) I get an error (and even cannot run that code).

    – Scheff
    Nov 16 '18 at 7:03













  • With gcc 7.3.0 (++17), I got other compilation errors. Moreover, some function implementations are missing, not only main()

    – Damien
    Nov 16 '18 at 13:33














1












1








1








I'm trying to create a search function in a binary search tree that can be used by both the insert and search functions.



I tried passing my cursor as a reference



template<class key_type, class data_type>
bool binary_tree<key_type, data_type>::internal_search(node *& cursor, key_type query) {
if (cursor == NULL) {
return false;
}else if (cursor->key == query) {
return true;
}
if (cursor->key < query) {
internal_search(cursor->left, query);
}
else {
internal_search(cursor->right, query);
}
}


Here is the insert function I'm trying to use it in



template<class key_type, class data_type>
void binary_tree<key_type, data_type>::insert(key_type key_in, data_type data_in) {
node * local_cursor = start;
if (!internal_search(local_cursor, key_in)) {
local_cursor = new node;
local_cursor->key = key_in;
local_cursor->data = data_in;
local_cursor->left = NULL;
local_cursor->right = NULL;
size++;
}
else {
std::cout << "entry already present" << std::endl;
}
}


Here is the search function I'm trying to use it in



template<class key_type, class data_type>
data_type binary_tree<key_type, data_type>::search(key_type query) {
node * local_cursor = start;
if (internal_search(local_cursor, query)) {
return local_cursor->data;
}
std::cout << "search query not found" << std::endl;
}


Neither passing through as a reference or returning as a value have worked



I don't understand why when I run this code the start pointer is always NULL when inserting a new value into the binary search tree.



I also tried rewriting the code with the internal_search function returning a node pointer but that didn't work either.



Why does start point to NULL everytime instead of the new node I assigned it to?



here's the header if that might help



#pragma once

template <class key_type, class data_type>
class binary_tree
{
private:
struct node {
key_type key;
data_type data;
node * left;
node * right;
};
node * start;
int size;

bool internal_search(node *, key_type);

void print_preorder(node * cursor = start);
void file_preorder( std::ofstream&, node *);

void file_inorder(std::ofstream&, node *);
void print_inorder_pri(node *);

void print_postorder(node *);
void file_postorder(std::ofstream&, node *);

public:
binary_tree();
void insert(key_type);
void remove();
bool is_empty();
data_type search(key_type);
void print_preorder();
void file_preorder(std::ofstream&);
void print_inorder();
void file_inorder(std::ofstream&);
void print_postorder();
void file_postorder(std::ofstream&);
void print_level();
bool load_file(std::string);
void save_file(std::string);
~binary_tree();
};









share|improve this question
















I'm trying to create a search function in a binary search tree that can be used by both the insert and search functions.



I tried passing my cursor as a reference



template<class key_type, class data_type>
bool binary_tree<key_type, data_type>::internal_search(node *& cursor, key_type query) {
if (cursor == NULL) {
return false;
}else if (cursor->key == query) {
return true;
}
if (cursor->key < query) {
internal_search(cursor->left, query);
}
else {
internal_search(cursor->right, query);
}
}


Here is the insert function I'm trying to use it in



template<class key_type, class data_type>
void binary_tree<key_type, data_type>::insert(key_type key_in, data_type data_in) {
node * local_cursor = start;
if (!internal_search(local_cursor, key_in)) {
local_cursor = new node;
local_cursor->key = key_in;
local_cursor->data = data_in;
local_cursor->left = NULL;
local_cursor->right = NULL;
size++;
}
else {
std::cout << "entry already present" << std::endl;
}
}


Here is the search function I'm trying to use it in



template<class key_type, class data_type>
data_type binary_tree<key_type, data_type>::search(key_type query) {
node * local_cursor = start;
if (internal_search(local_cursor, query)) {
return local_cursor->data;
}
std::cout << "search query not found" << std::endl;
}


Neither passing through as a reference or returning as a value have worked



I don't understand why when I run this code the start pointer is always NULL when inserting a new value into the binary search tree.



I also tried rewriting the code with the internal_search function returning a node pointer but that didn't work either.



Why does start point to NULL everytime instead of the new node I assigned it to?



here's the header if that might help



#pragma once

template <class key_type, class data_type>
class binary_tree
{
private:
struct node {
key_type key;
data_type data;
node * left;
node * right;
};
node * start;
int size;

bool internal_search(node *, key_type);

void print_preorder(node * cursor = start);
void file_preorder( std::ofstream&, node *);

void file_inorder(std::ofstream&, node *);
void print_inorder_pri(node *);

void print_postorder(node *);
void file_postorder(std::ofstream&, node *);

public:
binary_tree();
void insert(key_type);
void remove();
bool is_empty();
data_type search(key_type);
void print_preorder();
void file_preorder(std::ofstream&);
void print_inorder();
void file_inorder(std::ofstream&);
void print_postorder();
void file_postorder(std::ofstream&);
void print_level();
bool load_file(std::string);
void save_file(std::string);
~binary_tree();
};






c++ search binary-tree binary-search-tree






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 5:56









marc_s

574k12811091256




574k12811091256










asked Nov 16 '18 at 3:29









Abdelsalam Mohamed El-TamawyAbdelsalam Mohamed El-Tamawy

113




113













  • Please, debug this step-wise for a small sample to find out what is going wrong where. Please, watch the warnings your compiler is probably emitting (or raise the warning level to get warnings). When I started with looking at binary_tree<key_type, data_type>::internal_search(), I realized that not every code path ends in return. (At this point, I stopped looking and started complaining. ;-)) If I compile such code in VS2013 (with my usual project settings) I get an error (and even cannot run that code).

    – Scheff
    Nov 16 '18 at 7:03













  • With gcc 7.3.0 (++17), I got other compilation errors. Moreover, some function implementations are missing, not only main()

    – Damien
    Nov 16 '18 at 13:33



















  • Please, debug this step-wise for a small sample to find out what is going wrong where. Please, watch the warnings your compiler is probably emitting (or raise the warning level to get warnings). When I started with looking at binary_tree<key_type, data_type>::internal_search(), I realized that not every code path ends in return. (At this point, I stopped looking and started complaining. ;-)) If I compile such code in VS2013 (with my usual project settings) I get an error (and even cannot run that code).

    – Scheff
    Nov 16 '18 at 7:03













  • With gcc 7.3.0 (++17), I got other compilation errors. Moreover, some function implementations are missing, not only main()

    – Damien
    Nov 16 '18 at 13:33

















Please, debug this step-wise for a small sample to find out what is going wrong where. Please, watch the warnings your compiler is probably emitting (or raise the warning level to get warnings). When I started with looking at binary_tree<key_type, data_type>::internal_search(), I realized that not every code path ends in return. (At this point, I stopped looking and started complaining. ;-)) If I compile such code in VS2013 (with my usual project settings) I get an error (and even cannot run that code).

– Scheff
Nov 16 '18 at 7:03







Please, debug this step-wise for a small sample to find out what is going wrong where. Please, watch the warnings your compiler is probably emitting (or raise the warning level to get warnings). When I started with looking at binary_tree<key_type, data_type>::internal_search(), I realized that not every code path ends in return. (At this point, I stopped looking and started complaining. ;-)) If I compile such code in VS2013 (with my usual project settings) I get an error (and even cannot run that code).

– Scheff
Nov 16 '18 at 7:03















With gcc 7.3.0 (++17), I got other compilation errors. Moreover, some function implementations are missing, not only main()

– Damien
Nov 16 '18 at 13:33





With gcc 7.3.0 (++17), I got other compilation errors. Moreover, some function implementations are missing, not only main()

– Damien
Nov 16 '18 at 13:33












1 Answer
1






active

oldest

votes


















1














After some trivial modifications (including the one related to @Scheff's comment), I got it compiled.



However, start was effectively always equal to NULL.
I discovered that the problem was that ìnternal_search was always returning NULL, i.e.
the value of the node* before node creation and not the address of node* where to create the new node. Therefore, it was needed to replace (node* &) by (node** &).



Here is the code that seems to work, (with a main() for test) at least for the simple test searchthat was causing problem to the PO. Some work must be done to improve (e.g. a recursive insert) and complete the code (e.g. to delete the object binary_tree) but this is out of the scope of the question (fortunately!).



#include    <iostream>

template <class key_type, class data_type>
class binary_tree
{
private:
struct node {
key_type key;
data_type data;
node* left = NULL;
node* right = NULL;
};
node* start = NULL;
int size = 0;

bool internal_search(node** &cursor, key_type);

//void print_preorder(node * cursor = start);
//void file_preorder( std::ofstream&, node *);

void file_inorder(std::ofstream&, node *);
void print_inorder_pri(node *);

void print_postorder(node *);
void file_postorder(std::ofstream&, node *);

public:
binary_tree() {};
void insert(key_type, data_type);
void remove();
bool is_empty();
data_type search(key_type);
//void print_preorder();
void file_preorder(std::ofstream&);
void print_inorder();
void file_inorder(std::ofstream&);
void print_postorder();
void file_postorder(std::ofstream&);
void print_level();
bool load_file(std::string);
void save_file(std::string);

void print_start () {std::cout << start << "n";} // Added

//~binary_tree();
};

template<class key_type, class data_type>
bool binary_tree<key_type, data_type>::internal_search (node** &cursor, key_type query) {
if (*cursor == NULL) {
return false;
} else if ((*cursor)->key == query) {
return true;
}
if ((*cursor)->key < query) {
cursor = &((*cursor)->left);
return internal_search(cursor, query);
} else {
cursor = &((*cursor)->right);
return internal_search(cursor, query);
}
}

template<class key_type, class data_type>
void binary_tree<key_type, data_type>::insert(key_type key_in, data_type data_in) {
node** local_cursor = &start;
if (!internal_search(local_cursor, key_in)) {
*local_cursor = new node;
(*local_cursor)->key = key_in;
(*local_cursor)->data = data_in;
size++;
}
else {
std::cout << "entry already present" << std::endl;
}
}

template<class key_type, class data_type>
data_type binary_tree<key_type, data_type>::search(key_type query) {
node** local_cursor = &start;
if (internal_search(local_cursor, query)) {
return (*local_cursor)->data;
}
std::cout << "search query not found" << std::endl;
return 0;
}

int main() {
binary_tree<int,int> tree;
tree.insert (0,0);
tree.insert (2,3);
tree.insert (-2,3);
tree.insert (-1,-1);
std::cout << "start = ";
tree.print_start();

std::cout << tree.search(2) << "n";
return 0;
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331001%2fcreating-a-unified-search-function-for-a-binary-search-tree%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    After some trivial modifications (including the one related to @Scheff's comment), I got it compiled.



    However, start was effectively always equal to NULL.
    I discovered that the problem was that ìnternal_search was always returning NULL, i.e.
    the value of the node* before node creation and not the address of node* where to create the new node. Therefore, it was needed to replace (node* &) by (node** &).



    Here is the code that seems to work, (with a main() for test) at least for the simple test searchthat was causing problem to the PO. Some work must be done to improve (e.g. a recursive insert) and complete the code (e.g. to delete the object binary_tree) but this is out of the scope of the question (fortunately!).



    #include    <iostream>

    template <class key_type, class data_type>
    class binary_tree
    {
    private:
    struct node {
    key_type key;
    data_type data;
    node* left = NULL;
    node* right = NULL;
    };
    node* start = NULL;
    int size = 0;

    bool internal_search(node** &cursor, key_type);

    //void print_preorder(node * cursor = start);
    //void file_preorder( std::ofstream&, node *);

    void file_inorder(std::ofstream&, node *);
    void print_inorder_pri(node *);

    void print_postorder(node *);
    void file_postorder(std::ofstream&, node *);

    public:
    binary_tree() {};
    void insert(key_type, data_type);
    void remove();
    bool is_empty();
    data_type search(key_type);
    //void print_preorder();
    void file_preorder(std::ofstream&);
    void print_inorder();
    void file_inorder(std::ofstream&);
    void print_postorder();
    void file_postorder(std::ofstream&);
    void print_level();
    bool load_file(std::string);
    void save_file(std::string);

    void print_start () {std::cout << start << "n";} // Added

    //~binary_tree();
    };

    template<class key_type, class data_type>
    bool binary_tree<key_type, data_type>::internal_search (node** &cursor, key_type query) {
    if (*cursor == NULL) {
    return false;
    } else if ((*cursor)->key == query) {
    return true;
    }
    if ((*cursor)->key < query) {
    cursor = &((*cursor)->left);
    return internal_search(cursor, query);
    } else {
    cursor = &((*cursor)->right);
    return internal_search(cursor, query);
    }
    }

    template<class key_type, class data_type>
    void binary_tree<key_type, data_type>::insert(key_type key_in, data_type data_in) {
    node** local_cursor = &start;
    if (!internal_search(local_cursor, key_in)) {
    *local_cursor = new node;
    (*local_cursor)->key = key_in;
    (*local_cursor)->data = data_in;
    size++;
    }
    else {
    std::cout << "entry already present" << std::endl;
    }
    }

    template<class key_type, class data_type>
    data_type binary_tree<key_type, data_type>::search(key_type query) {
    node** local_cursor = &start;
    if (internal_search(local_cursor, query)) {
    return (*local_cursor)->data;
    }
    std::cout << "search query not found" << std::endl;
    return 0;
    }

    int main() {
    binary_tree<int,int> tree;
    tree.insert (0,0);
    tree.insert (2,3);
    tree.insert (-2,3);
    tree.insert (-1,-1);
    std::cout << "start = ";
    tree.print_start();

    std::cout << tree.search(2) << "n";
    return 0;
    }





    share|improve this answer






























      1














      After some trivial modifications (including the one related to @Scheff's comment), I got it compiled.



      However, start was effectively always equal to NULL.
      I discovered that the problem was that ìnternal_search was always returning NULL, i.e.
      the value of the node* before node creation and not the address of node* where to create the new node. Therefore, it was needed to replace (node* &) by (node** &).



      Here is the code that seems to work, (with a main() for test) at least for the simple test searchthat was causing problem to the PO. Some work must be done to improve (e.g. a recursive insert) and complete the code (e.g. to delete the object binary_tree) but this is out of the scope of the question (fortunately!).



      #include    <iostream>

      template <class key_type, class data_type>
      class binary_tree
      {
      private:
      struct node {
      key_type key;
      data_type data;
      node* left = NULL;
      node* right = NULL;
      };
      node* start = NULL;
      int size = 0;

      bool internal_search(node** &cursor, key_type);

      //void print_preorder(node * cursor = start);
      //void file_preorder( std::ofstream&, node *);

      void file_inorder(std::ofstream&, node *);
      void print_inorder_pri(node *);

      void print_postorder(node *);
      void file_postorder(std::ofstream&, node *);

      public:
      binary_tree() {};
      void insert(key_type, data_type);
      void remove();
      bool is_empty();
      data_type search(key_type);
      //void print_preorder();
      void file_preorder(std::ofstream&);
      void print_inorder();
      void file_inorder(std::ofstream&);
      void print_postorder();
      void file_postorder(std::ofstream&);
      void print_level();
      bool load_file(std::string);
      void save_file(std::string);

      void print_start () {std::cout << start << "n";} // Added

      //~binary_tree();
      };

      template<class key_type, class data_type>
      bool binary_tree<key_type, data_type>::internal_search (node** &cursor, key_type query) {
      if (*cursor == NULL) {
      return false;
      } else if ((*cursor)->key == query) {
      return true;
      }
      if ((*cursor)->key < query) {
      cursor = &((*cursor)->left);
      return internal_search(cursor, query);
      } else {
      cursor = &((*cursor)->right);
      return internal_search(cursor, query);
      }
      }

      template<class key_type, class data_type>
      void binary_tree<key_type, data_type>::insert(key_type key_in, data_type data_in) {
      node** local_cursor = &start;
      if (!internal_search(local_cursor, key_in)) {
      *local_cursor = new node;
      (*local_cursor)->key = key_in;
      (*local_cursor)->data = data_in;
      size++;
      }
      else {
      std::cout << "entry already present" << std::endl;
      }
      }

      template<class key_type, class data_type>
      data_type binary_tree<key_type, data_type>::search(key_type query) {
      node** local_cursor = &start;
      if (internal_search(local_cursor, query)) {
      return (*local_cursor)->data;
      }
      std::cout << "search query not found" << std::endl;
      return 0;
      }

      int main() {
      binary_tree<int,int> tree;
      tree.insert (0,0);
      tree.insert (2,3);
      tree.insert (-2,3);
      tree.insert (-1,-1);
      std::cout << "start = ";
      tree.print_start();

      std::cout << tree.search(2) << "n";
      return 0;
      }





      share|improve this answer




























        1












        1








        1







        After some trivial modifications (including the one related to @Scheff's comment), I got it compiled.



        However, start was effectively always equal to NULL.
        I discovered that the problem was that ìnternal_search was always returning NULL, i.e.
        the value of the node* before node creation and not the address of node* where to create the new node. Therefore, it was needed to replace (node* &) by (node** &).



        Here is the code that seems to work, (with a main() for test) at least for the simple test searchthat was causing problem to the PO. Some work must be done to improve (e.g. a recursive insert) and complete the code (e.g. to delete the object binary_tree) but this is out of the scope of the question (fortunately!).



        #include    <iostream>

        template <class key_type, class data_type>
        class binary_tree
        {
        private:
        struct node {
        key_type key;
        data_type data;
        node* left = NULL;
        node* right = NULL;
        };
        node* start = NULL;
        int size = 0;

        bool internal_search(node** &cursor, key_type);

        //void print_preorder(node * cursor = start);
        //void file_preorder( std::ofstream&, node *);

        void file_inorder(std::ofstream&, node *);
        void print_inorder_pri(node *);

        void print_postorder(node *);
        void file_postorder(std::ofstream&, node *);

        public:
        binary_tree() {};
        void insert(key_type, data_type);
        void remove();
        bool is_empty();
        data_type search(key_type);
        //void print_preorder();
        void file_preorder(std::ofstream&);
        void print_inorder();
        void file_inorder(std::ofstream&);
        void print_postorder();
        void file_postorder(std::ofstream&);
        void print_level();
        bool load_file(std::string);
        void save_file(std::string);

        void print_start () {std::cout << start << "n";} // Added

        //~binary_tree();
        };

        template<class key_type, class data_type>
        bool binary_tree<key_type, data_type>::internal_search (node** &cursor, key_type query) {
        if (*cursor == NULL) {
        return false;
        } else if ((*cursor)->key == query) {
        return true;
        }
        if ((*cursor)->key < query) {
        cursor = &((*cursor)->left);
        return internal_search(cursor, query);
        } else {
        cursor = &((*cursor)->right);
        return internal_search(cursor, query);
        }
        }

        template<class key_type, class data_type>
        void binary_tree<key_type, data_type>::insert(key_type key_in, data_type data_in) {
        node** local_cursor = &start;
        if (!internal_search(local_cursor, key_in)) {
        *local_cursor = new node;
        (*local_cursor)->key = key_in;
        (*local_cursor)->data = data_in;
        size++;
        }
        else {
        std::cout << "entry already present" << std::endl;
        }
        }

        template<class key_type, class data_type>
        data_type binary_tree<key_type, data_type>::search(key_type query) {
        node** local_cursor = &start;
        if (internal_search(local_cursor, query)) {
        return (*local_cursor)->data;
        }
        std::cout << "search query not found" << std::endl;
        return 0;
        }

        int main() {
        binary_tree<int,int> tree;
        tree.insert (0,0);
        tree.insert (2,3);
        tree.insert (-2,3);
        tree.insert (-1,-1);
        std::cout << "start = ";
        tree.print_start();

        std::cout << tree.search(2) << "n";
        return 0;
        }





        share|improve this answer















        After some trivial modifications (including the one related to @Scheff's comment), I got it compiled.



        However, start was effectively always equal to NULL.
        I discovered that the problem was that ìnternal_search was always returning NULL, i.e.
        the value of the node* before node creation and not the address of node* where to create the new node. Therefore, it was needed to replace (node* &) by (node** &).



        Here is the code that seems to work, (with a main() for test) at least for the simple test searchthat was causing problem to the PO. Some work must be done to improve (e.g. a recursive insert) and complete the code (e.g. to delete the object binary_tree) but this is out of the scope of the question (fortunately!).



        #include    <iostream>

        template <class key_type, class data_type>
        class binary_tree
        {
        private:
        struct node {
        key_type key;
        data_type data;
        node* left = NULL;
        node* right = NULL;
        };
        node* start = NULL;
        int size = 0;

        bool internal_search(node** &cursor, key_type);

        //void print_preorder(node * cursor = start);
        //void file_preorder( std::ofstream&, node *);

        void file_inorder(std::ofstream&, node *);
        void print_inorder_pri(node *);

        void print_postorder(node *);
        void file_postorder(std::ofstream&, node *);

        public:
        binary_tree() {};
        void insert(key_type, data_type);
        void remove();
        bool is_empty();
        data_type search(key_type);
        //void print_preorder();
        void file_preorder(std::ofstream&);
        void print_inorder();
        void file_inorder(std::ofstream&);
        void print_postorder();
        void file_postorder(std::ofstream&);
        void print_level();
        bool load_file(std::string);
        void save_file(std::string);

        void print_start () {std::cout << start << "n";} // Added

        //~binary_tree();
        };

        template<class key_type, class data_type>
        bool binary_tree<key_type, data_type>::internal_search (node** &cursor, key_type query) {
        if (*cursor == NULL) {
        return false;
        } else if ((*cursor)->key == query) {
        return true;
        }
        if ((*cursor)->key < query) {
        cursor = &((*cursor)->left);
        return internal_search(cursor, query);
        } else {
        cursor = &((*cursor)->right);
        return internal_search(cursor, query);
        }
        }

        template<class key_type, class data_type>
        void binary_tree<key_type, data_type>::insert(key_type key_in, data_type data_in) {
        node** local_cursor = &start;
        if (!internal_search(local_cursor, key_in)) {
        *local_cursor = new node;
        (*local_cursor)->key = key_in;
        (*local_cursor)->data = data_in;
        size++;
        }
        else {
        std::cout << "entry already present" << std::endl;
        }
        }

        template<class key_type, class data_type>
        data_type binary_tree<key_type, data_type>::search(key_type query) {
        node** local_cursor = &start;
        if (internal_search(local_cursor, query)) {
        return (*local_cursor)->data;
        }
        std::cout << "search query not found" << std::endl;
        return 0;
        }

        int main() {
        binary_tree<int,int> tree;
        tree.insert (0,0);
        tree.insert (2,3);
        tree.insert (-2,3);
        tree.insert (-1,-1);
        std::cout << "start = ";
        tree.print_start();

        std::cout << tree.search(2) << "n";
        return 0;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 16:07

























        answered Nov 16 '18 at 15:29









        DamienDamien

        68725




        68725






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331001%2fcreating-a-unified-search-function-for-a-binary-search-tree%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini