Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. // HEADER FILE
  2. #pragma once
  3.  
  4. class BinaryTree
  5. {
  6.  
  7. private:
  8. struct Node {
  9. int key;
  10. Node *left;
  11. Node *right;
  12. };
  13. Node *root;
  14.  
  15. Node *CreateLeaf(int key);
  16.  
  17. void AddPrivate(int key, Node *ptr);
  18. bool SearchPrivate(int key, Node *ptr);
  19. void PrintPrivate(Node *ptr);
  20.  
  21. public:
  22. BinaryTree();
  23.  
  24. void Add(int key);
  25. void Print();
  26. bool Search(int key);
  27. };
  28.  
  29. // CPP FILE
  30. #include <iostream>
  31.  
  32. #include "BinaryTree.h"
  33.  
  34. using namespace std;
  35.  
  36.  
  37. BinaryTree::BinaryTree()
  38. {
  39. this->root = nullptr;
  40.  
  41. }
  42.  
  43.  
  44. // Creates new leaf with nullpointers and returns it
  45.  
  46. BinaryTree::Node *BinaryTree::CreateLeaf(int key) {
  47. Node *NewNode = new Node;
  48. NewNode->key = key;
  49. NewNode->left = nullptr;
  50. NewNode->right = nullptr;
  51.  
  52. return NewNode;
  53. }
  54.  
  55. // public method to add a number to the tree
  56. // calls private method which does the work
  57.  
  58. void BinaryTree::Add(int key)
  59. {
  60. AddPrivate(key, this->root);
  61. }
  62.  
  63.  
  64. //private method to Add a number to the tree
  65.  
  66. void BinaryTree::AddPrivate(int key, Node *ptr)
  67. {
  68. if (this->root == nullptr)
  69. {
  70. this->root = CreateLeaf(key);
  71. }
  72. else if (key < ptr->key)
  73. {
  74. if (ptr->left != nullptr)
  75. {
  76. AddPrivate(key, ptr->left);
  77. }
  78. else
  79. {
  80. ptr->left = CreateLeaf(key);
  81. }
  82. }
  83. else if (key > ptr->key)
  84. {
  85. if (ptr->right != nullptr)
  86. {
  87. AddPrivate(key, ptr->right);
  88. }
  89. else
  90. {
  91. ptr->right = CreateLeaf(key);
  92. }
  93. }
  94. else
  95. {
  96. cout << key << " has already been added " << endl;
  97. }
  98. }
  99.  
  100. bool BinaryTree::Search(int key)
  101. {
  102. return SearchPrivate(key, this->root);
  103. }
  104.  
  105. bool BinaryTree::SearchPrivate(int key, Node *ptr)
  106. {
  107. if (ptr->key == key)
  108. {
  109. return true;
  110. }
  111. else if (key < ptr->key)
  112. {
  113. if (ptr->left == nullptr)
  114. {
  115. return false;
  116. }
  117. else
  118. {
  119. SearchPrivate(key, ptr->left);
  120. }
  121. }
  122. else if (key > ptr->key)
  123. {
  124. if (ptr->right == nullptr)
  125. {
  126. return false;
  127. }
  128. else
  129. {
  130. SearchPrivate(key, ptr->right);
  131. }
  132. }
  133. }
  134.  
  135. void BinaryTree::Print() {
  136. PrintPrivate(this->root);
  137. }
  138.  
  139. void BinaryTree::PrintPrivate(Node *ptr) {
  140. if (this->root == nullptr) {
  141. return;
  142. }
  143. else {
  144.  
  145. std::cout << ptr->key << std::endl;
  146.  
  147. if (ptr->right) {
  148. PrintPrivate(ptr->right);
  149. }
  150. if (ptr->left) {
  151. PrintPrivate(ptr->left);
  152. }
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement