Difference between complete binary search tree and AVL tree? - data-structures

Is there any difference between the complete binary search tree and an AVL tree? Give an example.
Searched on google but found this. not much helpful

Every complete binary tree is an AVL tree, but not necessarily the other way around.
A complete binary tree is one where every layer except possibly the last is completely filled in. An AVL tree is one where every node's children are AVL trees whose heights differ by at most one. The maximally skewed AVL trees are the Fibonacci trees, which generally aren't complete trees. Here's an example of a tree that's an AVL tree and not a complete binary tree:
.
/ \
. .
/ \ / \
. . . .
/ / / \
. . . .
/
.

AVL tree and Binary search tree are both same but AVL tree has a constraint that the difference between the height of left sub tree and right sub tree should either be 0, 1 or -1.
If any binary search tree meet these conditions it will be called as AVL tree.
Binary search tree + HEIGHT CONDITION is an AVL tree.
Refer: Introduction to Algorithms by Cormen
https://books.google.co.in/books..

Related

Do Red-Black tree and AVL tree have same balance condition?

For example:
An unbalanced tree:
4
/ \
1 11
/
7
/ \
5 9
After balance, it will become AVL tree:
11
/ \
4 7
/ / \
1 5 9
However, if the unbalanced tree is R-B tree like this:
(\\ means read pointers. \ means black pointers.)
4
/ \\
1 11
/
7
// \\
5 9
Is this a legal R-B tree? Or I should make it balance just like what I did to the tree?
The balance condition of AVL trees is different from the balance condition of Red-Black trees. An AVL tree is, in a sense, more balanced than a Red-Black tree.
In an AVL tree, for every node v, the difference between the height of v's right sub-tree and the height of v's left sub-tree must be at most 1. This is a very strict balance property compared to the one imposed by the Red-Black tree. In a Red-Black tree, no simple path from the root to a leaf is allowed to be more than twice as long as any other. This property results from the five color conditions a binary search tree must satisfy in order to be considered a valid Red-Black tree.
Your example Red-Black tree is indeed not balanced in the AVL sense because the difference between the height of the root's left sub-tree and the height of its right sub-tree is 2. Nevertheless, the tree is balanced in the Red-Black sense as it satisfies the five Red-Black color conditions.
The AVL balance condition implies that the height of an AVL tree is bounded by roughly 1.44log(n) while there's nothing preventing the height of a Red-Black tree from being greater: the Red-Black tree balance condition only implies a bound of 2log(n) on the height.
The fact that AVL trees tend to be shorter than Red-Black trees seems to suggest they must perform better. This is not necessarily the case: an AVL tree is indeed generally faster to search because it's more balanced than a Red-Black tree. But the reason an AVL tree is so well balanced is that keeping it balanced is computationally harder than keeping a Red-Black tree balanced. In particular, the AVL rotations make insertions and deletions in an AVL tree slower in comparison to these operations in the corresponding Red-Black tree.

Splay tree: does an inorder traversal look at nodes in increasing order for a splay tree?

I just finished an exam on this, where I used an inorder traversal to check correct node order in one of my splay trees. Is this valid?
Yes, in-order traversal accesses a splay tree's elements in increasing order.
By the definition of a splay tree in the original article:
The splay tree, (is) a self-adjusting form of binary search tree
So, a splay tree is just a regular binary search tree by structure, which is all that is required for in-order traversal to access elements in increasing order. Apart from that, operations along the search path of a splay tree modify the structure, but they do so in a way which doesn't violate the binary search tree invariants.

Depth first search and binary tree traversal

Strictly speaking, binary tree pre-order traversal is the same as binary tree depth first search. Binary tree in-order and post-order traversals are not the same as binary tree depth first search. Am I right?
All three tree traversals are produced by a depth first search. Whether the current node is added to the output before, after, or between its children doesn't make a difference - the algorithms walk the nodes of the tree in exactly the same order.

Skewed Trees relation to Binary Search Tree

I know what Binary search tree is and I know how they work. But what does it take for it to become a skewed tree? What I mean is, do all nodes have to go on one side? or is there any other combination?
Having a tree in this shape (see below) is the only way to make it a skewed tree? If not, what are other possible skewed trees?
Skewed tree example:
Also, I searched but couldn't find a good solid definition of a skewed tree. Does anyone have a good definition?
Figured out a skewed Tree is the worst case of a tree.
`
The number of permutations of 1, 2, ... n = n!
The number of BST Shapes: (1/n+1)(2n!/n!n!)
The number of skewed trees of 1, 2, ....n = 2^(n-1)
`
Here is an example I was shown:
http://i61.tinypic.com/4gji9u.png
A good definition for a skew tree is a binary tree such that all the nodes except one have one and only one child. (The remaining node has no children.) Another good definition is a binary tree of n nodes such that its depth is n-1.
A binary tree, which is dominated solely by left child nodes or right child nodes, is called a skewed binary tree, more specifically left skewed binary tree, or right skewed binary tree.

Balanced Binary Tree

What is the name of the binary tree (or the family of the binary
trees), that is balanced, and has the minimum number of nodes
possible for its height?
balanced binary tree
(data structure)
Definition: A binary tree where no leaf is more than a certain amount farther from the root than any other. After inserting or deleting a node, the tree may rebalanced with "rotations."
Generalization (I am a kind of ...)
binary tree.
Specialization (... is a kind of me.)
AVL tree, red-black tree, B-tree, balanced binary search tree.
Aggregate child (... is a part of or used in me.)
left rotation, right rotation.
See also BB(α) tree, height-balanced tree.
-- http://www.itl.nist.gov/div897/sqg/dads/HTML/balancedbitr.html
It is called Fibonacci tree
AVL is a balanced tree with log(n) height (this is the lowest height possible for binary tree).
Another implementation of a similar data structure is Red Black Tree.
Both trees implement all operations in O(log(n)).
AVL Tree is something that you have been looking for.
From Wikipedia:
In computer science, an AVL tree is a self-balancing binary search tree, and it is the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; therefore, it is also said to be height-balanced. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations.

Resources