Height difference between leaves in an AVL tree - data-structures

What is the maximum difference between any two leaves in an AVL tree? If I take an example, my tree becomes unbalanced, if the height difference is more than 2(for any two leaves), but the answer is the difference can be any value. I really don't understand, how this is possible.Can anyone explain with examples?

The difference in levels of any two leaves can be any value! Definition of AVL describes height difference only on two sub-trees from one node.
So you need to fill subtrees with equal height then add new nodes just to create that single node difference. But nobody said that that subtree doesn't contain some subtrees with the exact same definition. Of course tree is selfbalanced but if we'll be that accurate to not touch it's balance then we can create any height difference between some leaves.
Example with leaf 24 on level 3 and leaf 10 on level 6:

According to the explanation in this Wikipedia article, the balancing operations in an AVL tree successfully aim at rearranging the tree such that the height of any two leaves differs no more than one. This is the key property of the data structure which makes the retrieval of nodes efficient (namely logarithmic in the number of nodes of the tree, as a path from the root to a leaf is traversed in the worst case).

Related

What is the number of nodes at a particular level in a balanced binary search tree?

I was asked this question in a phone screen interview and I was not able to answer it. For example, in a BST, I know that the maximum number of nodes is given by 2^h (assuming the root node at height = 0)
I wanted to ask, is there a similar mathematical outcome for a balanced binary search tree as well (For AVL, Red Black trees?), i.e. the number of nodes at a particular level k.
Thanks!
A balanced binary tree starts with one node, which has two descendants. Each of those then has two descendants again. So there will be 1, 2, 4, 8 and so on nodes per level.
As a formula you can use 2^(level-1). The last row might not be completely full, so it can have less elements.
As the balancing step is costly, implementations usually do not rebalance after every mutation of the tree. They will rather apply a heuristic to find out when a rebalancing will make the most sense. So in practice, levels might have less nodes than if the tree were perfectly balanced and there might be additional levels from nodes being inserted in the wrong places.

AVL Tree height as a function of nodes

I am trying to find the way to find out the height of the AVL tree as a function of his nodes.
I want to know if it is possible to make an AVL Tree at the height of 4 with exactly 11 nodes. I know that the upper bound of height of an AVL tree which is approximately 1.44*logn. So if I have 11 nodes it is actually 4.32. And yet, I am trying to built one with height 4 for at least 2 hours and fail to do so every time.
Build full binary tree of height 4 with 15 nodes.
Remove ANY four nodes from the last level. Now it is valid AVL tree (the heights of the two child subtrees of any node differ by at most one). Note that it's impossible to remove a node from 3rd level (together with childs, of course) and preserve AVL balance criteria.
One variant (from wiki):

Running time for binary search tree

The textbook says the number of split operations is bounded by the height of the tree, which is O(logn).
I dont quite understand why it is bounded by the height of the tree? Can someone explain that?
When you start at the root, and go as far as you can down some path towards the bottom, the maximum number of nodes you can come across is equal to the height of the tree (this should be easy to see and it is, pretty much by definition, the height of the tree).
Now when you're searching in a binary search tree, you start at the root, and, at each step, you look at the current node, and stop, go left or go right (going left or going right can be considered a split operation). This process involves the same number of nodes as the one described above (going from the root down some path), which involves encountering a number of nodes, and thus split operations, no more than the height of the tree.
Also note that the height of the tree is only O(log n) if the tree is balanced (see this page for more).
Most probably, in the textbook you are referring to, the data structure in question in a balanced binary tree with n nodes. Since it is balanced, its height is log(n). Detailed definitions and brief explanations converning the height ca be found here.

Complete binary tree definitions

I have some questions on binary trees:
Wikipedia states that a binary tree is complete when "A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible." What does the last "as far left as possible" passage mean?
A well-formed binary tree is said to be "height-balanced" if (1) it is empty, or (2) its left and right children are height-balanced and the height of the left tree is within 1 of the height of the right tree, taken from How to determine if binary tree is balanced?, is this correct or there's "jitter" on the 1-value? I read on the answer I linked that there could be also a difference factor of 4 between the height of the right and the left tree
Do the complete and height-balanced definitions just apply to binary tree or just any other tree?
Following the reference of the definition in wikipedia, I got to
this page. The definition was taken from there but modified:
Definition: A binary tree in which every level, except possibly the deepest, is completely filled. At depth n, the height of the
tree, all nodes must be as far left as possible.
It continues with a note below though,
A complete binary tree has 2k nodes at every depth k < n and between 2n and 2^(n+1) - 1 nodes altogether.
Sometimes, definitions vary according to convenience (be useful for something). That passage might be a variation which, as I understand, requires leaf nodes to fill first the left side of the deepest level (that is, fill from left to right). The definition that I usually found is exactly as described above but without that
passage.
Usually the definition taken for height-balanced tree is the one you
described. In other words:
A tree is balanced if and only if for every node the heights of its two subtrees differ by at most 1.
That definition was taken from here. Again, sometimes definitions are made more flexible to serve specific purposes. For example, the definition of an AVL tree says that
In an AVL tree, the heights of the two child subtrees of any node
differ by at most one
Still, I remember once I had to rewrite an algorithm so that the tree
would be considered height-balanced if the two child subtrees of any
node differed by at most 2. Note that the definition you gave is recursive, this is very common for binary trees.
In a tree whose number of children is variable, you wouldn't be able to say that it is complete (any parent could have the number of children that you want). Still, it can apply to n-ary trees (with a fixed amount of n children).
Do the complete and height-balanced definitions just apply to binary
tree or just any other tree?
Short answer: Yes, it can be extended to any n-ary tree.

AVL tree - Why is it a must that the heights of the left and right children of some node may differ by 1?

What's wrong with the heights of the left and right children of some node differ being by 2?
This is my first encounter with AVL trees, and I can't seem to understand why it is a must?
Really, what's wrong with the children being different by 2?
Regards
By definition, a balanced binary tree can only differ by one. If you look at the algorithms that operate on the AVL trees, you'll see that this property is always maintained.
While it's possible to make some sort of data structure where the height differs by +- 2 at most, there is not real benefit in doing so. By leaving it as +- 1, you create a simpler, self-balancing data structure.
Well that's the concept of an AVL tree, the height of left and right childs must not differ by at most one.
From Wikipedia
In an AVL tree, the heights of the two child subtrees of any node
differ by at most one.
Since it's balanced, this make the search 0(logn), so it's faster than a non-balanced binary tree, where all the elements could be on the left, making it 0(n)

Resources