SHARE
TWEET

balance_tree.cpp

a guest Mar 19th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "bintree.h"
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <list>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8. using namespace main_savitch_10;
  9.  
  10. binary_tree_node<int>* create_tree ();
  11. // return: pointer root of binary search tree of integers
  12.  
  13. template <typename T>
  14. list<T>* flatten (const binary_tree_node<T>* t);
  15. // precondition: t is a pointer root of binary search tree
  16. // return: non-null pointer to a sorted list containing the elements of t
  17.  
  18. template <typename T>
  19. binary_tree_node<T>* rebuild_tree (const list<T>& l);
  20. // precondition: l is a sorted list
  21. // returns balanced binary search tree containing the elements of l
  22.  
  23. template <typename T>
  24. void print_list (const list<T>& l);
  25. // postcondition: l has been displayed on standard output
  26.  
  27. template <typename T>
  28. bool is_sorted (const list<T>& l);
  29. // return whether l is sorted in non-decreasing order
  30.  
  31.  
  32.  
  33. int main ()
  34. {
  35.   binary_tree_node<int>* t = create_tree ();
  36.   print (t, 0);
  37.   cout << endl;
  38.   list<int> l( 4, 5);
  39.   //list<int>* flat_t = flatten (t);
  40.   print_list (l);
  41.   /*cout << endl;
  42.   binary_tree_node<int>* bal_t = rebuild_tree (*flat_t);
  43.   print (bal_t, 2);
  44.   delete t;
  45.   delete bal_t;
  46.   delete flat_t;*/
  47.   return EXIT_SUCCESS;
  48. }
  49.  
  50. binary_tree_node<int>* create_tree()
  51. {
  52.   binary_tree_node<int>* t1 = new binary_tree_node<int>(20);
  53.   binary_tree_node<int>* t2 = new binary_tree_node<int>(99);
  54.   binary_tree_node<int>* t3 = new binary_tree_node<int>(16, NULL, t1);
  55.   binary_tree_node<int>* t4 = new binary_tree_node<int>(72, NULL, t2);
  56.   binary_tree_node<int>* t5 = new binary_tree_node<int>(56, NULL, t4);
  57.   binary_tree_node<int>* t6 = new binary_tree_node<int>(48, NULL, t5);
  58.   binary_tree_node<int>* t7 = new binary_tree_node<int>(27, t3, t6);
  59.   return t7;
  60. }
  61.  
  62.  
  63.  
  64.  
  65. template <typename T>
  66. list<T>* flatten (const binary_tree_node<T>* t){
  67.    
  68. }
  69.  
  70. template <typename T>
  71. binary_tree_node<T>* rebuild_tree (const list<T>& l){
  72.    
  73. }
  74.  
  75. template <typename T>
  76. void print_list (const list<T>& l){
  77.     cout<< l <<endl;
  78. }
  79.  
  80. template<typename T>
  81. std::ostream& operator<<(std::ostream& s, const std::list<T>& v) {
  82.     s.put('[');
  83.     char comma[3] = {'\0', ' ', '\0'};
  84.     for (const auto& e : v) {
  85.         s << comma << e;
  86.         comma[0] = ',';
  87.     }
  88.     return s << ']';
  89. }
  90.  
  91. template <typename T>
  92. bool is_sorted (const list<T>& l){
  93.    
  94. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top