c++ - 给定二叉树,设计一种算法,该算法创建每个深度的所有节点的链表

标签 c++ linked-list binary-tree levels

我试过在c++中实现相同。我想我差不多了,但是从链表访问节点时遇到错误。

list<list<Node*> > Binary_Tree::level(Node* node)
{
list<Node*> current,parent;
list<list<Node*> > result;

list<Node*>::iterator itr;
if (node != NULL)
current.push_back(node);

while(current.size() > 0)
{
   result.push_back(current);
   parent = current;

   current.clear();
   itr = parent.begin();
   while(itr != parent.end())
   {
     if(itr->left != NULL) // I am getting an error here.error:    request for member ‘right’ in ‘* itr.std::_List_iterator<_Tp>::operator-><Node*>()’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?
     current.push_back(itr->left);

     if(itr->right != NULL)
       current.push_back(itr->right);

        itr++;
   }

}

返回结果;
}

最佳答案

这是上述问题的解决方案。我把完整的代码。

#include <iostream>
#include <list>

using namespace std;
struct Node{

Node *left;
Node *right;
int data;
Node(int d)
{
  left = NULL;
  right = NULL;
  data =d;
} 
};
class Binary_Tree{

Node *root;
list<list<Node*> > level(Node*);
public:
Binary_Tree()
{
   root = NULL;
} 

  void insert(int element);
  list<list<Node*> > level();

};
void Binary_Tree::insert(int ele)
{
 Node *node = new Node(ele); 
  if (root == NULL)
  {
    root = node;
    return;
  }
  Node* temp = root;
  while(temp != NULL)
  {
    if (ele > temp->data)
    {
      if(temp->right != NULL)
         temp = temp->right;
      else
      { 
        temp->right = node;
        return;
      }
    } 
   else
   {
      if(temp->left != NULL)
        temp = temp->left;
      else
       {
         temp->left = node;
         return;
       }
   }
  }  
}
list<list<Node*> > Binary_Tree::level()
{
   return level(root);
}
list<list<Node*> > Binary_Tree::level(Node* node)
{
   list<Node*> current,parent;
   list<list<Node*> > result;

   list<Node*>::iterator itr;
   if (node != NULL)
   current.push_back(node);

   while(current.size() > 0)
   {
    result.push_back(current);
    parent = current;

    current.clear();
    itr = parent.begin();
    while(itr != parent.end())
    {
     if((*itr)->left != NULL)
      current.push_back((*itr)->left);

     if((*itr)->right != NULL)
       current.push_back((*itr)->right);

        itr++;
   }

}
return result;
}

int main()
{
list<list<Node*> >l;
list<list<Node*> >::iterator itr;
list<Node*>::iterator itr1;

Binary_Tree bt;
bt.insert(3);
bt.insert(2);
bt.insert(4);
bt.insert(1);
bt.insert(5);
l=bt.level();

itr = l.begin();
while(itr != l.end())
{

 itr1 = (*itr).begin();
 while(itr1 != (*itr).end())
 {
     cout<<(*itr1)->data<<" ";
     itr1++; 
 }
 cout<<endl;
 itr++;

} 

return 0;
}

关于c++ - 给定二叉树,设计一种算法,该算法创建每个深度的所有节点的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28779540/

相关文章:

c - 在 C 段错误中追加链表

c - 在函数的参数中声明指针类型以修改链表

java - 链表实现,在头部添加还是在尾部添加?

c - 线程二叉树的好处

c++ - 将 std::vector<Eigen::Vector3d> 旋转为刚性变换?

c++ - 'else' 之前的预期主表达式

c++ - C++ 枚举中的枚举

c++ - 如何从调试符号中排除外部依赖项?

c++ - 使用索引将 BST 值存储到数组中

java - 通过Key区分优先级队列堆的时间复杂度