首页 > 试题广场 >

以 A0 作为起点,对下面的无向图进行深度优先遍历时...

[单选题]
以 A0 作为起点,对下面的无向图进行深度优先遍历时,遍历顺序不可能是

  • A<sub>0</sub> A<sub>1</sub> A<sub>2</sub> A3
  • A<sub>0</sub> A<sub>1</sub> A3 A2
  • A<sub>0</sub> A2 A1 A3
  • A<sub>0</sub> A3 A1 A2
const int TREE_SIZE = 9; 
    std::stack<node*> visited, unvisited; 
    node nodes[TREE_SIZE]; 
    node* current; 
    for( int i=0; i<TREE_SIZE; i++) //初始化树 
        {    
            nodes[i].self = i;   
            int child = i*2+1;    
            if( child<TREE_SIZE ) //Left child       
            nodes[i].left = &nodes[child];    
            else nodes[i].left = NULL;    
            child++;    
            if( child<TREE_SIZE ) //Right child           
            nodes[i].right = &nodes[child];    
            else       nodes[i].right = NULL;
        }             
     unvisited.push(&nodes[0]); //先把0放入UNVISITED stack  
     while(!unvisited.empty()) //只有UNVISITED不空  
     {    
         current=(unvisited.top()); //当前应该访问的    
         unvisited.pop();      
         if(current->right!=NULL)     
         unvisited.push(current->right); // 把右边压入 因为右边的访问次序是在左边之后     
         if(current->left!=NULL)     
         unvisited.push(current->left);     
         visited.push(current);     
         cout<<current->self<<endl; 
      }


发表于 2019-10-04 10:45:04 回复(0)
只要你懂深度优先搜索,你就知道,确定先判断的方向不能改变,还可以向前便利时,不能回溯
发表于 2023-09-12 19:33:56 回复(0)