m2skills

boundary traversal cpp

May 16th, 2018
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. // program to print the boundary traversal of a binary tree
  2.  
  3. #include <iostream>
  4. #include <queue>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. // node class
  10. class node{
  11. public:
  12.     int data;
  13.     node* left;
  14.     node* right;
  15. };
  16.  
  17. // function that returns a pointer to new node
  18. node* createNode(int element){
  19.     node* temp = (node*) malloc(sizeof(node));
  20.     temp->data = element;
  21.     temp->left = NULL;
  22.     temp->right = NULL;
  23.     return temp;
  24. }
  25.  
  26. // function to print the left boundary of the tree
  27. void print_left_boundary(node* root){
  28.     if (root != NULL){
  29.         if (root->left != NULL){
  30.             cout<<root->data<<" ";
  31.             print_left_boundary(root->left);
  32.         }else if (root->right != NULL){
  33.             cout<<root->data<<" ";
  34.             print_left_boundary(root->right);
  35.         }
  36.     }
  37.     return;
  38. }
  39.  
  40. // function to print the right boundary of the tree
  41. void print_right_boundary(node* root){
  42.     if (root != NULL){
  43.         if (root->right != NULL){
  44.             print_right_boundary(root->right);
  45.             cout<<root->data<<" ";
  46.         }else if (root->left != NULL){
  47.             print_right_boundary(root->left);
  48.             cout<<root->data<<" ";
  49.         }
  50.     }
  51.     return;
  52. }
  53.  
  54. // function to print the leaves of a binary tree
  55. void print_leaves(node* root){
  56.     if (root != NULL){
  57.         print_leaves(root->left);
  58.         if(root->left == NULL && root->right == NULL){
  59.             cout<<root->data<<" ";
  60.         }
  61.         print_leaves(root->right);
  62.     }
  63. }
  64.  
  65. void boundary_traversal(node* root){
  66.     print_left_boundary(root);
  67.     print_leaves(root);
  68.     print_right_boundary(root);
  69.     return;
  70. }
  71.  
  72.  
  73. int main() {
  74.     node* head = createNode(1);
  75.     head->left = createNode(2);
  76.     head->right = createNode(3);
  77.     head->left->left = createNode(4);
  78.     head->left->right = createNode(5);
  79.     head->right->right = createNode(6);
  80.     head->left->left->right = createNode(7);
  81.     head->right->right->left = createNode(8);
  82.     head->left->left->right->left = createNode(9);
  83.     head->left->left->right->left->left = createNode(10);
  84.     head->right->right->left->right = createNode(11);
  85.  
  86.     cout<<"Boundary traversal of the binary tree is : "<<endl;
  87.     boundary_traversal(head);
  88.     return 0;
  89. }
  90.  
  91.  
  92. /*
  93.  
  94. Boundary traversal of the binary tree is :
  95. 1 2 4 7 9 10 5 11 8 6 3 1
  96. Process finished with exit code 0
  97.  
  98. */
Add Comment
Please, Sign In to add comment