搜索与-2平衡因子中的一个节点AVL树 [英] Searching a node with balance factor of -2 in an AVL tree

查看:194
本文介绍了搜索与-2平衡因子中的一个节点AVL树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何寻找一个节点与特定的钥匙插入AVL树。但我想知道如何在AVL树-2,平衡因子搜索

下面是code,我都试过了。

 无效searchForUnrequiredBalanceFactor(avlnode * N,avlnode * R)
{
    avlnode * PTR;
    PTR = N;
    如果(PTR == NULL)
        返回;
    其他
    {
        如果(PTR - > balFact == -2)
        {
            R = PTR;
            返回;
        }
        其他
        {
            sea​​rchForUnrequiredBalanceFactor(ptr->左,R);
            sea​​rchForUnrequiredBalanceFactor(ptr->右,R);
        }
    }
}

不过,code不工作的要求,什么在它的问题?

输出:

 节点3的平衡因子:0
节点5的平衡因子:0
节点10的平衡因子:0
节点30的平衡系数:0
节点25的平衡因子:-1
节点20的平衡因子:-2
节点15的平衡因子:-1
* searchForUnrequiredBalanceFactor叫和printf *
节点的数据:0有平衡因子:0


解决方案

假设 avlnode * R 是要存储的发现节点的输出参数,你需要要改变这一行:

  R = PTR;

这样:

  * R = * PTR;

I know how to search a node with a particular key into an AVL tree . But I want to know how to search in an AVL tree with a balance factor of -2

Here is the code that I have tried.

void searchForUnrequiredBalanceFactor(avlnode *n , avlnode *r)
{
    avlnode *ptr ;
    ptr = n ;
    if (ptr==NULL)
        return;
    else
    {
        if (ptr ->balFact == -2)
        {
            r = ptr ;
            return ;
        }
        else
        {
            searchForUnrequiredBalanceFactor(ptr->left,r);
            searchForUnrequiredBalanceFactor(ptr->right,r);
        }
    }
}

But the code isn't working as required , whats the problem in it ??

Output :

balance factor of node 3 : 0 
balance factor of node 5 : 0 
balance factor of node 10 : 0 
balance factor of node 30 : 0 
balance factor of node 25 : -1 
balance factor of node 20 : -2 
balance factor of node 15 : -1 
*searchForUnrequiredBalanceFactor called and printf*
node with data : 0 have balance factor : 0

解决方案

Assuming that avlnode *r is an output parameter where you want to store the found node, you need to change this line:

r = ptr ;

to this:

*r = *ptr ;

这篇关于搜索与-2平衡因子中的一个节点AVL树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