Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4.  
  5. using std::string;
  6. using std::cout;
  7. using std::cin;
  8. using std::vector;
  9. using std::endl;
  10. using std::swap;
  11.  
  12.  
  13. class BST {
  14. struct Node {
  15. int value;
  16. Node* parent;
  17. Node* left;
  18. Node* right;
  19. Node(int value, Node* parent = nullptr, Node* left = nullptr, Node* right = nullptr);
  20. };
  21. Node* root;
  22.  
  23. public:
  24. BST();
  25. ~BST();
  26.  
  27. Node* CereateNew(int value);
  28. Node* Insert(int value);
  29. };
  30.  
  31. int main() {
  32.  
  33. }
  34.  
  35. BST::BST()
  36. : root(nullptr)
  37. {}
  38.  
  39. BST::Node *BST::CereateNew(int value) {
  40. Node* new_node = new Node(value);
  41. return new_node;
  42. }
  43.  
  44. BST::Node::Node(int value, BST::Node *parent, BST::Node *left, BST::Node *right)
  45. : value(value)
  46. , parent(parent)
  47. , left(left)
  48. , right(right)
  49. {}
  50.  
  51. BST::Node *BST::Insert(int value) {
  52. if ( root == nullptr ) {
  53. root->value = value;
  54. return nullptr;
  55. }
  56. Node* currNode = root;
  57. bool done=false;
  58.  
  59. while(true){
  60. while(true) {
  61. if(currNode->value < value) {
  62. if (currNode->left == nullptr) {
  63. currNode->left = CereateNew(value);
  64. currNode->left->parent = currNode;
  65. done = true;
  66. break;
  67. } else {
  68. currNode = currNode->left;
  69. break;
  70. }
  71. }
  72. if(currNode->value >= value) {
  73. if (currNode->right == nullptr) {
  74. currNode->right = CereateNew(value);
  75. currNode->right->parent=currNode;
  76. done = true;
  77. break;
  78. } else {
  79. currNode = currNode->right;
  80. }
  81. }
  82. }
  83. if(done) break;
  84. }
  85. }
  86.  
  87.  
  88.  
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement