c++ program to print root to leaf paths without using recursion














































c++ program to print root to leaf paths without using recursion



DESCRIPTION:

In this c++ article i am going to explain how to print the nodes from root to leaf in binary tree .According to binary tree concept we need to traverse through the whole tree to print each path from root to leaf .In this program preorder traversal is used. There can be multiple paths  and as it is wrote without recursion so it will be using stack and map concept

for example : different paths for the following binary tree : PATHS ARE BEEN MARKED BY RED 


ALGORITHM:

1.      First the input will be binary tree.

        after that  current variable will traverse through the binary tree in which there will three conditions 

3.      case 1: if the current node has right node value then it will be stored in stack nodeStack1

4.      case 2:  if the current node has left node value then it will be stored in stack  nodeStack1

5.      case 3: if the current node doesn't have either right or left or both the nodes then the control will be shifted to printValues  function where it push the elements from bottom to  top and the print the values by poping out the top values .

 INPUT:

 Input is pre-defined if user want to change the values the user have to make changes in the program itself.

 CODE:


#include <bits/stdc++.h> 



using namespace std;


struct Node 

int data; 

struct Node *left, *right; 

}; 



Node* new1(int data) 

Node* node = new Node; 

node->data = data; 

node->left = node->right = NULL; 

return node; 

void printValues(Node* current, map<Node*, Node*> parent) ;

void traverse(Node* root) ;


int main() 


Node* r1 = new1(68); 

r1->left = new1(69); 

r1->right = new1(92); 

r1->left->left = new1(98); 

r1->left->right = new1(20); 

r1->right->left = new1(63); 


traverse(r1); 


return 0; 


void printValues(Node* current, map<Node*, Node*> parent) 

   

stack<Node*> s1; 

while (current) 

s1.push(current); 

current = parent[current]; 

while (s1.empty()==0) 

current = s1.top(); 

s1.pop(); 

cout << current->data << " "; 

cout << endl; 

void traverse(Node* root) 

if (root == NULL) 

return; 

    stack<Node*> nodeStack1; 

nodeStack1.push(root); 

map<Node*, Node*> parent; 

parent[root] = NULL; 


while (nodeStack1.empty()==0) 

 

Node* current = nodeStack1.top(); 

nodeStack1.pop(); 


if (current->right) 

parent[current->right] = current; 

nodeStack1.push(current->right); 

if (current->left) 

parent[current->left] = current; 

nodeStack1.push(current->left); 

if (!(current->left) && !(current->right)) 

printValues(current, parent); 


                    

 

EXPLANATION:

In this example the input be binary tree which is given below:

    

  • this binary tree will transferred to the traverse function "traverse(Node* root)"
  • in this function the values of the binary tree are saved in the map "parent" according to the three cases .to store the values in  the map "nodestack1" node is used.the values will be stored as shown below:
  •  


  • if     the third condition is valid it will be going to print the values using the stack s1 using pusing and poping values from the stack.
     in     this function printValues(Node* current, map<Node*, Node*> parent)  there will be two while loops which we work as shown below : 



OUTPUT:


More Articles of Konada Sunita:

Name Views Likes
C++ boost::accumulator::p_square_cumulative_distribution 672 1
C++ boost::Numeric Conversion::converter<> 864 10
C++ boost::ref 1227 9
C++ boost::accumulator::count 930 2
C++ boost::function::function_base 557 10
C++ boost::accumulator::weighted_mean 889 1
C++ boost::accumulator::non_coherent_weighted_tail_mean 563 1
C++ boost::accumulator::rolling_count rolling_sum rolling_mean rolling_moment 727 2
C++ boost::lockfree::spsc_queue 2202 10
C++ boost::coroutine:: passing data 640 1
C++ boost::accumulator::weighted_tail_quantile 619 1
C++ boost::TokenizerClass 83 2
C++ boost::accumulator::weighted_variance 559 1
C++ boost::accumulator::rolling mean 2595 2
C++ boost::accumulator::median 1417 1
C++ boost::accumulator::extended_p_square_quantile 837 1
C++ boost::accumulator::extended_p_square 648 1
C++ boost::function::functionN 829 10
C++ boost::accumulator::weighted_density 505 1
C++ boost::accumulator::covariance density 780 3
C++ boost::accumulator::weighted_skewness 503 1
C++ boost::format::formatter 654 10
C++ boost::accumulator::skewness 911 1
C++ boost::accumulator::max 700 3
C++ boost::accumulator::variance 992 1
C++ boost::accumulator::min 592 2
C++ boost::accumulator::density 951 1
C++ boost::token_iterator 885 10
c++ program to print root to leaf paths without using recursion 705 10
C++ boost::accumulator::rolling variance 1476 4
C++ boost::accumulator::weighted_sum 544 1
C++ boost::accumulator::weighted_extended_p_square_quantile 605 2
C++ boost::accumulator::sum_kahan 1117 2
C++ boost::accumulator::weighted_moment 469 1
C++ boost::accumulator::error_of 658 2
C++ boost::accumulator::tail 845 2
C++ boost::format 4138 10
C++ boost::accumulator::weighted_p_square_cumulative_distribution 522 1
C++ boost::accumulator::coherent_tail_mean 471 1
C++ boost::accumulator::rolling count 1153 2
C++ boost::accumulator::moment 703 2
C++ boost::lockfree::stack 1207 8
C++ boost::accumulator::sum 821 2
C++ boost::function::bad_function_call() 1096 10
C++ boost::accumulators::Mean Moment Count 1370 2
C++ boost::accumulator::mean 1617 2
C++ boost::accumulator::non_coherent_tail_mean 501 1
C++ boost::accumulator::weighted_extended_p_square 521 1
C++ boost::accumulator::weighted_kurtosis 522 1
C++ boost::accumulator::covariance density error_of_mean 587 9
C++ boost::accumulator::rolling sum 834 2
C++ boost::lockfree::queue 3084 9
C++ boost::accumulator::weighted_p_square_quantile 852 1
C++ boost::accumulator::p_square_quantile 840 1
C++ boost::accumulator::tail_quantile 1201 1
C++ boost::accumulator::rolling moment 598 2
C++ boost::accumulator::min max 1230 3
C++ boost::accumulator::kurtosis 1209 1
C++ boost::coroutine::Pushtype and pulltype 1564 1
C++ boost::accumulator::tail_variate 491 2
C++ boost::accumulator::weighted_covariance 519 2
C++ boost::format::Exception 2202 10
C++ boost::function 3035 10
C++ boost::NumericConversion::bounds<> 677 10
C++ boost::NumericConversion::UDT 853 10

Comments