Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用STL队列的二叉树镜像_C++_Binary Tree - Fatal编程技术网

C++ 使用STL队列的二叉树镜像

C++ 使用STL队列的二叉树镜像,c++,binary-tree,C++,Binary Tree,我试图得到一个二叉树的镜像,但由于对指针的了解较少,我在调试代码时遇到了困难。你们能帮我调试代码吗 算法: 使用级别顺序travesal(从右到左)遍历树 然后弹出前节点,创建镜像树 我在做什么 将根目录插入队列,然后运行while(!queue.empty()) 弹出前元素并将其插入镜像树 然后,按“前->右->前->左” 我知道这是一个非常琐碎的问题,但只是为了我对指针的清晰理解,我在这里发布了它。我也无法理解我的编译器在说什么。她的是我试图编译时的快照。 任何帮助都将不胜感激。谢谢 我

我试图得到一个二叉树的镜像,但由于对指针的了解较少,我在调试代码时遇到了困难。你们能帮我调试代码吗

算法:

  • 使用级别顺序travesal(从右到左)遍历树
  • 然后弹出前节点,创建镜像树

  • 我在做什么

  • 将根目录插入队列,然后运行while(!queue.empty())
  • 弹出前元素并将其插入镜像树
  • 然后,按“前->右->前->左”
  • 我知道这是一个非常琐碎的问题,但只是为了我对指针的清晰理解,我在这里发布了它。我也无法理解我的编译器在说什么。她的是我试图编译时的快照。
    任何帮助都将不胜感激。谢谢

    我已经更正了代码。请看看这是否有帮助

    #include<bits/stdc++.h>
    using namespace std;
    
    struct treenode{
      int data;
      struct treenode* left , *right;
    };
    
    
    struct treenode* createNode(int data)
    {
      struct treenode* node = (struct treenode*)malloc(sizeof(struct treenode));
      node->data = data;
      node->right = NULL;
      node->left = NULL;
      return node;
    }
    
    
    //Function for inserting node for creating mirror of binary tree
    void insert(struct treenode **root2,int data)
    {
      struct treenode* temp;
      if(*root2 == NULL)
      {
        *root2 = createNode(data);
        return;
      }
      else{
        queue<treenode*> q;
        q.push(*root2);
        while(!q.empty())
        {
          temp = q.front();
          q.pop();
          if(temp->left)
          q.push(temp->left);
          else{
            temp->left = createNode(data);
            return;
          }
          if(temp->right)
          {
            q.push(temp->right);
          }
          else{
            temp -> right = createNode(data);
            return;
          }
        }
      }
    }
    
    void mirror(struct treenode* node) 
    {
      if (node==NULL) 
        return;  
      else
      {
        struct treenode* temp;
    
        /* do the subtrees */
        mirror(node->left);
        mirror(node->right);
    
        /* swap the pointers in this node */
        temp        = node->left;
        node->left  = node->right;
        node->right = temp;
      }
    } 
    
    //For printing the tree
    void level_order(struct treenode** root)
    {
      if(root==NULL) return;
      queue<treenode*> q;
      q.push(*root);
      while(!q.empty())
      {
        struct treenode* temp = q.front();
        q.pop();
        cout << temp->data << " ";
        if(temp->left)
        q.push(temp->left);
        if(temp->right)
        q.push(temp->right);
      }
    }
    
    //Driver Function
    int main()
    {
      struct treenode* root2 = NULL;
      struct treenode* root = createNode(1);
      root->left = createNode(3);
      root->right = createNode(2);
      root->right->left = createNode(5);
      root->right->right = createNode(4);
      level_order(&root);
      mirror(root);
      printf("\n");
      level_order(&root);
      return 0;
    }
    
    //用于创建二叉树的函数
    //假设原始树为*root,镜像树为*root2
    //在全球范围内声明
    无效插入(整型数据)
    {
    结构树节点*temp;
    if(root2==NULL)
    {
    root2=createNode(数据);
    回来
    }
    否则{
    队列q;
    q、 推(root2);
    而(!q.empty())
    {
    温度=q.前();
    q、 pop();
    如果(临时->左)
    q、 按下(温度->左);
    否则{
    temp->left=createNode(数据);
    回来
    }
    如果(临时->右侧)
    {
    q、 按下(温度->右侧);
    }
    否则{
    temp->right=createNode(数据);
    回来
    }
    }
    }
    }
    //二叉树镜像的迭代版本
    void mirrorBinaryTree()
    {
    结构树节点*前端;
    队列q;
    q、 推(根);
    而(!q.empty())
    {
    front=q.front();
    插入(前端->数据);
    q、 pop();
    如果(前->右)
    q、 推(前->右);
    如果(前->左)
    q、 推(前->左);
    }
    }
    
    我认为递归编码更容易。例如:

    Node * mirror(Node * root)
    {
      if (root == nullptr)
        return nullptr;
    
      Node * r = new Node; // here we will copy the root
      r->key = root->key; // or whatever the root contains
      r->left = mirror(root->right);
      r->right = mirror(root->left);
    
      return r;
    }
    

    当人们发布没有错误的代码时,至少我们可以自己编译它。您希望我们如何处理错误日志和无代码的图片?您的实现有很多技术错误。请看一看这篇文章,代码在@storyteller链接中。我认为如果你以递归的方式提出解决方案,会容易得多。请查看我的答案以了解更多详细信息,是的,但我正在寻找一个迭代版本。谢谢你的建议@你能不能建议我的二叉树逻辑正确与否@另一个托福的逻辑是正确的。只是你处理内存引用的方式不对。thanx@Anoop-Toffy
    //Function for creating Binary Tree
    //Assuming *root for original tree and *root2 for mirror tree
    //are declared globally
    void insert(int data)
    {
    struct treenode* temp;
    if(root2 == NULL)
    {
    root2 = createNode(data);
    return;
    }
    else{
    queue<treenode*> q;
    q.push(root2);
    while(!q.empty())
    {
      temp = q.front();
      q.pop();
      if(temp->left)
      q.push(temp->left);
      else{
        temp->left = createNode(data);
        return;
      }
      if(temp->right)
      {
        q.push(temp->right);
      }
      else{
        temp -> right = createNode(data);
        return;
      }
     }
     }
     }
    //Iterative version for Mirror of a Binary Tree 
    void mirrorBinaryTree()
    {
    struct treenode *front;
    queue<treenode*> q;
    q.push(root);
    while(!q.empty())
    {
      front = q.front();
      insert(front->data);
      q.pop();
      if(front->right)
      q.push(front->right);
      if(front->left)
      q.push(front->left);
    }
    }
    
    Node * mirror(Node * root)
    {
      if (root == nullptr)
        return nullptr;
    
      Node * r = new Node; // here we will copy the root
      r->key = root->key; // or whatever the root contains
      r->left = mirror(root->right);
      r->right = mirror(root->left);
    
      return r;
    }