I already have a working binary tree database. Unfortunately, it needs to have the ability to balance itself. I don't want to rewrite the whole thing, I just want to include a function that will balance the tree. Any algorithms or ideas?
AVL and RedBlack trees are self balancing trees.
You can traverse your original tree and insert the nodes in these trees.
Afterwards you can keep the new tree and discard your original tree.
I have found the Stanford libavl tutorial quite helpful.
Check out the examples in AVL tree wiki.
Also, try to play with the AVL tree animations available in the web, such as
http://www.cs.jhu.edu/~goodrich/dsa/trees/avltree.html or
http://www.strille.net/works/media_technology_projects/avl-tree_2001/ or
http://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.html
AVL and Red-Black trees are balanced binary trees. I have an implementation of AVL trees. Look here. It supports insertion and search. Deletion is not implemented yet.
look for balanced trees like avl red black
Related
I am aware of some of the advantages of using AVL trees such as getting better time complexity to search an element log(number of nodes in tree) since the tree has self-balancing capabilities.
I am curious - what are some of the real time applications of AVL trees? Like in real world, for what applications/use-cases have the AVL trees been used?
So recently I am learning database system on Youtube published by CMU. The professor said skip list is like the rotated B+ tree. And it seems that the time complexity of operations on b+ tree and skip list is nearly the same. Plus, skip list does not need to store intermediate nodes like B+ tree does. However, rare DBMS uses skip list to store indexes of tables. So what are the advantages of B+ tree over skip list?
I would like to implement data structure which is able to make fast insertion and keeping data sorted, without duplicates, after every insert.
I thought about binomial heap, but what I understood about that structure is that it can't tell during insertion that particular element is yet in heap. On the another hand there is AVL tree, which fits perfectly for my case, but honestly there are rather too hard for implement for me, at that moment.
So my question is: is there any possiblity to edit binomial heap insertion algorithm to skip duplicates? Maybe anyoune could suggest another structure?
Grettings :)
In C++, there is std::set. it is internally an implementation of red black tree. So it will sort when you enter data.You can have a look into that for a reference.
A good data structure for this is the red-black tree, which is O(log(n)) for insertion. You said you would like to implement a data structure that does this. A good explanation of how to implement that is given here, as well as an open source usable library.
If you're okay using a library you may take a look at libavl Here
The library implements some other varieties of binary trees as well.
Skip lists are also a possibility if you are concerned with thread safety. Balanced binary search trees will perform more poorly than a skip list in such a case as skip lists require no rebalancing, and skip lists are also inherently sorted like a BST. There is a disadvantage in the amount of memory required (since multiple linked lists are technically used), but theoretically speaking it's a good fit.
You can read more about skip lists in this tutorial.
If you have a truly large number of elements, you might also consider just using a doubly-linked list and sorting the list after all items are inserted. This has the benefit of ease of implementation and insertion time.
You would then need to implement a sorting algorithm. A selection sort or insertion sort would be slower but easier to implement than a mergesort, heapsort, or quicksort algorithm. On the other hand, the latter three are not terribly difficult to implement either. The only thing to be careful about is that you don't overflow the stack since those algorithms are typically implemented using recursion. You could create your own stack implementation (not difficult) and implement them iteratively, pushing and popping values onto your stack as necessary. See Iterative quicksort for an example of what I'm referring to.
if you looking for fast insertion and easy implemantaion why not linked list (single or double).
insertion : push head/ push tail - O(1)
remove: pop head/pop tail - O(1)
the only BUT is "find" will be in O(n)
I have read about Splay tree and I found,there are two methods for constructing the splay tree. They are
Bottom-up
Top-down
So I need to know What is the difference between the two methods and their working ?
A top-down splay tree: performs rotations on the initial access path. Thus a top-down splay tree node does not need a parent link. The splay operation finishes as soon as the search does. That means the overhead for operations of a top-down splay tree is of a relatively small amount.
Bottom-up splay tree: requires a traversal from the root down the tree and then a bottom-up traversal to implement the splaying step, so a bottom-up splay tree implementation looks similar to that of an AVL tree. Besides, it needs a parent link or a stack to store the search path.
An implementation of a top-down splay tree can be found in the data structure book (Chapter 12) written by Weiss.
The methods define how you use splaying when searching:
Bottom-up: you search the tree and rotate at the same iteration
Top-down: you first search and at another iteration you rotate
you can read Splay tree
This ideas hold to the creation also, when using the top down you insert the key as if it was a binary serach tree and than move it to the head in another iteration.
I've implemented Prim's algorithm in C (www.bubblellicious.es/prim.tar.gz) but I was just wondering how to transform this into Kruskal's algorithm.
It seems they're quite similar, but I can't imagine how can I modify my old code into that new one. It'd be delicious if you give some advices or something. I know that's easy, but I'm still a n00b in C programming ...
Why not just write Kruskal's from scratch and see how they compare in your own solutions? Best way to learn.
To convert you need a forest (i.e. a set of trees where initially each node is a tree) as your temporary output structure rather than a single tree. Then on each step, rather than finding the cheapest vertex that adds a currently unconnected node to your tree, you find the cheapest edge in the graph and, if it creates a new tree (i.e. connects two previously unconnected nodes) add that tree to the forest and remove the source trees. Otherwise discard the edge.
A proper implementation of Kruskal is more memory intensive but less time intensive than a proper Prim implementation.
But the differences between the two are quite large. Probably all you can keep between are some helper functions and some data structures. Not a conversion, more a rewrite using more high level building blocks.
Why dont you consider switching to C++ and using the boost graph library
(http://www.boost.org/)?
It contains very well implementations for both algorithms. Type-safe and highly performant.
See kruskal_minimum_spanning_tree and prim_minimum_spanning_tree