Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rank Tree in C++

We need ADT having search and rank features. That is, in addition to the interface of STL map, a function 'int get_rank(key)' is required.

Standard implementation of such function requires supporting and updating an extra integer field in every node of self-balanced searching tree (e.g., in black-red tree, used in STL map/set). But it seems, STL map/set do not do this.

We're looking for a solution based on standard containers (STL, Boost) having the best possible Time Complexity: finding/adding/erasing an element take O(log n) (like in STL map/set), computing the rank by a key takes also O(log n).

By the rank of an element, we mean the position of the element in the sorted sequence of all the elements of the map/set.

Example. set = {0, 4, 6, 7, 8} rank(0)=1, rank(4)=2, rank(6)=3, rank(7)=4, rank(8)=5.

In our opinion, under Time Complexity constrains above, the problem cannot be solved by a combination of two maps one sorting by key and other sorting by rank.

Thanks.

like image 304
Slava Avatar asked Feb 18 '10 16:02

Slava


People also ask

What is rank of tree?

The rank of a node value in a tree is the number of the nodes whose values are . The nodes can be of any data type as long as it comes with an ordering relation . For example, the rank of in the following tree is : So, we have a value and the root of a tree, and the goal is to find the. 's rank in it.

What is rank of a tree node?

The rank of a node is its position in a sorted list of the nodes. Solution: We keep the number of children in a node's right and left subtree (denoted as R(n) and L(n)) as an augmentation. To find the rank of a node x, we begin with r = 0. We begin at the root node and traverse the tree to find x.

What is rank in AVL tree?

AVL trees are rank- balanced trees. The rank, r(v), of each node, v, is its height. Rank-balance rule: An AVL Tree is a binary search tree such that for every internal node v of T, the heights (ranks) of the children of v can differ by at most 1. Fact: The height of an AVL tree storing n keys is O(log n).

How can I check my rank in BST?

the rank of a right child = rank of the parent + 1 + number of elements in its left subtree. It can be used to find any general ith order statistic in the BST in O(h) time, i.e. O(log n) time if the tree is balanced. So it is useful to find the median of the elements or ith largest/smallest element among the elements.


3 Answers

The rank of the given key K is the number of keys which are less or equal to K.

E.g., let set s = {1, 3, 4, 6, 9}. Then rank(1) = 1, rank(4) = 3, rank(9) = 5.

The STL function distance() can be used for computing the rank of an element x appearing in the set s.

rank = distance(s.begin(), s.find(x));

The problem is that its time complexity is O(n).

Note that proposed two maps (or sets) indexed by key and by rank is not correct solution. The problem is that a change of one element affects ranks of many others. E.g., adding element 0 to the set s above change the ranks of all existing elements: s' = {0, 1, 3, 4, 6, 9}. rank(1) = 2, rank(4) = 4, rank(9) = 6.

Thanks.

like image 192
Slava Avatar answered Sep 22 '22 04:09

Slava


I've implemented a "ranked red-black tree" which is similar to a red-black tree except each node stores the distance from the node that precedes it via an in-order traversal, rather than storing a key.

This does exactly what you want, except the "rank" of the first node is 0 and not 1 (you can add/subtract 1 if needed).

My solution is PUBLIC DOMAIN and is based on a public domain tutorial for a regular red-black tree. All operations -- including insert, remove, find, and determine rank have logarithmic time with respect to the number of elements in the data structure.

You can find it here: http://code.google.com/p/options/downloads/list

You should get the latest version from the above link, currently (as of this writing) rrb_v4_release.cpp.

like image 21
Willow Schlanger Avatar answered Sep 26 '22 04:09

Willow Schlanger


you can use some other map like containers .
keep a size fields can make binary search tree easy to random access .
here is my implementation ...
std style , random access iterator ...
size balanced tree ...
https://github.com/mm304321141/zzz_lib/blob/master/sbtree.h
and B+tree ...
https://github.com/mm304321141/zzz_lib/blob/master/bpptree.h

like image 22
奏之章 Avatar answered Sep 25 '22 04:09

奏之章