Questions tagged [avl-tree]

Named after its inventors, Adelson-Velskii and Landis, an AVL tree is a self-balancing binary search tree.

Named after its inventors, Adelson-Velskii and Landis, an AVL tree is a self-balancing binary search tree. They were the first dynamically balanced trees to be proposed.

Like red-black trees, they are not perfectly balanced, but pairs of sub-trees differ in height by at most 1, maintaining an O(logn) search time.

842 questions
5
votes
1 answer

Container with fast inserts and index?

I am looking for a C++ container class that is indexed like an std::vector, but has fast insertions, deletions and indexing. For example, a vector interface implemented with an underlying balancing tree would have O(logN) insertions/deletions and…
Max
  • 3,214
  • 2
  • 23
  • 25
5
votes
2 answers

More than one rotation needed to balance an AVL Tree?

My best guess is that one rotation is always enough to balance an AVL tree when you insert or delete ONE element from an already balanced AVL tree. Is one rotation always enough? An example will help where more than one rotations are needed. PS: I…
kBisla
  • 550
  • 2
  • 6
  • 22
5
votes
2 answers

AVL TREE in c++

I have a problem with this very simple block of code. please give me your advice . (My this problem is solved, and in solving this problem the person having id stakx really helped me, the only problem was that i was using stack< treeNode >, when i…
Zia ur Rahman
  • 1,369
  • 4
  • 18
  • 42
5
votes
1 answer

Balancing an AVL Tree haskell

I am creating an AVL Tree in Haskell but I am unsure of how to balance the tree. I can add elements in but they aren't balanced. like using the addList method I add in [4,2,1,3,6,8] adds it in as: The layout for: Root 4 (Root 2 (Root 1 Empty Empty)…
Macken101
  • 63
  • 1
  • 8
5
votes
2 answers

Implement balanced factor with only 1 extra bit per node

In the book Introduction To Algorithms - A Creative Approach, Question 4.18: The AVL algorithms that were presented in Section 4.3.4 require balanced factors with three possible values: 1, 0, or -1. To represent three values we need 2 bits. Suggest…
Guocheng
  • 523
  • 3
  • 14
5
votes
1 answer

Prove maximum number of rotations for two trees to become equal

In the book Introduction To Algorithms - A Creative Approach, Question 4.24: Let T1, and T2 be two arbitrary trees, each having n nodes. Prove that it is sufficient to apply at most 2n rotations to T1, so that it becomes equal to T2. For binary…
Guocheng
  • 523
  • 3
  • 14
5
votes
1 answer

Why does the AVL tree in Map of OCaml use balance factor (height diff) 2 instead of 1?

According to AVL tree wiki The balance factor is calculated as follows: balanceFactor = height(left-subtree) - height(right-subtree). For each node checked, if the balance factor remains −1, 0, or +1 then no rotations are necessary. However, in…
Jackson Tale
  • 23,820
  • 29
  • 135
  • 251
5
votes
1 answer

Why does this AVL tree implementation pack bits into pointers in 64-bit but not 32-bit implementations?

In this AVL tree implementation from Solaris, struct avl_node is defined in an obvious way if compiling for 32-bit library. But for 64-library a pointer to node's parent is packed into "avl_pcb". And it looks like only 61 bits of a ponter are…
user933161
5
votes
1 answer

Deletion in AVL Tree

As you know how avl should be balanced after deletion of a node, I'll get to point. For starting, Im considering deleting a node with no children. For Example a Tree: 10 / \ 5 17 / \ / \ 2 9 12 20 \ …
InamTaj
  • 256
  • 1
  • 5
  • 15
5
votes
3 answers

Remove from AVL tree example code

I am looking into AVL trees and can not seem to find a reference code about removal (either by Googling or from a couple of textbooks I have handy). I am not sure why is this, but do you know of any reference/example of deletion of AVL in java? (I…
Cratylus
  • 49,824
  • 60
  • 195
  • 327
4
votes
1 answer

Delete all entries with a given value in AVL Tree

I am implementing an AVL Tree of the following type AVLTree,V> basically every node of the Tree is a Key-Value Node, at the moment i am struggling with a method that deletes all the V values. This is what i have: private void…
Andrea Dattero
  • 503
  • 4
  • 20
4
votes
3 answers

AVL trees balancing

Given an AVL tree below: 23 / \ 19 35 / \ / \ 8 20 27 40 / 38 / 36 Is it ok to just do a single rotation at 40, to the right? Making it…
Mark
  • 591
  • 1
  • 8
  • 23
4
votes
1 answer

How to improve the efficiency of the function that finds the number of items in a range from AVL tree?

I'm writing a function that finds out the total number of items in a AVL tree by range. For example, the arguments that passed in is "ab" and "au", then I need to find out how many items they are in an AVL tree is in that range. Currently my way of…
4
votes
1 answer

how to rebalance a random binary search tree

Here is the situation: There's a balanced binary search tree which may be access by tens of threads. So when I need to insert or delete a node, I don't want to lock the whole tree due to the concurrency. as time goes it becomes not balanced again.…
4
votes
2 answers

"Rotating" to get AVL Tree

Why is the process of balancing to get an AVL tree called rotation? (While you are at it, what's single & double rotation?) Every textbook of mine blatantly uses that word without any explanation.
user191776