Advertisement
RobertDeMilo

All are here

Dec 12th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. struct Node
  8. {
  9.     int data;
  10.     Node* left;
  11.     Node* right;
  12. };
  13.  
  14. Node* GetNewNode(int data)
  15. {
  16.     Node* newNode = new Node();
  17.     newNode->data = data;
  18.     newNode->left = newNode->right = nullptr;
  19.     return newNode;
  20. }
  21.  
  22. //void Insert(Node** root, int data)
  23. //{
  24. //  if (*root == nullptr)
  25. //  {
  26. //      *root = GetNewNode(data);
  27. //  }
  28. //}
  29.  
  30. Node* Insert(Node* root, int data)
  31. {
  32.     if (root == nullptr)
  33.     {
  34.         root = GetNewNode(data);
  35.     }
  36.     else if (data <= root->data)
  37.     {
  38.         root->left = Insert(root->left, data);
  39.     }
  40.     else
  41.     {
  42.         root->right = Insert(root->right, data);
  43.     }
  44.     return root;
  45. }
  46.  
  47. bool Search(Node* root, int data)
  48. {
  49.     if (root == nullptr)
  50.     {
  51.         return false;
  52.     }
  53.     else if (root->data == data)
  54.     {
  55.         return true;
  56.     }
  57.     else if (data <= root->data)
  58.     {
  59.         return Search(root->left, data);
  60.     }
  61.     else
  62.     {
  63.         return Search(root->right, data);
  64.     }
  65. }
  66. ////////////////////////////////////////////////////////////////////2
  67.  
  68. int FindMin(Node* root)
  69. {
  70.     if (root == nullptr)
  71.     {
  72.         cout << "Error: Tree is empty\n";
  73.         return -1;
  74.     }
  75.  
  76.     while (root->left != nullptr)
  77.     {
  78.         root = root->left;
  79.     }
  80.     return root->data;
  81.  
  82.     /*Node* current = root;
  83.  
  84.     while (current->left != nullptr)
  85.     {
  86.         current = current->left;
  87.     }
  88.     return current->data;*/
  89. }
  90.  
  91. int FindMinRec(Node* root)
  92. {
  93.     if (root == nullptr)
  94.     {
  95.         cout << "Error: Tree is empty\n";
  96.         return -1;
  97.     }
  98.     else if (root->left == nullptr)
  99.     {
  100.         return root->data;
  101.     }
  102.     return FindMinRec(root->left);
  103. }
  104. ////////////////////////////////////////////////////////////////////3
  105.  
  106. int FindHeight(Node* root)
  107. {
  108.     if (root == nullptr)
  109.     {
  110.         return -1;
  111.     }
  112.     return std::max(FindHeight(root->left), FindHeight(root->right)) + 1;
  113. }
  114. ////////////////////////////////////////////////////////////////////4
  115.  
  116. void LevelOrder(Node* root)
  117. {
  118.     if (root == nullptr)
  119.     {
  120.         return;
  121.     }
  122.  
  123.     queue<Node*> Q;
  124.     Q.push(root);
  125.  
  126.     while (!Q.empty())
  127.     {
  128.         Node* current = Q.front();
  129.         cout << current->data << " ";
  130.  
  131.         if (current->left != nullptr)
  132.         {
  133.             Q.push(current->left);
  134.         }
  135.         if (current->right != nullptr)
  136.         {
  137.             Q.push(current->right);
  138.         }
  139.         Q.pop();
  140.     }
  141. }
  142. ////////////////////////////////////////////////////////////////////5
  143.  
  144. void Preorder(Node* root)
  145. {
  146.     if (root == nullptr)
  147.     {
  148.         return;
  149.     }
  150.     cout << root->data << " ";
  151.     Preorder(root->left);
  152.     Preorder(root->right);
  153. }
  154.  
  155. void Inorder(Node* root)
  156. {
  157.     if (root == nullptr)
  158.     {
  159.         return;
  160.     }
  161.     Inorder(root->left);
  162.     cout << root->data << " ";
  163.     Inorder(root->right);
  164. }
  165.  
  166. void Postorder(Node* root)
  167. {
  168.     if (root == nullptr)
  169.     {
  170.         return;
  171.     }
  172.     Postorder(root->left);
  173.     Postorder(root->right);
  174.     cout << root->data << " ";
  175. }
  176.  
  177. ////////////////////////////////////////////////////////////////////6
  178.  
  179. bool IsSubTreeLesser(Node* root, int value)
  180. {
  181.     if (root == nullptr)
  182.     {
  183.         return true;
  184.     }
  185.     if (root->data <= value
  186.         && IsSubTreeLesser(root->left, value)
  187.         && IsSubTreeLesser(root->right, value))
  188.     {
  189.         return true;
  190.     }
  191.     else
  192.     {
  193.         return false;
  194.     }
  195. }
  196. bool IsSubTreeGreater(Node* root, int value)
  197. {
  198.     if (root == nullptr)
  199.     {
  200.         return true;
  201.     }
  202.     if (root->data > value
  203.         && IsSubTreeGreater(root->left, value)
  204.         && IsSubTreeGreater(root->right, value))
  205.     {
  206.         return true;
  207.     }
  208.     else
  209.     {
  210.         return false;
  211.     }
  212. }
  213.  
  214. bool IsBinarySearchTree(Node* root)
  215. {
  216.     if (root == nullptr)
  217.     {
  218.         return true;
  219.     }
  220.  
  221.     if (IsSubTreeLesser(root->left, root->data)
  222.         && IsSubTreeGreater(root->right, root->data)
  223.         && IsBinarySearchTree(root->left)
  224.         && IsBinarySearchTree(root->right))
  225.     {
  226.         return true;
  227.     }
  228.     else
  229.     {
  230.         return false;
  231.     }
  232. }
  233.  
  234. //bool IsBstUtil(Node* root, int minValue, int maxValue)
  235. //{
  236. //    if (root == nullptr)
  237. //    {
  238. //        return true;
  239. //    }
  240. //    if (root->data < minValue && root-> data > maxValue  
  241. //        && IsBstUtil(root->left, minValue, root->data)
  242. //        && IsBstUtil(root->right, root->data, maxValue))
  243. //    {
  244. //        return true;
  245. //    }
  246. //    else
  247. //    {
  248. //        return false;
  249. //    }
  250. //}
  251.  
  252. //bool IsBinarySearchTree(Node* root)
  253. //{
  254. //    return IsBstUtil(root, INT_MIN, INT_MAX);
  255. //}
  256.  
  257. ////////////////////////////////////////////////////////////////////7
  258.  
  259. Node* FindMIN(Node* root)
  260. {
  261.     if (root == nullptr)
  262.     {
  263.         return nullptr;
  264.     }
  265.     else if (root->left == nullptr)
  266.     {
  267.         return root;
  268.     }
  269.  
  270.     return FindMIN(root->left);
  271. }
  272.  
  273. Node* Delete(Node* root, int data)
  274. {
  275.     if (root == nullptr)
  276.     {
  277.         return root;
  278.     }
  279.     else if (data < root->data)
  280.     {
  281.         root->left = Delete(root->left, data);
  282.     }
  283.     else if (data > root->data)
  284.     {
  285.         root->right = Delete(root->right, data);
  286.     }
  287.     else
  288.     {   // case 1: No child
  289.         if (root->left == nullptr && root->right == nullptr)
  290.         {
  291.             delete root;
  292.             root = nullptr;
  293.             //return root;
  294.         }
  295.         // case 2: One child
  296.         else if (root->left == nullptr)
  297.         {
  298.             Node* temp = root;
  299.             root = root->right;
  300.             delete temp;
  301.             //return root;
  302.         }
  303.         else if (root->right == nullptr)
  304.         {
  305.             Node* temp = root;
  306.             root = root->left;
  307.             delete temp;
  308.             //return root;
  309.         }
  310.         // case 3: Two children
  311.         else
  312.         {
  313.             Node* temp = FindMIN(root->right);
  314.             root->data = temp->data;
  315.             root->right = Delete(root->right, temp->data);
  316.         }
  317.     }
  318.     return root;
  319. }
  320. ////////////////////////////////////////////////////////////////////8
  321.  
  322. Node* Findmin(Node* root)
  323. {
  324.     if (root == nullptr)
  325.     {
  326.         return nullptr;
  327.     }
  328.     while (root->left != nullptr)
  329.     {
  330.         root = root->left;
  331.     }
  332.     return root;
  333. }
  334.  
  335. Node* GetSuccessor(Node* root, int data)
  336. {
  337.     Node* current = Find(root, data);//Search?
  338.     if (current == nullptr)
  339.     {
  340.         return nullptr;
  341.     }
  342.     if (current->right != nullptr)
  343.     {
  344.         return Findmin(current->right);
  345.     }
  346.     else
  347.     {
  348.         Node* succesor = nullptr;
  349.         Node* ancestor = root;
  350.         while (ancestor != current)
  351.         {
  352.             if (current->data < ancestor->data)
  353.             {
  354.                 succesor = ancestor;
  355.                 ancestor = ancestor->left;
  356.             }
  357.             else
  358.             {
  359.                 ancestor = ancestor->right;
  360.             }
  361.         }
  362.         return succesor;
  363.     }
  364. }
  365.  
  366. int main()
  367. {
  368.     Node* root = nullptr;
  369.  
  370.     /*Insert(&root, 15);
  371.     Insert(&root, 10);
  372.     Insert(&root, 20);*/
  373.  
  374.     root = Insert(root, 15);
  375.     root = Insert(root, 10);
  376.     root = Insert(root, 20);
  377.  
  378.     root = Insert(root, 25);
  379.     root = Insert(root, 8);
  380.     root = Insert(root, 12);
  381.  
  382.     int number;
  383.  
  384.     cout << "Enter number be searched\n";
  385.     cin >> number;
  386.  
  387.     if (Search(root, number) == true)
  388.     {
  389.         cout << "Found\n";
  390.     }
  391.     else
  392.     {
  393.         cout << "Not Found\n";
  394.     }
  395.  
  396.     return 0;
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement