【问题标题】:Breadth First Traversal With Binary Search Tree C++使用二叉搜索树 C++ 的广度优先遍历
【发布时间】:2013-07-30 05:55:09
【问题描述】:

也许是快速/简单的问题。我已经实现了一棵二叉树,然后我希望将二叉搜索树转换为数组,或者至少像在数组中一样打印出来。我遇到的问题是如何在 '\0' 中获取 NULL/标志。

例如,假设我有一棵树:

                10
               /  \
              6   12
             / \   \
            1  8   15
             \
              4   

我希望它打印出它应该如何打印。喜欢:

        [10,6,12,1,8,\0,15,\0,4,\0,\0,\0,\0,\0,\0]
        ^Something Like this^ I don't know if I counted the NULL correctly.

或者关于我想如何以可视方式显示我的树的另一个选项是如何正确输出间距,例如“/”和“\”指向父母的键:

                10
               /  \
              6   12
             / \   \
            1  8   15
             \
              4   

这是我尝试在代码方面详细阐述的内容,但我卡住了:

void BreadthFirstTravseral(struct node* root)
{
    queue<node*> q;

    if (!root) {
        return;
    }
    for (q.push(root); !q.empty(); q.pop()) {
        const node * const temp_node = q.front();
        cout<<temp_node->data << " ";

        if (temp_node->left) {
            q.push(temp_node->left);
        }
        if (temp_node->right) {
            q.push(temp_node->right);
        }
    }
}

非常感谢任何类型的帮助或链接和/或建议和/或示例代码。

【问题讨论】:

    标签: c++ binary-search-tree breadth-first-search


    【解决方案1】:

    很难正确获得间距,因为键可能有多个数字,这会影响给定节点上方所有级别的间距。

    至于如何添加NULL - 只需在打印 NULL 的 ifs 中添加 else 子句:

    if (root) {
      q.push(root);
      cout << root->data << " ";  
    } else {
      cout << "NULL ";
    }
    while (!q.empty()) {
        const node * const temp_node = q.front(); 
        q.pop();
    
        if (temp_node->left) {
          q.push(temp_node->left);
          cout << temp_node->left->data << " ";
        } else {
          cout << "NULL ";
        }
    
    
        if (temp_node->right) {
          q.push(temp_node->right);
          cout << temp_node->right->data << " ";
        } else {
          cout << "NULL ";
        }
    }
    

    【讨论】:

    • While 循环不会因为 q 中没有弹出任何内容而永远消失吗?我的计算机系统正在实现无限循环。
    • @Xaphen 是的,谢谢。我忘了弹出。会马上解决的
    【解决方案2】:
    void TreeBreadthFirst(Node*  treeRoot) 
    {  
     Queue *queue  = new Queue();
    
          if (treeRoot == NULL) return;
           queue->insert(treeRoot); 
           while (!queue->IsEmpty())
              {          
              Node * traverse = queue->dequeue();
             cout<< traverse->data << “ “ ;
              if (traverse->left != NULL) 
                queue->insert( traverse->left); 
              if (traverse->right != NULL) 
                queue->insert(traverse->right); 
               } 
          delete queue;
           } 
    

    【讨论】:

      【解决方案3】:

      我用 c 编写了一个程序。此代码将显示得有点像一棵树。

      struct node{
          int val;
          struct node *l,*r;
      };
      
      typedef struct node node;
      
      
      int findDepth(node *t){
          if(!t) return 0;
          int l,r;
          l=findDepth(t->l);
          r=findDepth(t->r);
      
          return l>r?l+1:r+1;
      }
      
      void disp(node *t){
          if(!t)
              return;
          int l,r,i=0;
          node *a[100],*p;
          int front=0,rear=-1,d[100],dep,cur,h;
          a[++rear]=t;
          d[rear]=0;
          cur=-1;
          h=findDepth(t);
          printf("\nDepth : %d \n",h-1);
      
          while(rear>=front){
              dep = d[front];
              p=a[front++];
              if(dep>cur){
                  cur=dep;
                  printf("\n");
                  for(i=0;i<h-cur;i++) printf("\t");
              }
              if(p){
                  printf("%d\t\t",p->val);
                  a[++rear]=p->l;
                  d[rear]=dep+1;
                  a[++rear]=p->r;
                  d[rear]=dep+1;
              }
              else printf ("-\t\t");
      
          }
      }
      

      【讨论】:

      • 为代码添加一些解释并编辑您的答案。
      【解决方案4】:
      void BreadthFirstTravseral(struct node* root)
      {
          queue<node*> q;
      
          if (!root) {
              return;
          }
          for (q.push(root); !q.empty(); q.pop()) {
              const node * const temp_node = q.front();
              if( temp_node->special_blank ){
                  cout << "\\0 " ;
                  continue;//don't keep pushing blanks
              }else{
                  cout<<temp_node->data << " ";
              }
              if (temp_node->left) {
                  q.push(temp_node->left);
              }else{
                  //push special node blank
              }
              if (temp_node->right) {
                  q.push(temp_node->right);
              }else{
                  //push special node blank
              }
          }
      }
      

      【讨论】:

      • 什么是“special_blank”标识符?
      • 您可以在节点结构中添加一个布尔值。如果不是特殊节点则为假,否则为真。这样您就可以捕获特殊的空白节点并打印空叶节点。
      【解决方案5】:

      这个怎么样:

      std::vector<node*> list;
      list.push_back(root);
      int i = 0;
      while (i != list.size()) {
        if (list[i] != null) {
          node* n = list[i];
          list.push_back(n->left);
          list.push_back(n->right);
        } 
        i++;
      }
      

      未经测试,但我认为它应该可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-08
        • 1970-01-01
        • 1970-01-01
        • 2019-08-10
        • 1970-01-01
        相关资源
        最近更新 更多