Binary Search Tree question

Sorry, it’s like 80 lines of code but here I’ll send.

include <iostream>

class Bst { public: double datax; Bst* right, * left;

}; Bst* GetNode(double data) { Bst* dataptr = new Bst(); dataptr->datax = data; dataptr->right = dataptr->left = NULL; return dataptr; } Bst* Insert(Bst* root, double input) { if (root == NULL) { root = GetNode(input); } else if (input <= root->datax) { root->left = Insert(root->left, input); } else { root->right = Insert(root->right, input); } return root; } bool Search(Bst* root, double datay) { if (root == NULL) return false; else if (datay == root->datax) return true; else if (datay <= root->datax) return Search(root->left, datay); else return Search(root->right, datay);

} int main() { Bst* root = NULL; root = Insert(root, 18.1); root = Insert(root, 10.2); root = Insert(root, 12.4); root = Insert(root, 45.454); root = Insert(root, 65.232); //At about this point we have input enough values into the search tree, now were going to write a method to print it //the last part is a boolean, we are checking if true, if so we can determine it is found, as you guessed we are going to use //the function we made called "Search" above to determine the validity of what we are trying to search. int number; std::cout << "Enter the number you want to search" << std::endl; std::cin >> number; if (Search(root, number) == true) { std::cout << "Found" << std::endl; } else { std::cout << "Not found" << std::endl; } }

/r/cpp_questions Thread Parent