pastebin

Paste Search Dynamic
Recent pastes
Tree Node
  1. #include<iostream>
  2. using namespace std;
  3. template<typename type>
  4.  
  5. class Tree{
  6. struct Node{
  7. type data;
  8. Node* left;
  9. Node* right;
  10. Node (type d) {
  11.  
  12. this->data= d;
  13. this->left= this->right= nullptr;
  14. }
  15. };
  16. public:
  17.  
  18. Node* Root=nullptr;
  19. Tree() {
  20. Node* f=new Node('F');
  21. Node* b=new Node('B');
  22. Node* g=new Node('G');
  23. Node* a=new Node('A');
  24. Node* d=new Node('D');
  25. Node* i=new Node('I');
  26. Node* c=new Node('C');
  27. Node* e=new Node('E');
  28. Node* h=new Node('H');
  29. f->left=b; f->right=g;
  30. b->left=a; b->right=d;
  31. g->right=i;
  32. d->left=c; d->right=e;
  33. i->left;
  34. }
  35. void preorder (Node* ptr){
  36. if (ptr!=nullptr) {
  37. cout<<" "<<ptr->data;
  38. preorder (ptr->left);
  39. preorder (ptr->right);
  40. }
  41. }
  42. void inorder (Node* ptr) {
  43. if (ptr!=nullptr) {
  44. inorder (ptr->left);
  45. cout<<" "<<ptr->data;
  46. inorder (ptr->right);
  47. }
  48. }
  49. void postorder (Node* ptr) {
  50. if (ptr!=nullptr) {
  51. postorder (ptr->left);
  52. postorder (ptr->right);
  53. cout<<" "<<ptr->data;
  54. }
  55. }
  56. };
  57. int main(){
  58. Tree<char> obj;
  59. if (obj.Root=nullptr)
  60. cout<<"Tree is full!!";
  61. else{
  62. cout<<"nPreorder Traversalt:t";
  63. obj.preorder (obj.Root);
  64. }
  65. return 0;
  66. }
Parsed in 0.024 seconds