简体   繁体   English

为什么我们要返回指向节点的指针而不是 void function 来实现 avl 树?

[英]Why do we return a pointer to a node instead of void function for avl tree implementation?

Okay so I seem to have gotten a bit lost.好吧,我似乎有点迷路了。 I am trying to insert data into a tree and when checking for balance and whether or not to rotate, I default to checking through the root.我正在尝试将数据插入树中,并且在检查平衡以及是否旋转时,我默认检查根。 When I check examples online, I see that we can also rotate along other nodes in the tree as well.当我在线查看示例时,我发现我们也可以沿着树中的其他节点旋转。 How do we figure out which node to use to balance the tree and how do we reach said node?我们如何确定使用哪个节点来平衡树以及我们如何到达所述节点? I also saw that instead of having void functions to implement the insert and rotation functions, people use a node pointer return type instead.我还看到,人们没有使用 void 函数来实现插入和旋转函数,而是使用节点指针返回类型。 What is the purpose of that?这样做的目的是什么? I know the answers may be super obvious I am just lost.我知道答案可能非常明显,我只是迷路了。

struct TNode{
    int data;
    TNode* left;
    TNode* right;
};
class Tree{
public:
    TNode* root;
public:
    Tree()
    {root = nullptr;}
    int height(TNode* node);
    int balanceFactor(TNode* node);
    bool isBalance(TNode* node)
    {return balanceFactor(node)<-1 && balanceFactor(node)>1?true:false;}
    void avlInsert(int key);
    void LLRotation(TNode* node);
};
int Tree::height(TNode* node){
    int l = 0;
    int r = 0;
    if(!node->left && !node->right)
        return 0;
    if(node->left)
        l = height(node->left) + 1;
    if(node->right)
        r = height(node->right) + 1;
    return l > r ? l : r;
}
int Tree::balanceFactor(TNode *node){
    if(node->left && node->right)
        return height(node->left) - height(node->right);
    else if(node->left && !node->right)
        return height(node);
    else
        return -1 * height(node);
}
void Tree::LLRotation(TNode *node){
    TNode* nl = node->left;
    node->left = nl->right;
    nl->right = node;
    if(root == node)
        root = nl;
}
void Tree::avlInsert(int key){
    TNode* trav;
    TNode* follow;
    TNode* node = new TNode;
    node->data = key;
    node->left = nullptr;
    node->right = nullptr;
    if (!root)
        root = node;
    else{
        trav = root;
        while (trav){
            if (key < trav->data){
                follow = trav;
                trav = trav->left;
            }
            else{
                follow = trav;
                trav = trav->right;
            }
        }
        if(key < follow->data)
            follow->left = node;
        else
            follow->right = node;
    }
    if (balanceFactor(root) == 2 && balanceFactor(root->left) == 1)
               LLRotation(root);
}

It really depends on your implementation some people they want to return a node, using a void function does not change the result much.这真的取决于你的实现,有些人他们想要返回一个节点,使用 void function 并不会改变结果。 For insert function you need to check only after you insert the node so you can go ahead and insert that node than rotate it.对于插入 function ,您只需要在插入节点后检查,以便您可以提前 go 并插入该节点而不是旋转它。 Please note that this function will only work if you have an AVL Tree to start with.请注意,此 function 仅在您有 AVL 树开始时才有效。 Also your code is not correct, below:您的代码也不正确,如下:

if (balanceFactor(root) == 2 && balanceFactor(root->left) == 1)
               LLRotation(root);

This is not how you check it for insertion.... That's for deletion buddy, I think you are getting mixed up.这不是你检查它是否插入的方式......那是删除好友,我认为你搞混了。 Usually for insertion you check if the balanceFactor is between 1 and -1, so if you are getting 2, then it is a left heavy tree, in your case, because it is left-right, which means you should do Right rotation........ Buddy......通常在插入时检查 balanceFactor 是否在 1 和 -1 之间,所以如果你得到 2,那么在你的情况下,它是一个左重树,因为它是左右旋转的,这意味着你应该做右旋转。 ...... 伙伴......

int l = 0;
    int r = 0;
    if(!node->left && !node->right)
        return 0;
    if(node->left)
        l = height(node->left) + 1;
    if(node->right)
        r = height(node->right) + 1;
    return l > r ? l : r;

Height function, extremely inefficient... Instead define height as a variable for node and then modify it after each insertion, deletion.高度function,效率极低...而是将高度定义为节点的变量,然后在每次插入,删除后对其进行修改。

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

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