fork download
  1. /*
  2.  * <one line to give the library's name and an idea of what it does.>
  3.  * Copyright (C) 2013 <copyright holder> <email>
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Lesser General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2.1 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13.  * Lesser General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Lesser General Public
  16.  * License along with this library; if not, write to the Free Software
  17.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18.  *
  19.  */
  20.  
  21. #pragma once
  22.  
  23. #include "../tree_binary.hpp"
  24. #include "../../error.hpp"
  25.  
  26. namespace tree {
  27.  
  28. data_t& BinaryTree::find(key_t key) {
  29. if(root_.empty()) {
  30. throw not_found_exception(key);
  31. }
  32.  
  33. element_t* tmp_element = &root_;
  34. do {
  35. if(tmp_element->key() < tmp_element->left()->key()) {
  36. tmp_element = tmp_element->left();
  37. } else {
  38. tmp_element = tmp_element->right();
  39. }
  40. } while(tmp_element->is_not_leaf());
  41.  
  42. if(tmp_element->key() == key) {
  43. return tmp_element->data();
  44. } else {
  45. throw not_found_exception(key);
  46. }
  47. }
  48.  
  49. bool BinaryTree::insert(key_t key, data_t& data) noexcept {
  50. if(root_.empty()) {
  51. root_.set_key(key);
  52. root_.set_data(data);
  53. return true;
  54. }
  55.  
  56. element_t* tmp_element = &root_;
  57. while(tmp_element->is_not_leaf()) {
  58. if(tmp_element->key() < key) {
  59. tmp_element = tmp_element->left();
  60. } else {
  61. tmp_element = tmp_element->right();
  62. }
  63. }
  64.  
  65. if(tmp_element->key() == key) {
  66. return false;
  67. }
  68.  
  69. element_t* old_element = new element_t(*tmp_element);
  70. element_t* new_element = new element_t(key, data);
  71.  
  72. if(tmp_element->key() < key) {
  73. tmp_element->set_left(old_element);
  74. tmp_element->set_right(new_element);
  75. tmp_element->set_key(key);
  76. } else {
  77. tmp_element->set_left(new_element);
  78. tmp_element->set_right(old_element);
  79. }
  80.  
  81. return true;
  82. }
  83.  
  84. } // end tree namespace
  85.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:21:9: warning: #pragma once in main file [enabled by default]
 #pragma once
         ^
prog.cpp:23:30: fatal error: ../tree_binary.hpp: No such file or directory
 #include "../tree_binary.hpp"
                              ^
compilation terminated.
stdout
Standard output is empty