Comparison between optimal binary search tree and AVL Trees? - data-structures

I was preparing for my finals and while going through these topics, I got stuck with few queries.
I know that optimal binary Search trees give optimal structure to trees with respect to the frequency of the nodes. The idea is to keep the nodes having higher frequency (and also satisfying the bst properties) at the top for optimal solution.
Whereas Avl are BST which on rotation perform balancing and hence provide a balanced tree but frequencies don't play a role here.
So, my query is if we know that for frequencies of the respective nodes, we come to a optimal bst structure which is of the form :
Eg :
10
\
12
\
14
\
16...And so on
I.e., linked list kind of a structure but it is optimal as frequencies are, for example [15,13,10,4,2..]
But if this is solved with AVL, AVL will balance the tree and form a different structure making it not so optimal for these frequencies.
So, my question is if we know the respective frequencies for the nodes, then applying AVL on such data is not the right choice as compared to optimal binary search tree ?
Is optimal binary search tree a better choice in such situations ?
I would be thankful if anyone can clear this doubt of mine.
Thanks in advance.

Related

Is kd-tree always balanced?

I have used kd-tree algoritham and make tree.
But i found that tree is not balanced so my question is if we used kd-tree algoritham then that tree is always balanced if not then how can we make it balance ?.
We can use another algoritham likes AVL or Red-Black for balancing kd tree ?
I have some sample data for that i used kd-tree algoritham but that tree is not balanced.
(14,31), (15,32), (17,42), (16,44), (18,52), (16,62)
This is a fairly broad topic and the questions themselves are kind of general.
Hopefully this will give you some useful insights and material to work with:
Kd tree is not always balanced.
AVL and Red-Black will not work with K-D Trees, you will have either construct some balanced variant such as K-D-B-tree or use other balancing techniques.
K-d Tree are commonly used to store GeoSpatial data because they let you search over more then one key, contrary to 'traditional' tree which lets you do single dimensional search. GeoSpatial data certainly cannot be represented in single dimension.
Note that there are also specialized databases working with GeoSpatial data so it might be worth checking if the overhead could be shifted to them instead of making your own solution: Although i don't have much experience with this, maybe it is worth checking the postgis.
postgis
Here are some useful links showing how to build balanced K-D tree variant and usage of K-D trees with Spatial data:
balancing K-D-Tree
K-D-B-tree
spatial data k-d-trees
It depends on how you build the tree.
If built as originally published, the tree will be balanced, i.e. only at the leaf level it will have at most a height difference of 1. If your data set has 2^n-1 elements, the tree will be perfectly balanced.
When constructed with the median, then half of the objects must be on either branch of the tree, thus it has minimal height and is balanced.
However, this tree cannot be changed then. I am not aware of an insert or remove algorithm that would preserve this property, but YMMV. I bet there are two dozens of kd-tree extensions that aim at rebalancing and making insertions/deletions more effective.
The k-d-tree is not designed for changes, and will quickly lose efficiency. It relies on the median, and thus any change to the tree would worst-case propagate through all of the tree. Therefore, you need to allow some tolerance in the tree quality to support changes. It appears to be a common approach to just keep track of insertions/deletions and rebuild the tree eventually. You cannot combine it with red-black-trees or AVL-trees, because data with more than 1 dimension is not ordered; these trees only work for ordered data. Upon rotation of the tree the splitting axis changes; and there may be elements in either half that suddenly would need to move to the other branch. This does not happen in AVL or red-black trees.
But as you can imagine, people have published several indexes that remain balanced. Such as k-d-b-trees, and R-trees. These also work better for large data that needs to be stored on disk.
In order to make your kd-tree balanced use median value.
(14,31), (15,32), (17,42), (16,44), (18,52), (16,62)
In the root choose median of x-cordinates [14,15,16,16,17,18] which is 16,
So all the elements less than 16 goes to left part of the tree and
greater than or equal to goes to right side of tree.
as of now,
left part tree consists of [14,31],[15,32] ,now for y-axis find the median for [31,32]
so that the tree is balanced

α-β prune algorithm - what should be the perfect leaf nodes order, which alpha-beta algorithm prunes as many nodes as possible?

I'm just start learning AI algorithms. when it comes across the α-β prune, this algorithm's performance is highly rely on the order of leaf nodes. But according to the textbook, this ordering function is not existing. Based on my research, it appears this issue is related with the even/odd number of leaf nodes, and the depth of the tree. Is this correct? For instance, given a binary tree with depth 4, and 0-15 for the value of leaf nodes, what should be a good ordering of the nodes, so that the algorithm can have maximum performance?
You get maximum performance (=pruning) when the first leaf you get to has the best score. Of course, if you knew which leaf had the best score there'd be no need to do alpha-beta. So it's pretty much a catch-22. In practice, you want to order the immediate children of a node during the search in decreasing estimated score order, where your estimates are the best guess you can come up with without actually performing a search.

