This is a conceptual question. I have a tree where the data is stored with strings but not stored alphabetically. How do I search through the entire tree to find the node with string I'm looking for. So far I can only search through one side of the tree.
Here are the thing you can:
1. traverse the tree in any manner, say `DFS` or `BFS`
2. while travering nodes, keep checking the the current node is equivalent to the key string you are searching for.
2.1. compare each character of your search string with each character of current node's value.
2.2. if match found, process your result.
2.3. if not, continue with point 2.
3. if all the nodes exhausted, you don't have any match. stop the algorithm.
The complexity of above mentioned algorithm will be:
O(N)* O(M) => O(NM)
N - nodes of your tree.
M - length of your node's value + length of your search key's value.
You may iterate throught all tree levels and on each of level check all nodes. Depth of the tree is equivalent to numbers of itetations.
You may recursively go down to each branches and stop all itetations when node is found (by using external variable or flag) or if there is no child nodes.
Related
Say that I have a sequence of key values to be inserted into a B-tree of any given order. After insertion of all the elements, I am performing a deletion operation on some of those elements. Does it always give an unique result (in the form of a B-tree) or it can it differ according to the deletion operation?
Quoted from wiki :
link:https://en.wikipedia.org/wiki/B-tree
Deletion from an internal node
Each element in an internal node acts as a separation value for two
subtrees, therefore we need to find a replacement for separation. Note
that the largest element in the left subtree is still less than the
separator. Likewise, the smallest element in the right subtree is
still greater than the separator. Both of those elements are in leaf
nodes, and either one can be the new separator for the two subtrees.
Algorithmically described below:
Choose a new separator (either the largest element in the left subtree or the smallest element in the right subtree), remove it from
the leaf node it is in, and replace the element to be deleted with the
new separator.
The previous step deleted an element (the new separator) from a leaf
node. If that leaf node is now deficient (has fewer than the required
number of nodes), then rebalance the tree starting from the leaf node.
I think according to the deletion operation it may vary because of the above lines quoted in bold letters. Am I right? help :)
If your question is whether two B-trees that contain the exact same collection of key values will always have identical nodes, then the answer is No.
Note that this is also true for e.g. simple binary trees.
However, in the case of B-trees this can be more pronounced because B-trees are optimized for minimizing page changes and thus the need to write back to slow secondary storage.
I've implemented an algorithm to construct a Suffix Tree.
Now, I'm trying to implement a method count that returns the number of times query occurs as a sublist/subinterval of the reference sequence.
What's the best way to do that?
Example:
suffix tree for sequence
1,2,50,100,25,25,25,50,100,25,25
query
25,25
result
3
One approach is:
Add a unique terminating symbol to the list (e.g. -1).
Construct the suffix tree.
Now walk down the suffix tree based on the numbers in the query.
If this is impossible then the query appears 0 times.
Otherwise, count the leaf nodes in the subtree based at your current position.
The number of times the query appears in the string is equal to the number of leaf nodes in the subtree.
If you wish to do several queries then you can use a depth first search to count the number of leaf nodes in O(n) and store the answers in each node. This will then let you perform queries in time O(k) where k is the length of your query string.
This works because your suffix tree will have leaf nodes for each of the suffixes:
1,2,50,100,25,25,25,50,100,25,25
2,50,100,25,25,25,50,100,25,25
50,100,25,25,25,50,100,25,25
100,25,25,25,50,100,25,25
25,25,25,50,100,25,25
25,25,50,100,25,25
25,50,100,25,25
50,100,25,25
100,25,25
25,25
25
of these, after you follow the 25,25 query down the tree, the remaining leaf nodes in the subtree correspond to:
25,25,25,50,100,25,25
25,25,50,100,25,25
25,25
which gives a count of 3 times for the query in the string.
I have a set of items that are supposed to for a balanced binary tree. Each item is of the form (data,parent), data being the useful information and parent being the index of the parent node in the binary tree.
Nodes in the tree are numbered left-to-right, row-by-row, like this:
1
___/ \___
/ \
2 3
_/\_ _/\_
4 5 6 7
These elements come stored in a linked list. How should I order this list such that it's easier for me to build the tree? Each parent node will be referenced (by index) by exactly two child nodes; if I sort these by parent index, the sorting must be stable.
You can sort the list in any stable sort, according to the parent field, in increasing order.
The result will be a list like that:
[(d_1,nil), (d_2,1), (d_3,1) , (d_4,2), (d_5,2), ...(d_i,x), (d_i+1,x) ]
^
the root has no parent...
Note that in this list, since we used a stable sort - for each two pairs (d_i,x), (d_i+1,x) in the sorted list, d_i is the left leaf!
Now, you can populate the tree in breadth-first traversal,
Since it is homework - I still want you to make sure you understand everything by your own. So I do not want to "feed answer". If you have any specific question, please comment - and I will try to edit and explain the relevant parts with more details.
Bonus: The result of this organization is very common way to implement a binary heap structure, which is a complete binary tree, but for performance, we usually store it as an array, which is very similar to the output generated by this approach.
I don't think I understand what exactly are you trying to achieve. You have to write the function that inserts items in the tree. The red-black tree, for example, has the same complexity for insertions, O(log n), no matter how the input data is sorted. Is there a specific implementation that you have to use or a specific speed target that you must reach for inserts?
PS: Sounds like a homework to me :)
It sounds like you want a binary tree that allows you to go from a leaf node to its ancestors, using an array.
Usually sorting a list before putting it into a binary tree causes an unbalanced binary tree, unless you use a treap or other O(logn) datastructure.
The usual way of stashing a (complete) binary tree in an array, is to make node i have two children 2i and 2i+1.
Given this organization (not sorting but organization), you can go to a parent node from a leaf node by dividing the array index by 2 using integer arithmetic which will truncate fractions.
if your binary trees are not always complete, you'll probably be better served by forgetting about using an array, and instead using a more traditional tree structure with pointers/references.
hi i have implemented a bst in mips and i need to print this tree.
Each node has following four information
its value
parent's address
left child's address
right child's address
i should print the tree in the following format.
(x means no child)
12
8-16
x-9 13-17
x-x x-11 x-x x-x
Could you please suggest a way to implement this print method?
The ordering in which you are printing the tree is a breadth-first (level-by-level) traversal. One option would be as follows: maintain a work list, initially seeded with the root of the tree. Then, repeatedly dequeue from the work list, print the current element (or x if none is present), then add the two children to the work list. You would need some way to track when you're done traversing the tree, perhaps by counting the number of nodes first and stopping once you've printed that many nodes.
That said, since you're doing this in MIPS, one simpler option is to linearize the tree into an array, then print the array. If you number the nodes in a fashion similar to how you number the nodes in an implicit binary heap, you can recursively/iteratively walk the tree, fill in the array with the tree nodes, then walk over the array printing everything out.
Hope this helps!
As you need to print level by level of your binary tree, the most obivous way to print the information is to traverse the tree using breadth-first search method.
The rest is straightforward and shouldn't be a problem. :)
Let A[1..n] be an array of real numbers. Design an algorithm to perform any sequence of the following operations:
Add(i,y) -- Add the value y to the ith number.
Partial-sum(i) -- Return the sum of the first i numbers, i.e.
There are no insertions or deletions; the only change is to the values of the numbers. Each operation should take O(logn) steps. You may use one additional array of size n as a work space.
How to design a data structure for above algorithm?
Construct a balanced binary tree with n leaves; stick the elements along the bottom of the tree in their original order.
Augment each node in the tree with "sum of leaves of subtree"; a tree has #leaves-1 nodes so this takes O(n) setup time (which we have).
Querying a partial-sum goes like this: Descend the tree towards the query (leaf) node, but whenever you descend right, add the subtree-sum on the left plus the element you just visited, since those elements are in the sum.
Modifying a value goes like this: Find the query (left) node. Calculate the difference you added. Travel to the root of the tree; as you travel to the root, update each node you visit by adding in the difference (you may need to visit adjacent nodes, depending if you're storing "sum of leaves of subtree" or "sum of left-subtree plus myself" or some variant); the main idea is that you appropriately update all the augmented branch data that needs updating, and that data will be on the root path or adjacent to it.
The two operations take O(log(n)) time (that's the height of a tree), and you do O(1) work at each node.
You can probably use any search tree (e.g. a self-balancing binary search tree might allow for insertions, others for quicker access) but I haven't thought that one through.
You may use Fenwick Tree
See this question