is it possible to create an avl tree given any set of numbers?

Your question is not the right one.

An AVL tree is a binary tree that has additional properties. First it is a search tree, which means we can easily find each number in the tree. Second it is balanced, meaning that there are no leafs very far form the root. (Formal definitions on request.)

Assume you have a set of $n$ numbers in advance, and an arbitrary “empty” binary tree with $n$ nodes, you can make the tree into a search tree putting the nodes at a well defined position. So, If you can find an empty tree with AVL structure it is no problem to fill it with the values. And indeed, a completely balanced binary tree, adding nodes level by level, satisfies the requirement of AVL trees.

The proper question is: “can we keep a binary tree in AVL form when the values are added and deleted one by one“. When we can do this we have build a quite efficient data structure for sets of values.

induction – Every AVL tree can be colored to be a red-black tree

I want to prove any AVL tree can be turnt into a red-black tree by coloring nodes appropriately.
Let $h$ be the height of a subtree of an AVL tree.
It is given that such a coloring is constrained by these cases:

  1. $h$ odd $implies$ black height $=$ $frac{h}{2} + 1$, root node black
  2. $h$ even $implies$ black height $=$ $frac{h+1}{2}$, root node red

After that the root node is colored black.

I’m trying to prove this inductively. Let’s start with the base case $h=1$. Then there is only one node (the root node) and it gets colored black (using case 2) which yields a valid red-black tree.

Now suppose the statement is true for some $h geq 1$. Then for any node $u$ in the AVL tree, the height difference between their children is less than $1$. That is, for an AVL tree of height $h+1$ either both subtrees of the root node have height $h$ or one has height $h-1$.

By the induction hypothesis we know how to color the subtree of height $h$, depending on the parity of $h$. I’m unsure if I should use strong induction instead because it is not given in the hypothesis how to color a subtree of height $h-1$.

If we would know how to color both subtrees, then consider the following cases:

  1. $h+1$ is even
    • one subtree has height $h$, the other height $h-1$
    • both subtrees have height $h$
  2. $h+1$ is odd
    • one subtree has height $h$, the other height $h-1$
    • both subtrees have height $h$

For case 1.1 we would get
quad & h+1 &text{even} \
implies quad & h &text{odd} \
implies quad & text{black height} = frac{h}{2} + 1 \
implies quad & h-1 &text{even} \
implies quad & text{black height} = frac{(h-1)+1}{2} = frac{h}{2}

So their black heights differ by $1$. How would I take that into consideration?

data structures – What exactly is the difference between a Balanced Binary Search Tree and an AVL tree?

There are some implementations of Balanced Binary Search Trees, also known as Self-balancing Binary Search Trees. One of the aspects that can be considered when balancing a tree is it’s subtrees height difference. This height aspect aims to keep the tree as flat as possible. The more flat the tree is, the less levels it will have and the search will be faster.

The implementation that you mentioned reconstructs the entire tree when a new node is inserted, from the root to the leaves. The AVL tree will reorganize (balance) only the subtrees needed to meet its invariant, i.e., the height difference less or equal to one, when a new node is inserted or a existing node is removed.

The pre-order traversal will be different, since these two BBSTs have different implementations! If you test, maybe a red-black tree (another BBST), the preorder will be different too!

The main thing to keep in mind is that the reason of the BBSTs to exist is that they will keep them more flat as possible to speed up the search for nodes. The only traversal that will be equal among them (if they are search trees) is the in-order traversal, that can be also used to sort data.

complexity theory – Worst Case for AVL Tree Balancing after Deletion

After deleting a node in an AVL tree, self-balancing (zig-zag rotation or the left-right balancing) maintains O(logn) time that is not guaranteed in other unbalanced trees (like BST).

The Balancing operation is said to be O(1).

What is the worst case for balancing?

Any specific type of tree providing the worst case?

algorithms – Augmenting AVL tree to calculate sum of subtree

Suggest a way to augment an AVL tree to support a $O(log n)$ implementation of the function
calculateSum(key), which receives a key of a node and returns the sum of its subtree.

I implemented it this way:

    if node != null:
        return sumSubtree(node.left) + sumSubtree(node.right) + node.key
    return 0
    node = Search(key) // assuming I have a search function
    return sumSubtree(node)

which solves it in $O(log n)$.

But I read it is possible to maintain the sum during insertion and deletion. And augment an AVL tree this way.

Which solution would be better? Mine, or the other method? Does it matter?

data structures – What is the insertion algorithm for an AVL tree with balance constraint of 2?

Thanks for contributing an answer to Computer Science Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

AVL Tree Dictionary

Sorry in advance if this seems like a homework dump type question but I am writing a code that is an AVL tree and has an inserted file that contains a dictionary. The code should ask the user for a word and the program will print its definition. The problem is, I don't even know where to start. I know for the dictionary I have to use a BufferedReader and I don't know how to insert it into an AVL tree. Any help will be greatly appreciated.

algorithms – Effectively find the min-cost path of an AVL tree

It seems that in a full AVL tree, the left edge is always the path at minimum cost. For example, take the following complete AVL tree:

complete avl tree

The min-cost path would be 8-6-5. However, this is not the case with other AVL trees. Take the previous tree with an extra one 4 inserted:

avl tree

Here, the min-cost path would be 8-6-7 rather than 8-6-5-4.

What is the most efficient algorithm for finding the min-cost path in an AVL tree? Given the characteristics of AVL trees, is this algorithm faster than it would be for a standard BST?

data structures – What is the number of leaves in the AVL tree

How can I prove that the number of leaves in a balanced BST is omega N where N is the number of nodes in the tree?

I somehow tried to prove that an AVL / Fibonacci tree should have omega N leaves by the Fibonacci sequence and I didn't know how to continue!

Could anyone help?

Data structures – Store the height of an AVL tree node in constant time

From approximately 9:00 am in this conference, Erik Demaine explains how to calculate the height of a node in an AVL tree by performing an operation constant time on his children. He then goes on to talk about a general theorem for this model that would store the pitch in constant time.

1) If you know what Erik is talking about, how is pitch stored in constant time?

2) In a related question, a person asked how to calculate the height of AVL as efficiently as possible (Calculate the height of the AVL tree as efficiently as possible). All but one of the responses agreed that the best way is to $ O ( lg n) $but it is not a constant time. The only answer suggesting to store the height was rejected. Could someone explain why?