fork(1) download
  1. #ifndef STACK_H
  2. #define STACK_H
  3. //#include "BinaryTree.h"
  4. using namespace std;
  5.  
  6. #include <cassert>
  7. #include <vector>
  8.  
  9. template<class T>
  10. class stack
  11. {
  12. public:
  13. T pop() { assert(!empty()); T v = _data.back(); _data.pop_back(); return v; }
  14. void push(T x) { _data.push_back(x); }
  15. bool empty() { return _data.empty(); }
  16. int size() { return _data.size(); }
  17.  
  18. private:
  19. std::vector<T> _data;
  20. };
  21.  
  22. #endif /* STACK_H */
  23. #ifndef BINARYTREE_H
  24. #define BINARYTREE_H
  25. using namespace std;
  26.  
  27. template<typename T> class BinaryTree
  28. {
  29. public:
  30. // Binary Tree Things
  31. BinaryTree(); // default constructor to make empty tree
  32. BinaryTree(T ro); // default constructor 2 to make tree with only root
  33. BinaryTree(T ro, T le, T ri); // default constructor 3 to make complete binary tree
  34. ~BinaryTree(); // destructor for dynamics
  35.  
  36. BinaryTree(BinaryTree const& other) : root(other.root? new Tree_Node(*other.root) : 0) {}
  37.  
  38. bool isEmpty(); // method that returns t/f if tree is empty
  39. T info(); // method to return value in root of the tree
  40. void inOrder(); // traverses nodes in a tree left, root, right
  41. void preOrder(); // traverses nodes in a tree root, left, right
  42. void postOrder(); // traverses nodes in a tree left, right, root
  43.  
  44. private:
  45. struct Tree_Node // represents a node
  46. {
  47. T data;
  48. Tree_Node *left; // left pointer
  49. Tree_Node *right; // right pointer
  50.  
  51. Tree_Node(T data, Tree_Node* left = 0, Tree_Node* right = 0)
  52. : data(data), left(left), right(right) {}
  53.  
  54. Tree_Node(Tree_Node const& other)
  55. : data(other.data),
  56. left (other.left? new Tree_Node(*other.left) : 0),
  57. right(other.right?new Tree_Node(*other.right) : 0)
  58. {}
  59.  
  60. ~Tree_Node()
  61. {
  62. delete left;
  63. delete right;
  64. }
  65. };
  66.  
  67. Tree_Node *root; // create root with 2 pointers from this };
  68. };
  69. /***********************************************************************/
  70.  
  71. template<typename T> BinaryTree<T>::BinaryTree()
  72. : root(0)
  73. {
  74. }
  75.  
  76. template<typename T> BinaryTree<T>::BinaryTree(T ro)
  77. : root(new Tree_Node(ro, 0, 0))
  78. {
  79. }
  80.  
  81. template<typename T> BinaryTree<T>::BinaryTree(T ro, T le, T ri)
  82. : root(new Tree_Node(ro,
  83. new Tree_Node (le, 0, 0),
  84. new Tree_Node (ri, 0, 0)))
  85. {
  86. }
  87.  
  88. template<typename T> BinaryTree<T>::~BinaryTree() {
  89. delete root;
  90. }
  91.  
  92. template<typename T> bool BinaryTree<T>::isEmpty()
  93. {
  94. return !root;
  95. }
  96.  
  97. template<typename T> T BinaryTree<T>::info()
  98. {
  99. }
  100.  
  101. template<typename T> void BinaryTree<T>::inOrder()
  102. {
  103. }
  104.  
  105. template<typename T> void BinaryTree<T>::preOrder()
  106. {
  107. }
  108.  
  109. template<typename T> void BinaryTree<T>::postOrder()
  110. {
  111. }
  112.  
  113. #endif /* BINARYTREE_H */
  114.  
  115. #include <cstdlib>
  116. #include <iostream>
  117. #include <fstream>
  118. #include <iomanip>
  119. #include <math.h>
  120. #include <cmath>
  121. #include <ctime>
  122. #include <limits>
  123. //#include "BinaryTree.h"
  124. //#include "stack.h"
  125.  
  126. using namespace std;
  127.  
  128. int main()
  129. {
  130. stack<BinaryTree<char> > testing;
  131. BinaryTree<char> testing2('d', 'd', 'd');
  132. testing.push(testing2);
  133. cout << testing.size();
  134. return 0;
  135. }
  136.  
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
1