简体   繁体   English

BST树到AVL

[英]BST tree to AVL

i was wondering how can i convert a BST to an avl in O(nlogn) time if every node of the BST is: 我想知道如何在BST的每个节点为O(nlogn)的时间内将BST转换为avl:

struct node{
    int leftHeight;
    int rightHeight;
    struct node* lr;
    struct node* rc;
}

Do i have to visit every node of the bst to check if the balance is different than 0,1, -1? 我是否必须访问bst的每个节点以检查余额是否不同于0,1,-1? And if so i have to check if it's a right or left child so that i will perform left or right rotation respectively? 如果是这样,我必须检查它是一个右孩子还是一个左孩子,以便我分别执行左旋转或右旋转? How do i do that? 我怎么做?

I'm using C. Please don't judge me i'm a beginner. 我正在使用C。请不要判断我我是一个初学者。

So you have a Binary Search Tree and you want to convert that to a balanced search tree (AVL tree) and wonder if that can be done in O(n.log(n)). 因此,您有一个二叉搜索树,您想将其转换为平衡搜索树(AVL树),想知道是否可以在O(n.log(n))中完成。

After giving it a quick study, the general approach is to keep the tree balanced while building it. 经过快速研究后,一般的方法是在构建树时保持树的平衡。 The other approach is to create a new tree that is balanced (using the AVL algorithm). 另一种方法是创建一个平衡的新树(使用AVL算法)。

To balance a tree in-place you can use the Stout-Warren algorithm , the reference I found in another Stack Overflow question, Balancing a binary search tree . 要就地平衡树,可以使用Stout-Warren算法 ,该算法是我在另一个Stack Overflow问题中找到的参考,即平衡二分搜索树

The article quoted states its procedure works in linear time, that is O(n) which is less than O(n.log(n)). 引用的文章指出其过程在线性时间内工作,即O(n)小于O(n.log(n))。

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

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