Let us consider the following picture
this is a so called range tree. I don't understand one thing, it looks similar to a binary search tree, so if we are inserting elements, we can use the same procedure as during binary search tree insertion. So what is the difference?
I have read a tutorial and guess that it is a varation of kd trees, query search trees (like geometric point searching, and so on) but how to construct it? Like binary search tree or does it need additional parameters? Maybe like this
struct range
{
int lowerbound;
int upperbound,
int element;
};
and during insertion we have to check
if(element>lowerbound && element <upperbound)
then insert node
Please help me to understand correctly how to construct a range tree.
In binary search tree when you insert a value you insert a new node.
The range search tree is more similar to binary index tree. These two data structures have fixed structures. When you add / subtract a point to a given range you update the values in the nodes, but do not introduce new nodes.
The construction of this structure is much similar to that of KD-tree: based on the given points you choose the most appropriate points of splitting.
When you learn new data structure always consider the supported operations - this will help you understand the structure faster. In your case it would have helped you distinguish between binary search tree and range tree.
Related
[EDITED!!!] Having read about different data structures and created many of them (in C++) I just wondering how could we make a data structure where each node would be a pair of keys (x,y), where x would refer to a value of a Max Heap and y to a key of a Binary Search Tree. I would like something like a BST and Max Heap at the same time (using tuples or pairs of keys as nodes each time). To make it more clear, technically I mean that in each node i of the tree a pair of keys (x,y) will be stored where x is the priority of the key and y will be the the value of the key.
It will be able to support all the functionalities of the above mentioned data structures, such as the insertion and deletion. For example, regarding deletion the tuple is going to go consecutively deeper by a sequence of simple consecutive rotations, until the tuple be a leaf. Then the deletion is easy as far as you know. If the tuple – the one we would like to delete - would be a leaf or an inner node, the deletion could be done in the same way as in BSTs.
Regarding insertion a tuple will be inserted into the tree only based on the key of the binary search tree. After that, the pair is going to be moved consecutively higher in the tree until the fundamental property of max heaps be violated.
Moreover, I have some extra functionalities into my mind. An additional function could be something like find_second_next(), taking as argument the x key, which is already on the tree, and this function will find the second smaller among all the y keys of the tree which are greater than x.
Another function could be a print_between(k1,k2) as well. This function will print all y keys of the tree having value in the range [k1,k2]. Finally, I would like to have also a print_with_higher_priority(x) function which will print all the x keys of the tree which are greater than x.
If you have some additional functionalities write them! :D
I am looking forward to seeing your contribution to this question!
This C++ assignment requires us to create a binary tree and check to see if it is a binary search tree. If it isn't, then we need an algorithm to repair it WITHOUT using extra space or other data structures. My friends and I are all stuck on figuring out a proper algorithm because tons of searching online has come up with little to nothing. We don't care much about runtime, we are mostly focused on figuring out how to repair a BT with the given requirements.
Linked lists are used to create the trees but we're stumped on how to implement some kind of algorithm to convert it into a BST.
Any tips would be greatly appreciated!
Follow the steps given below:
Check your binary tree T1 is a Binary Search Tree or not. If Yes, no need to worry about, else goto step 2
Do the post-order traversal of the given Binary Tree and create another Tree T2(consider it as Binary Search Tree initially pointing to NULL, means empty initially).
While doing the post-order traversal of the Binary Tree, keep on deleting the nodes one by one and create a copy of each node and insert that node to the BST T2 one by one. (NOTE: The insertion should be done in BST manner)
Complexity:
Worst-Time Complexity: O(n2).
Space Complexity: O(1). constant
You can follow the following steps to convert your binary tree into binary search tree in place:
First we will convert the binary tree into a doubly linked list. Do an inorder traversal and change the left and right pointers (left can be used as next and right as previous) accordingly to form a linked list in place.
Then sort the linked list in place(use merge sort here as it sorts a linked list in place).
Then u can convert this linked list in place back to a tree. Start with root as middle element of linked list and use a recursive function to move left and right accordingly.
This will create a balanced tree.
I need a data structure in c++ STL for performing insertion, searching and retrieval of kth element in log(n)
(Note: k is a variable and not a constant)
I have a class like
class myClass{
int id;
//other variables
};
and my comparator is just based on this id and no two elements will have the same id.
Is there a way to do this using STL or I've to write log(n) functions manually to maintain the array in sorted order at any point of time?
Afaik, there is no such datastructure. Of course, std::set is close to this, but not quite. It is a red black tree. If each node of this red black tree was annotated with the tree weight (the number of nodes in the subtree rooted at this node), then a retrieve(k) query would be possible. As there is no such weight annotation (as it takes valuable memory and makes insert/delete more complex as weights have to be updated), it is impossible to answer such a query efficently with any search tree.
If you want to build such a datastructure, use a conventional search tree implementation (red-black,AVL,B-Tree,...) and add a weight field to each node that counts the number of entries in its subtree. Then searching for the k-th entry is quite simple:
Sketch:
Check the weight of the child nodes, and find the child c which has the largest weight (accumulated from left) that is not greater than k
Subtract from k all weights of children that are left of c.
Descend down to c and call this procedure recursively.
In case of a binary search tree, the algorithm is quite simple since each node only has two children. For a B-tree (which is likely more efficient), you have to account as many children as the node contains.
Of course, you must update the weight on insert/delete: Go up the tree from the insert/delete position and increment/decrement the weight of each node up to the root. Also, you must exchange the weights of nodes when you do rotations (or splits/merges in the B-tree case).
Another idea would be a skip-list where the skips are annotated with the number of elements they skip. But this implementation is not trivial, since you have to update the skip length of each skip above an element that is inserted or deleted, so adjusting a binary search tree is less hassle IMHO.
Edit: I found a C implementation of a 2-3-4 tree (B-tree), check out the links at the bottom of this page: http://www.chiark.greenend.org.uk/~sgtatham/algorithms/cbtree.html
You can not achieve what you want with simple array or any other of the built-in containers. You can use a more advanced data structure for instance a skip list or a modified red-black tree(the backing datastructure of std::set).
You can get the k-th element of an arbitrary array in linear time and if the array is sorted you can do that in constant time, but still the insert will require shifting all the subsequent elements which is linear in the worst case.
As for std::set you will need additional data to be stored at each node to be able to get the k-th element efficiently and unfortunately you can not modify the node structure.
I'm currently implementing a red-black binary search tree for geometric interval search.
I'm saving in the tree segments containing a startpoint and a endpoinit where the startpoint is the key entry to the tree.
My concern is to be able to save into the tree segments which have the duplicate starting point (or if you prefer, which have the same key).
It's a kind of C++ multimap for geometric search.
The solution I came up with is: for each entry who has duplicate keys, save a list (or vector) of segments with corresponding duplicate keys.
The problems I see with this approach are twofold:
1. It will reduces the efficiency of the search if there are a large number of duplicate keys.
2. I'll have to use more memory to store the duplicate keys.
My question is: is there another way to implement this more efficiently?
I'd recommend not to use a normal binary search tree! Instead, have a look at interval trees: I suspect this structure suits you needs better.
Binary tree http://img9.imageshack.us/img9/9981/binarytree.jpg
What would be the best way to serialize a given binary tree and inturn evaluate a unique id for each serialized binary tree?
For example, I need to serialize the sub-tree (2,7,(5,6,11)) and generate a unique id 'x' representing that sub-tree so that whenever I come across a similar sub-tree (2,7,(5,6,11)) it would serialize to the same value 'x' and hence I can deduce that I've found a match.
Here we assume that each node has properties that are unique. In the above example, it would be the numbers assigned to each node and hence they would always generate the same ids for similar sub-trees. I'm trying to do this in C++.
Do algorithms already exist to perform such serialized tree matching?
Do you want to to be able match any arbitrary part of the tree or a subtree running upto some leaf node(s)? IIUC, you are looking at suffix matching.
You can also look at Compact Directed Acyclic Word Graph for ideas.
I would make a hash value (in some Rabin-Karp fashion) based on the nodes' IDs and position in the tree, ie:
long h = 0
for each node in sub tree:
h ^= node.id << (node.depth % 30)
in pseudo code. The downside is that different subtrees may yield the same hash value. But the advantage is that it is fast to compare hash values, and when match is found you can further investige the actual sub tree for equality.
If you're not looking for high efficiency, you might want to use a very simple depth-first-search algorithm.
"2,7,2,U,6,5,U,11,U,U,U,5,9,4"
As you can see, i added U commands ("up") so as to show where the next child would be created. Of course you can make this more efficient, but i believe that's a start.
Also, you might want to have a look at Boost.Graph (BGL) for implementation.
What's wrong with the parentheses notation like you used in your question?