Advertisement
Guest User

Untitled

a guest
Dec 26th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. template<class T>//template class
  5. class Item
  6. {
  7. public:
  8. Item* left;//pointer on the left item
  9. Item* right;//pointer on the right item
  10. T data;//data variable
  11. public:
  12. Item();//default constructor
  13. Item(T);//constractor(data)
  14. T getData();//get of data
  15. void setData(T);//set of data
  16. };
  17.  
  18. template <class T>
  19. Item<T>::Item()//default constructor
  20. {
  21. this->left=NULL;
  22. this->right=NULL;
  23. }
  24.  
  25. template <class T>
  26. Item <T>::Item(T x)//constractor(<T>data=x)
  27. {
  28. data=x;
  29. left=right=NULL;
  30. }
  31. template <class T>
  32. T Item <T>::getData()//get of data
  33. {
  34. return data;
  35. }
  36. template <class T>
  37. void Item <T>::setData(T x)//set of data
  38. {
  39. data=x;
  40. }
  41.  
  42. #include "Item.h"
  43.  
  44. template<class T>
  45. class BinaryTree
  46. {
  47. private:
  48. Item<T>* root;//unit of tree
  49. public:
  50. BinaryTree(){root=NULL;};//default constractor
  51. BinaryTree(Item<T>);//constractor
  52. Item<T>* getRoot();//get pointer on the root of the tree
  53.  
  54. void setRoot(Item<T> );//seting a new root of the tree (deleting the old root)
  55. void setLeft(BinaryTree<T> );//seting a left tree
  56. void setRight(BinaryTree<T> );//seting a right tree
  57.  
  58. Item<T>* removeLeft();//removing of left child
  59. Item<T>* removeRight();//removing of right child
  60.  
  61. void preorder();//tree preorder
  62. void trip_preorder(Item<T>*);//preorder service function
  63. void inorder();//tree inorder
  64. void trip_inorder(Item<T>*);//inorder service function
  65. void postorder();//tree inorder
  66. void trip_postorder(Item<T>*);//postorder service function
  67. void levelorder();//tree level order
  68. void trip_levelorder(Item<T>*);//levelorder service function
  69. bool isEmpty() const { return root==NULL; }//Checking if tree is empty (boolean function)
  70.  
  71. };
  72.  
  73. template<class T>
  74. BinaryTree<T>::BinaryTree(Item<T> x)//constractor (<T>Item=x)
  75. {
  76. Item<T>* tmp=new Item<T>(x);//creating the temporarry unit (data copying constructor)
  77. tmp->left=tmp->right=NULL;//childs=0
  78. root=tmp;//copying of tmp variable by the adrres
  79. }
  80.  
  81. template<class T>
  82. Item<T>* BinaryTree<T>::getRoot()//get pointer on the root of the tree
  83. {
  84. return root;
  85. }
  86.  
  87. template<class T>
  88. void BinaryTree<T>::setRoot(Item<T> item)//seting a new root of the tree (deleting the old root)
  89. {
  90. Item<T>* tmp=new Item<T>(item);//creating the temporarry unit (data copying constructor)
  91. //seting the pointers on childs
  92. tmp->left=this->root->left;
  93. tmp->right=this->root->right;
  94. //seting the new root by copying the adrres
  95. this->root=tmp;
  96. }
  97.  
  98. template<class T>
  99. void BinaryTree<T>::setLeft(BinaryTree<T> tree)//seting a left tree
  100. {
  101. this->root->left=tree.root;
  102. }
  103.  
  104. template<class T>
  105. void BinaryTree<T>::setRight(BinaryTree<T> tree)//seting a right tree
  106. {
  107.  
  108. this->root->right=tree.root;
  109. }
  110.  
  111. template<class T>
  112. Item<T>* BinaryTree<T>::removeLeft()//removing of left child and returning of this child
  113. {
  114. Item<T>* leftRoot=new Item<T>(root->left->data);
  115. leftRoot=root->left;
  116. this->root->left=NULL;
  117. return leftRoot;
  118. }
  119.  
  120. template<class T>
  121. Item<T>* BinaryTree<T>::removeRight()//removing of right child and returning of this child
  122. {
  123. Item<T>* rightRoot=new Item<T>(root->right->data);
  124. rightRoot=root->right;
  125. this->root->right=NULL;
  126. return rightRoot;
  127. }
  128.  
  129. template<class T>
  130. void BinaryTree<T>::inorder()//inorder function
  131. {
  132. trip_inorder(root);//sending the root to service recursion trip_inorder function
  133. }
  134.  
  135. template<class T>
  136. void BinaryTree<T>::trip_inorder(Item<T>* walker)//recursion trip_inorder function
  137. {
  138. /*
  139. left
  140. print
  141. right
  142. */
  143. if(walker!=NULL)
  144. {
  145. if(walker->left)
  146. {
  147. trip_inorder(walker->left);
  148. }
  149. cout<<" "<<walker->data<<" ";
  150. if(walker->right)
  151. {
  152. trip_inorder(walker->right);
  153. }
  154.  
  155. }
  156. else
  157. return;
  158. }
  159.  
  160. template<class T>
  161. void BinaryTree<T>::preorder()//preorder function
  162. {
  163. trip_preorder(root);//sending the root to service recursion trip_preorder function
  164. }
  165.  
  166. template<class T>
  167. void BinaryTree<T>::trip_preorder(Item<T>* walker)
  168. {
  169. /*
  170. print
  171. left
  172. right
  173. */
  174. if(walker!=NULL)
  175. {
  176. cout<<" "<<walker->data<<" ";
  177. if(walker->left)
  178. {
  179. trip_preorder(walker->left);
  180. }
  181. if(walker->right)
  182. {
  183. trip_preorder(walker->right);
  184. }
  185. }
  186. else
  187. return;
  188. }
  189.  
  190. template<class T>
  191. void BinaryTree<T>::postorder()//postorder function
  192. {
  193. trip_postorder(root);//sending the root to service recursion trip_postorder function
  194. }
  195.  
  196. template<class T>
  197. void BinaryTree<T>::trip_postorder(Item<T>* walker)
  198. {
  199. /*
  200. left
  201. right
  202. print
  203. */
  204. if(walker!=NULL)
  205. {
  206. if(walker->left)
  207. {
  208. trip_postorder(walker->left);
  209. }
  210. if(walker->right)
  211. {
  212. trip_postorder(walker->right);
  213. }
  214. cout<<" "<<walker->data<<" ";
  215. }
  216. else
  217. return;
  218. }
  219.  
  220. template<class T>
  221. void BinaryTree<T>::levelorder()//level order function (print each level for left to right)
  222. {
  223. cout<<" "<<root->data<<" ";//Printing of the root of the tree
  224. trip_levelorder(root);//sending the root to recursion levelorder function
  225. }
  226.  
  227. template<class T>
  228. void BinaryTree<T>::trip_levelorder(Item<T>* walker)
  229. {
  230. if(walker!=NULL)
  231. {
  232. if(walker->left && walker->right)//If we have left and right child
  233. {
  234. cout<<" "<<walker->left->data<<" ";//print the left child
  235. cout<<" "<<walker->right->data<<" ";//print the right child
  236. trip_levelorder(walker->left), //firstly sending the left child to recursion trip_levelorder
  237. trip_levelorder(walker->right); //And at the same time, through a SUPER MICROSECOND later, we are sending the right child to recursion trip_levelorder
  238. }
  239. else if(walker->left && !walker->right)//if we have left child and don't have right child
  240. {
  241. cout<<" "<<walker->left->data<<" ";//print the left child
  242. trip_levelorder(walker->left);//and sending the left child to the recursion trip_levelorder
  243. }
  244. else if(walker->right && !walker->left)//if we have right child and don't have left child
  245. {
  246. cout<<" "<<walker->right->data<<" ";//print the right child
  247. trip_levelorder(walker->right);////and sending the right child to the recursion trip_levelorder
  248. }
  249. }
  250. else
  251. return;
  252. }
  253.  
  254. # include <iostream>
  255. # include <string>
  256. # include <cassert>
  257. # include <stdlib.h>
  258. #pragma once
  259. typedef struct Node
  260. {
  261. char x; //data
  262. Node *Next,*Head; //pointers on next and head of node
  263. }Node;
  264. using namespace std;
  265. class Stack
  266. {
  267. public:
  268. char x; //data
  269. Node* list;//our Stack
  270. public:
  271. Stack();//default constructor
  272. void push(char);//push new nod(data)
  273. char pop();//pop from stack
  274. char top();// top of the stack
  275. bool isEmpty();//checkin if stack is empty
  276.  
  277. void mix_stack();//mix of the stack
  278. void Show();//printig all the stack
  279. void NodePrint(Node*);//printing unit of stack
  280. ~Stack();//destructor
  281. };
  282.  
  283. #include "Stack.h"
  284.  
  285. Stack::Stack()//default constructor
  286. {
  287. list=new Node;
  288. assert(list);
  289. list->Head=NULL;
  290. }
  291.  
  292. void Stack::push(char x)//push new nod(data=x)
  293. {
  294. Node* temp=new Node;
  295. assert(temp);
  296. temp->x=x;
  297. temp->Next=list->Head;
  298. list->Head=temp;
  299. }
  300.  
  301. char Stack::pop()//pop from stack
  302. {
  303. char i;
  304. if(!isEmpty())
  305. {
  306. i=list->Head->x;
  307. Node* temp=list->Head->Next;
  308. delete list->Head;
  309. list->Head=temp;
  310. return i;
  311. }
  312. else
  313. cout<<"Error,stack is empty!"<<endl;
  314. return 0;
  315. }
  316.  
  317. char Stack::top()// top of the stack
  318. {
  319. if(!isEmpty())
  320. return list->Head->x;
  321. else
  322. cout<<"Error,stack is empty!"<<endl;
  323. return 0;
  324. }
  325.  
  326. bool Stack::isEmpty()//checkin if stack is empty
  327. {
  328. if(list-> Head==NULL )
  329. return true;
  330. return false;
  331. }
  332.  
  333. void Stack::mix_stack()//mix of the stack(with 2 service stack's)
  334. {
  335. Stack S1,S2;
  336. while(!this->isEmpty())
  337. {
  338. S1.push(this->pop());
  339. }
  340. while(!S1.isEmpty())
  341. {
  342. S2.push(S1.pop());
  343. }
  344. while(!S2.isEmpty())
  345. {
  346. this->push(S2.pop());
  347. }
  348. }
  349. void Stack::Show()//printig all the stack
  350. {
  351. Node* temp=new Node;
  352. assert(temp);
  353. temp=list->Head;
  354. while(temp!=NULL)
  355. {
  356. cout<<temp->x<<endl;
  357. temp=temp->Next;
  358. }
  359. cout<<endl;
  360. }
  361.  
  362. void Stack::NodePrint(Node* temp)//printing unit of stack
  363. {
  364. cout<<temp->x;
  365. }
  366.  
  367. Stack::~Stack()//destructor(while stack is not empty delete of each unit)
  368. {
  369. while(!isEmpty())
  370. {
  371. Node* temp=list->Head->Next;
  372. delete list->Head;
  373. list->Head=temp;
  374. }
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement