I was going through this leetcode problem.
https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/
One solution is to do an inorder traversal. Navigate through it and find the kth smallest.
https://www.programcreek.com/2014/07/leetcode-kth-smallest-element-in-a-bst-java/
The other solution I came across is where we pass in a int[]{poss,val} and recursively
However none of this resolve the follow up
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
anyone has any implementation handling this issue. I did see an incomplete example of using custom TreeNode with count but does not fit in since the TreeNode is being passed in.
What changes when implementing lazy deletion into a binary search tree exactly?
Lazy deletion is an option. What this is that you basically have a boolean flag value for each node indicating whether it is deleted or not(but not actually modify structure at all.)
Related
When I am doing some code in the website of hackerranker, there is a problem saying that you must use the heapsort and output the minimum number of an ArrayList given by the console, but you shouldn't destroy the structure of the tree or the heap.
I think it's easy but I don't have an idea of how to solve it? So please help me.
So you have to use heapsort algorithm to sort a ArraryList and then output the minimum element?
The first step is to build a binary-heap that takes the form of a binary tree. And then you should maintain this structure any time you extract an element.
What doesn't make sense is to use heapsort. If you build a min-binary-heap, the root element is already the min element of the Array.
I am trying to implement a binary search tree. But I do not understand how they would be stored within an array. This is the data structure I am storing it in :
private Comparable[] intHeap;
I am still unsure on how to use comparable, please could someone explain the use of this data structure?
How would I inspect the largest and smallest elements of the binary search tree?
If I had the following tree, would it be stored in the intHeap as 1,3,4,6,7,8,10,13,14?
How do you know which node to go to next, if there is multiple node levels?
Thank you in advance.
Judging from the declaration of an array, what you are building is a binary heap - a specialized tree-based data structure, which is typically stored in an array. Unlike tree structures that rely on explicit references, heaps rely on a clever indexing scheme that "packs" a tree into a linear data structure.
Comparable interface lets you build a heap that can store data of arbitrary classes, as long as they implement Comparable interface. This is useful, because you can build reusable code that works with numbers, strings, or objects of your own type.
How do you know which node to go to next, if there is multiple node levels?
If a node is at the index n, its two children would be at indexes 2n + 1 and 2n + 2. If you are at index k and you want to follow the left subtree, set k = 2*k+1; if you want to follow the right subtree, set k = 2*k+2. Continue following the same algorithm until you hit a null element in your intHeap array.
You need to go through a good tutorial.Just to give you heads up,the node will be like this
Class Node
{
int data;//ideally should be generic.
Node leftChild;
Node rightChild;
...
}
Recommend you to visit Binary Search Tree - Java Implementation
I'm trying to write a binary heap implemented over a binary tree, but I'm having trouble finding a way to add a new node to the "bottom" of the heap, i.e. the first null space on the tree in a breadth-first traversal. I've already got a working heapify function, but I can't figure out how to add a new node before heapifying.
I can't seem to think of a consistent algorithm that can find the null space that I can add my node to, every time I think I come up with something, it doesn't work. What do I do?
I have the following directed graph implementation
Nodes nod[]
List<Arcs> arc[]
So the node on the n'th position has all his arcs of the list in position n. Of course, the nodes are organized accordingly, so that I can use Binary Search.
Based on this implementation. I wish to create a DFS algorithm. I know very well the pseudo code, adapting to java shouldn't be a problem.
But my question is the following. In DFS we need to start searching from the "top" node. And thinking about it, i dont have this "top" node. Moreover, I have no idea how to get it.
So I ask, how do i get this top node, considering my implementation?
Thanks for the help.
Expanding on my comment:
Assuming that the arcs are directed (i.e., from the parent node to the child only) you can search all of the nodes for the one with no incoming arcs:
// parent_count is an integer array of the same size as nod[]
for i = 1..n
for each arc in arc[i] (arc going from i to j)
increment parent_count[j]
end
end
for k = 1..n
if parent_count[k] == 0
return k
end
DFS/BFS are very common and general algorithms to travel over the graph, both has no special meaning of top node, you could start DFS from any node. For example, DFS is used in topological sort algorithm and it states that we have to start from nodes with no incoming edges.
Could you please highlight what problem you want to solve? This should help us to find nodes that has to be used as a roots for DFS.
I am working on an algorithm that will store a small list of objects as a sublist of a larger set of objects. the objects are inherently ordered, so an ordered list is required.
The most common operations performed will be, in order of frequency:
retrieving the nth element from the list (for some arbitrary n)
inserting a single to the beginning or end of the list
removing the first or last n elements from the list (for some
arbitrary n)
removing and inserting from the middle will never be done so there is no need to consider the efficiency of that.
My question is what implementation of List is most efficient for this use case in Java (i.e. LinkedList, ArrayList, Vector, etc)? Please defend your answer by explaining the implementation s of the different data structures so that I can make an informed decision.
Thanks.
NOTE
No, this is not a homework question. No, I do not have an army research assistants who can do the work for me.
Based on your first criteria (arbitrary access) you should use an ArrayList. ArrayLists (and arrays in general) provide lookup/retrieval in constant time. In contrast, it takes linear time to look up items in a LinkedList.
For ArrayLists, insertion or deletion at the end is free. It may also be with LinkedLists, but that would be an implementation-specific optimization (it's linear otherwise).
For ArrayLists, insertion or deletion at front requires linear time (with consistent reuse of space, these may become constant depending on implementation). LinkedList operations at front of list are constant.
The last two usage cases somewhat balance each other out, however your most common case definitely suggests array-based storage.
As far as basic implementation details:
ArrayLists are basically just sequential sections of memory. If you know where the beginning is, you can just do a single addition to find the location of any element. Operations at the front are expensive because elements may have to be shifted to make room.
LinkedLists are disjoint in memory and consist of nodes linked to each other (with a reference to the first node). To find the nth node, you have to start at the first node and follow links until you reach the desired node. Operations at the front just require creating a node and updating your start pointer.
I vote for double linked list. http://docs.oracle.com/javase/6/docs/api/java/util/Deque.html
Probably the best data structure for this purpose would be a deque implemented with a dynamic array, which is basically an ArrayList that starts adding elements to the middle of the internal array instead of the beginning. Unfortunately Java's ArrayDeque does not support looking up an nth element.
It is, however, pretty easy to implement one yourself (or lookup an existing implementation), and then all three of the described operations can be done in O(1).
YOu can do all of them with arrayList with minimal confusion if your not worried about efficiency.
i would uses some sort of a queue or stack if i am only inserting at the front or end. They have the least overhead. Or you could also use a linked list.
To remove N elements from the first or end i would use a linked list, you can just delete one node and the ones before or after it are gone. Ie if i delete the first 5 elements just delete the 5th element and the ones before it will disappear. Also if i delete the last 6 elements just delete the 6th to last one and the rest will disappear. And java will do the garbage collecting for you. This would be an order of (1) for this operation.
is this a homework question?
Definitely go for LinkedList. For both inserting a value at the beginning/end of the list and removing the first/last element in the list, it runs in O(1). This is because all that needs to be changed to carry out these operations is a couple of pointers, a minimally costly operation.
Although ArrayLists retrieve the nth element in O(1) while LinkedLists retrieve the nth element in O(n), ArrayLists run the danger of having to adjust their size when elements are inserted. What do you suppose happens when the memory allotted for the ArrayList is used up and you try to insert another element? Well what happens is the ArrayList duplicates itself then allocates more memory (amounting to twice as much as it had initially allocated), a very costly operation. LinkedLists don't have this problem since, again, all that is done is the addition of a pointer.
I don't know a whole lot about Java Vectors, but if they're anything like C++ vectors, then they're very similar to ArrayLists.
I hope this helps.
java.util.TreeMap of Long to Object, and use index of i+tm.firstKey()