what is the meaning of lookup algorithm?

I am a bit confused a term "lookup algorithm of avl trees". When I have searched this in google, I see so many website with related about b-tree not avl tree.
So, Is b-tree algorithm equal lookup algorithm of avl tree ?
If not, what is "lookup algorithm of avl tree" ? Moreover, what is the meaning of "lookup algorithm"? Please give me a link, of course if possible.
b-tree is a data structure - a generalized binary tree.
A lookup algorithm is an algorithm used to lookup values in the data structure. It is how you decide to find items in the data structure.
An avl tree is a type of b-tree (in the abstract).
The lookup algorithm is just the way that you look through the nodes in the tree to find a specific value.
An AVL tree is a self-balancing binary search tree, so the lookup algorithm of an AVL tree is the exact same as for a binary tree.
A B-tree is not the same thing as a binary tree, so it has a different lookup algorithm. The difference is that in a B-tree each node can have several values and more than two children, so the lookup algorithm follows the same basic principle as for a binary tree, but it's a bit more complex.
AVL tree is kind of balancing in the binary tree. B-tree is abbreviation for "Bayer-tree" - a kind of multinode (exceeding 2) tree. So these algorithms are different, since look-up in B-tree takes also look-up over particular page

Why is avl tree faster for searching than red black tree?

I have read it in a couple of places that avl tree search faster, but not able to understand. As I understand :
max height of red-black tree = 2*log(N+1)
height of AVL tree = 1.44*logo(N+1)
Is it because AVL is shorter?
Yes.
The number of steps required to find an item depends on the distance between the item and the root.
Since the AVL tree is packed tighter (i.e. it has a lower max height) it means more items are closer to the root than in the red-black case.
The extra tight packing also means the AVL tree requires more work when inserting elements.
The best choice for any app depends on whether it is insert intensive or search intensive...
AVL tree is better than red-black tree if the input key is almost ascending/descending because then we would have to do single rotation(left-left or right-right case) to add this element. Also, since the tree would be tightly balanced, the search would also be faster.
But for randomly selected input key, RBTree are better since they require less rotation for insertion in comparison to AVL.
Overall, it depends on the input sequence, which would decide how tilted our tree is, and the operation performed.For insert-intensive use Red-Black Tree and for search-intensive use AVL.
AVL tree and RBTree do have respective advantages as well as disadvantages. You'll perceive that better if you've already learned how they work.
AVL is slightly faster than RBTree in insert operation because there would be at most one rotation involved in insertion, while there may be two for RBTree.
RBTree only require at most three rotations in deletion, but this is not guaranteed in AVL. So it can delete nodes faster than AVL.
However, above all, they both have strict logarithmic tree height.
Pick up any subtree, the property that makes AVL "balanced" guarantees that the difference of height between two child subtrees is at most one, which is to say, intuitively, the whole tree is rigidly balanced.
But when it comes to an RBTree, the rule becomes likely "looser", since property of RBTree can only guarantee the depth of a tree is not larger than twice as the logarithm of the total number of nodes.
Here're some facts that may be more precise:
An AVL tree's height is strictly less than: 1.44log(n+2)-0.328
(approximately)
A red-black tree's height is at most 2log(n+1)
See https://en.wikipedia.org/wiki/AVL_tree#Comparison_to_other_structures for detailed information.

Can a non binary tree be tranversed in order?

We are dealing with a Most similar neigthbour algorithm here. Part of the algorithm involves searching in order over a tree.
The thing is that until now, we cant make that tree to be binary.
Is there an analog to in order traversal for non binary trees. Particularly, I think there is, just traversing the nodes from left to right (and processing the parent node only once?")
Any thoughts?
update
This tree will have in each node a small graph of n objects. Each node will have n children (1 per each element in the graph), each of which will be another graph. So its "kind of" a b tree, without all the overflow - underflow mechanics. So I guess the most similar in order traversal would be similar to a btree inorder traversal ?
Thanks in advance.
Yes, but you need to define what the order is. Post and Pre order are identical, but inorder takes a definition of how the branches compare with the nodes.
There is no simple analog of the in-order sequence for trees other than binary trees (actually in-order is a way to get sorted elements from a binary search tree).
You can find more detail in "The art of computer programming" by Knuth, vol. 1, page 336.
If breadth-first search can serve your purpose then you can use that.

Resources