m2skills

spiral BT cpp

Jun 1st, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. // http://code2begin.blogspot.com
  2. // Program to print the given binary tree in a spiral order level wise traversal
  3. #include <iostream>
  4. #include <stack>
  5.  
  6. using namespace std;
  7.  
  8. // node class
  9. class node{
  10. public:
  11.     int data;
  12.     node* left;
  13.     node* right;
  14. };
  15.  
  16. // function that returns a pointer to new node
  17. node* createNode(int element){
  18.     node* temp = (node*) malloc(sizeof(node));
  19.     temp->data = element;
  20.     temp->hd = -1;
  21.     temp->left = NULL;
  22.     temp->right = NULL;
  23.     return temp;
  24. }
  25.  
  26. // function to print the tree using spiral level wise traversal
  27. void spiral_traversal(node* root){
  28.     if (root == NULL){
  29.         return;
  30.     }
  31.     stack<node *> STACK1, STACK2;
  32.     int level = 1;
  33.     STACK1.push(root);
  34.     while(!STACK1.empty() || !STACK2.empty()){
  35.         cout<<"\nNodes at level "<<level<<" are : ";
  36.         level += 1;
  37.         while (!STACK1.empty()){
  38.             // pop the first node from the queue
  39.             node *NODE = STACK1.top();
  40.             STACK1.pop();
  41.             cout<<NODE->data<<" ";
  42.  
  43.             // push the left child on queue
  44.             if (NODE->right != NULL) {
  45.                 STACK2.push(NODE->right);
  46.             }
  47.  
  48.             // push the right child on queue
  49.             if (NODE->left != NULL) {
  50.                 STACK2.push(NODE->left);
  51.             }
  52.         }
  53.  
  54.         cout<<"\nNodes at level "<<level<<" are : ";
  55.         level += 1;
  56.         while (!STACK2.empty()){
  57.             // pop the first node from the queue
  58.             node *NODE = STACK2.top();
  59.             STACK2.pop();
  60.             cout<<NODE->data<<" ";
  61.  
  62.             // push the left child on queue
  63.             if (NODE->right != NULL) {
  64.                 STACK1.push(NODE->right);
  65.             }
  66.  
  67.             // push the right child on queue
  68.             if (NODE->left != NULL) {
  69.                 STACK1.push(NODE->left);
  70.             }
  71.         }
  72.     }
  73. }
  74.  
  75.  
  76. int main() {
  77.  
  78.     node* head = createNode(1);
  79.     head->left = createNode(2);
  80.     head->right = createNode(3);
  81.     head->left->left = createNode(4);
  82.     head->left->right = createNode(5);
  83.     head->right->right = createNode(6);
  84.     head->left->left->right = createNode(7);
  85.     head->right->right->left = createNode(8);
  86.     head->left->left->right->left = createNode(9);
  87.     head->left->left->right->left->left = createNode(10);
  88.     head->right->right->left->right = createNode(11);
  89.    
  90.     cout<<"SPIRAL Level wise traversal of the above binary tree is : "<<endl;
  91.     spiral_traversal(head);
  92.  
  93. }
Add Comment
Please, Sign In to add comment