简体   繁体   English

二叉树旋转函数,不包含引用指针,指针到指针或返回新节点的函数

[英]Binary tree rotate function without reference-to-pointer, pointer-to-pointer, or the function returning the new node

I have to make an implementation of the following class in C++: 我必须在C ++中实现以下类的实现:

struct Node {
  Node* parent;
  Node* left;
  Node* right;
  std::string name;
};

class BinaryTree {
 public:
  BinaryTree();
  std::string ToString() const;  
  void RotateLeft(Node* node);
  void RotateRight(Node* node);
  void MakePerfect();
 private:
  void CreateVine();
  void MakePerfectFromVine();
 private:
  Node *root;
};

This class implements the DSW algorithm for making a Binary tree perfect (all levels are full, except the last one) 此类实现DSW算法以使二叉树完美(所有级别均已满,最后一级除外)

I have done an implementation which works, I tested it and everything seem fine but I had to change the rotate functions thus: 我已经完成了一个可行的实现,我对其进行了测试,并且一切似乎都很好,但是我不得不这样更改旋转功能:

  void RotateLeft(Node*& node);
  void RotateRight(Node*& node);

Unfortunately the person who set me this task said that I'm forbidden to change them. 不幸的是,将任务设置给我的人说我禁止更改它们。 But the unaltered functions don't work. 但是未更改的功能不起作用。 I use them in CreateVine() and MakePerfectFromVine() : 我在CreateVine()MakePerfectFromVine()使用它们:

void CreateVine() {
    Node *current = root;
    while(current != NULL)
        if(current->left != NULL){
            current=current->left;
            rotateRight(current);               
        }
        else
            current = current->right;

}

In this fragment the pointer current points to a node in the tree. 在此片段中,指针current指向树中的一个节点。 After the node is rotated, it will have changed its parent and/or left and/or right child, but current will point to the old node with the old information. 旋转节点后,它将更改其父节点和/或左子节点和/或右子节点,但是current节点将指向具有旧信息的旧节点。 In order for the changes made in the rotate function to be reflected outside of the rotate function, I have to use reference-to-pointer, pointer-to-pointer or the function itself has to return the node; 为了使对旋转函数所做的更改反映在旋转函数之外,我必须使用引用指向指针,指针指向指针,否则函数本身必须返回节点;

The only way I can think of to fix this problem is if I know that the binary tree doesn't allow duplicate elements, after I rotate the node I search for its string value and set current to point to it, but I'm not sure if the binary tree doesn't allow duplicate elements and that would increase the time complexity of the algorithm. 解决这个问题的唯一方法是,如果我知道二叉树不允许重复的元素,则在旋转节点后,我将搜索其字符串值并将其设置为current值,但我没有确定二叉树是否不允许重复元素,这会增加算法的时间复杂度。

Is there any way of avoiding the use of reference-to-pointer and pointer-to-pointer? 有什么方法可以避免使用引用指针和指针指向指针?

Excluding the root of the tree, for which you might have to think something else, the pointer to a given node inside the tree can be obtained with an extra level of indirection: 除了可能需要考虑其他问题的树的根以外,还可以通过额外的间接级别获得指向树内给定节点的指针:

Node **pp;
if (node->parent->left == node)
   pp = &node->parent->left;
else
   pp = &node->parent->right;

So you can actually maintain your algorithm and just resolve the reference doing this inside the function. 因此,您实际上可以维护算法,而只需解析函数内部的引用即可 Note that this is a sketch, you will have to check for the root ( parent==0 ) and operate differently there 请注意,这是一个草图,您将必须检查根( parent==0 )并在那里进行不同的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM