SHARE
TWEET

lab3zad2.cpp

michalkowalczyk Mar 19th, 2019 13 in 152 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<functions.h>
  2.  
  3.  
  4. std::unique_ptr<node> CreateLeaf(int val){
  5.  
  6.     std::unique_ptr<node> new_node = std::make_unique<node>();
  7.     new_node->value=val;
  8.  
  9.  
  10.  
  11.  
  12.  
  13.     return new_node;
  14. }
  15.  
  16. void PrintTreeInOrder(const std::unique_ptr<node> &root)
  17. {
  18.     if(root->left_child !=nullptr)
  19.         PrintTreeInOrder(root->left_child);
  20.  
  21.     std::cout<< root->value<< "\t";
  22.  
  23.     if(root->right_child !=nullptr)
  24.         PrintTreeInOrder(root->right_child);
  25.  
  26.  
  27.  
  28.  
  29.  
  30.     return;
  31. }
  32.  
  33. void PrintTreePreOrder(const std::unique_ptr<node> &root){
  34.  
  35.     std::cout<< root->value<< "\t";
  36.  
  37.     if(root->left_child !=nullptr)
  38.         PrintTreePreOrder(root->left_child);
  39.  
  40.  
  41.     if(root->right_child !=nullptr)
  42.         PrintTreePreOrder(root->right_child);
  43.  
  44.  
  45.  
  46.  
  47.  
  48. }
  49.  
  50. void PrintTreePostOrder(const std::unique_ptr<node> &root){
  51.  
  52.  
  53.  
  54.     if(root->left_child !=nullptr)
  55.         PrintTreePostOrder(root->left_child);
  56.  
  57.  
  58.     if(root->right_child !=nullptr)
  59.         PrintTreePostOrder(root->right_child);
  60.  
  61.     std::cout<< root->value<< "\t";
  62.  
  63. }
  64.  
  65. std::unique_ptr<node> InsertNode(std::unique_ptr<node> root,int val){
  66.  
  67.     std::unique_ptr<node> new_node = CreateLeaf(val);
  68.  
  69.     if(root==nullptr){
  70.         root=move(new_node);
  71.  
  72.     }
  73.  
  74.     else {
  75.  
  76.  
  77.  
  78.         if(root->value <= val){
  79.  
  80.             root->right_child = InsertNode(std::move(root->right_child),val);
  81.  
  82.  
  83.         }
  84.         else {
  85.  
  86.             root->left_child = InsertNode(std::move(root->left_child),val);
  87.  
  88.  
  89.         }
  90.  
  91.  
  92.  
  93.     }
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.     return root;
  101.  
  102. }
  103.  
  104. std::string DumpTree( std::unique_ptr<node> &root)
  105. {
  106.     std :: stringstream ss_tree;
  107.     std :: string tree;
  108.     //std::unique_ptr<node> wsk;
  109.  
  110.  
  111.     ss_tree<<root->value;
  112.     ss_tree << DumpTree(root->left_child);
  113.     ss_tree << DumpTree(root->right_child);
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.     tree = ss_tree.str();
  123.  
  124.     return tree;
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top