두 갈래 나무, 뿌리 노드에서 잎 노드까지의 경로

루트 노드를 잎 노드로 내보내는 경로

  
  
  
  
  1. #include <iostream>  
  2. #include <vector>  
  3. using namespace std;  
  4.   
  5. struct node   
  6. {  
  7.     int self; //    
  8.     node *left; //    
  9.     node *right; //    
  10.  };  
  11.   
  12. void PrintPath(node * root,vector<int> path,int length) 
  13. { 
  14.     if (root) 
  15.     { 
  16.         path.push_back(root->self); 
  17.         if (!root->left&&!root->right) 
  18.         //if (root->self==find) 
  19.         { 
  20.             for (int i=0;i<path.size();i++) 
  21.             { 
  22.                 cout<<path[i]<<","; 
  23.             } 
  24.             cout<<endl; 
  25.             return;  
  26.         } 
  27.         else 
  28.         { 
  29.             PrintPath(root->left,path,length+1); 
  30.             PrintPath(root->right,path,length+1);    
  31.         } 
  32.     } 
  33. } 
  34.  
  35. void main()  
  36. {  
  37.     const int TREE_SIZE = 10;  
  38.     std::stack <node*> visited, unvisited;  
  39.     node nodes[TREE_SIZE];   
  40.       
  41.     for( int i=0; i<TREE_SIZE; i++) //   
  42.     {  
  43.         nodes[i].self = i;  
  44.         int child = i*2+1;  
  45.         if( child<TREE_SIZE ) //Left child  
  46.             nodes[i].left = &nodes[child];  
  47.         else  
  48.             nodes[i].left = NULL;  
  49.         child++;  
  50.         if( child<TREE_SIZE ) //Right child      
  51.             nodes[i].right = &nodes[child];  
  52.         else  
  53.             nodes[i].right = NULL;  
  54.     }  
  55.      
  56.     vector<int> path; 
  57.     PrintPath(nodes,path,0); 
  58.     
  59. }  

루트 노드로 지정된 경로 내보내기

  
  
  
  
  1. #include <iostream>  
  2. #include <vector>  
  3. using namespace std;  
  4.   
  5. struct node   
  6. {  
  7.     int self; //    
  8.     node *left; //    
  9.     node *right; //    
  10.  };  
  11.   
  12. void PrintPath(node * root,vector<int> path,int length,int find) 
  13. { 
  14.     if (root) 
  15.     { 
  16.         path.push_back(root->self); 
  17.         //if (!root->left&&!root->right) 
  18.         if (root->self==find) 
  19.         { 
  20.             for (int i=0;i<path.size();i++) 
  21.             { 
  22.                 cout<<path[i]<<","; 
  23.             } 
  24.             cout<<endl; 
  25.             return;  
  26.         } 
  27.         else 
  28.         { 
  29.             PrintPath(root->left,path,length+1,find); 
  30.             PrintPath(root->right,path,length+1,find);   
  31.         } 
  32.          
  33.     } 
  34.  
  35. } 
  36.  
  37. void main()  
  38. {  
  39.      
  40.      
  41.     const int TREE_SIZE = 10;  
  42.     std::stack <node*> visited, unvisited;  
  43.     node nodes[TREE_SIZE];   
  44.       
  45.     for( int i=0; i<TREE_SIZE; i++) //   
  46.     {  
  47.         nodes[i].self = i;  
  48.         int child = i*2+1;  
  49.         if( child<TREE_SIZE ) //Left child  
  50.             nodes[i].left = &nodes[child];  
  51.         else  
  52.             nodes[i].left = NULL;  
  53.         child++;  
  54.         if( child<TREE_SIZE ) //Right child      
  55.             nodes[i].right = &nodes[child];  
  56.         else  
  57.             nodes[i].right = NULL;  
  58.     }  
  59.      
  60.     vector<int> path; 
  61.     PrintPath(nodes,path,0,4); 
  62.     
  63. }  

좋은 웹페이지 즐겨찾기