Why convert BST to DLL? - data-structures

conversion of Binary Search Tree to Doubly Linked List.
What is real world application or significance of this conversion?

Probably, the interviewer was trying to see if you understood what a BST and a linked list are, and also if you understood that if you do an inorder traversal of the BST, you'll get a list of the items in order. This exercise is a pretty good test of your understanding of BST traversal and linked list construction.
Building a linked list from a BST does have real-world use. For example, if you have a BST and you want to store it to disk or send it across the wire, it's easier to store or send as an ordered list of nodes. So you have to create that ordered list from the BST. In a language like C that doesn't have a dynamic list structure (like a C++ vector or a C# List<T> or a Java ArrayList), then the linked list is the tool of choice for building an arbitrarily-sized list.
Come to think of it, you could do this in-place. That is, you wouldn't need any extra memory except for the recursion stack. That would be very useful if you're working with a very large tree in a memory constrained environment such as an embedded system.
So, yes, there are real world uses for that technique.

Related

Iterating over classes in a disjoint set data structure

I've implemented a disjoint set data structure for my program and I realized I need to iterate over all equivalence classes.
Searching the web, I didn't find any useful information on the best way to implement that or how it influences complexity. I'm quite surprised since it seems like something that would be needed quite often.
Is there a standard way of doing this? I'm thinking about using a linked list (I use C so I plan to store some pointers in the top element of each equivalence class) and updating it on each union operation. Is there a better way?
You can store pointers to top elements in hash-based set or in any balanced binary search tree. You only need to delete and add elements - both operations in these structures run in O(1)* and in O(logN) respectively. In linked list they run in O(N).
Your proposal seems very reasonable. If you thread a doubly-linked list through the representatives, you can splice out an element from the representatives list in time O(1) and then walk the list each time you need to list representatives.
#ardenit has mentioned that you can also use an external hash table or BST to store the representatives. That's certainly simpler to code up, though I suspect it won't be as fast as just threading a linked list through the items.

Beneftis of Hybrid Data Structures on Efficiency

I have this homework assignment in my Computer Science class that involves combining different data structures for apparent increased efficiency
TL;DR --- Scroll Down
""""Build a data structure which behaves like a linked list with a binary tree as an indexing structure. It should be able to be used as a linked list and inherited from to construct indexed queues and indexed stacks. You may assume that all things that will be put into this data structure are Comparable, so that the indexing tree will function as a binary search tree. You should build a class of iterators to facilitate interaction with this data structure. Insertion into the list can be done 'after' a location specified by a list iterator (which could sometimes be returned by a find method). Naturally, in an inherited indexed queue, insertion will only be at the back of the queue, however the indexing via the tree will need to preserve the binary search tree ordering, and similarly for an inherited indexed stack. You should have methods to insert and delete, and methods to find (returning an iterator) and sort (any sorting technique will suffice for this question, though you might well want to take advantage of the inherent ordering information derived from the tree!!). Test this structure using a main method which plays with people (perhaps compared via height?).""""
TL;DR --- What are the benefits of having Binary Search Tree nodes containing the same Objects as doubly linked list nodes?
Also, how would inheritance work with such a list?
What are the benefits of having Binary Search Tree nodes containing the same Objects as doubly linked list nodes?
Perhaps a better way of asking the same question would be "what are the benefits of connecting the nodes of a Binary Search Tree (BST) with additional links to construct a linked list out of the same nodes?"
The benefit of adding an extra link is an ability to iterate over the entire tree using O(1) memory. Without this additional link you would need O(Log(N)) memory to iterate the tree, because you would need to keep the position at each level.
The "payment" for this is the use of additional O(N) blocks of memory for the links, and a somewhat more complex algorithm for maintaining the data structure. This may be a fair deal when you iterate the same tree a significant number of times, while insertions and modifications are generally rare.
How would inheritance work with such a list?
Rather than inheriting from a list and also from a tree, you would implement interfaces for the list and for the tree.

can we construct a binary tree with singly linked list

I have googled a lot and none of them showed how to create a binary tree with singly linked list.
Is it even possible to create one ?! I remember that I Have read somewhere that Binary trees can be created using singly linked list.
You can represent a binary tree as an array. If the only direction you want to go in your tree is root-to-leaf, then you could, in theory, use a singly linked list instead of the array.
This would result, however, in a huge performance loss as you will have to go pointer chasing instead of just jumping directly to the next node, as you do in an array.
I find it hard to think of a scenario where you would actually do that, but its possible in principle.

Binary Tree's usage

Can someone give me a real life example ( in programming, C#) of needing to use a Binary Tree or even just an ordinary tree?
I understand the principle of a Binary Tree and how they work, but I'm trying to find some real life example's of their usage?
Tony
In C#, Java, Python, C++ (using the STL) and other high-level languages, most of the time you will use one of the built-in/library-included types to store your data, at least the data you work on at the moment, so most of the time you won't be using a binary tree or another kind of tree explicitly.
This being said, some of these built-in types are implemented as trees of one kind or another "in the backstage", and in some situations you will have to implement one yourself.
Also, a related thing you HAVE to know is binary search. This is mostly done in binary trees (binary search trees :P) but the idea can be extrapolated to a lot of problems, even without trees involved, so try understand it well.
Edit: Real life classical example:
Imagine that you want to search for the phone number of a particular person in the phone guide of a big city. All things being equal, you will open it roughly at the middle, look for the guys in that page, and see if your "target" is before or after it, thus cutting the data by half. Then you repeat the operation in the half where you know your "target" is, and again and again until you found your "target". As each time you are looking into half the data you had before, you require a total of log(base 2) n operations to reach your "target", where n is the total size of the data.
So in a 1 million phone book, you find your target in log(base 2) 1 million = 20 comparisons, instead of comparing one by one as in a linear search (that's 1 million comparisons in the worst case).
Note that this only work in already sorted data.
Balanced binary trees, storing data maintained in sorted order, are used to achieve O(log(n)) lookup, delete, and insert times. "Balanced" just means there is a bounded limit between the depth of the shallowest and deepest leaves, counting empty left/right nodes as leaves. (optimally the depth of left and right subtrees differs at most by one, some implementations relax this to make the algorithms simpler)
You can use an array, rather than a tree, in sorted order with binary search to achieve O(log(n)) lookup time, but then the insert/delete times are O(n).
Some trees (notably B-trees for databases) use more than 2 branches per node, to widen the tree and reduce the maximum depth (which determines search times).
I can't think of a reason to use binary trees that are not maintained in sorted order (a point that has not been mentioned in most of the answers here), but maybe there's some application for this. Besides the sorted binary balanced tree, anything with hierarchy (as other answerers have mentioned, XML or directory structures) is a good application for trees, whether binary or not.
edit: re: unsorted binary trees: I just remembered that LISP and Scheme both make heavy use of unbalanced binary trees. The cons function takes two arguments (e.g. (define c (cons a b)) ) and returns a tree node whose branches are the two arguments. The car function takes such a tree node and returns the first argument given to cons. The cdr function is similar but returns the second argument to cons. Finally nil represents a null object. These are the primitives used to make all data structures in LISP and Scheme. Lists are implemented using an extreme unbalanced binary tree. The list containing literal elements 'Alabama, 'Alaska, 'Arizona, and 'Arkansas can be constructed explicitly as
(cons 'Alabama (cons 'Alaska (cons 'Arizona (cons 'Arkansas nil))))
and can be traversed using car and cdr (where car is used to get the head of the list and cdr is used to get the sublist excluding the list head). This is how Scheme works, I think LISP is the same or very similar. More complicated data structures, like binary trees (which need 3 members per node: two to hold the left and right nodes, and a third to hold the node value) or trees containing more than two branches per node can be constructed using a list to implement each node.
How about the directory structure in Unix. For instance the du command i.e. the disk usage command does a post order traversal (traversal order:: left child -> right child -> root node) of a tree representing the directory structure in order to fetch the disk space used by that directory.
The following slides should help.
http://www.cse.unt.edu/~rada/CSCE3110/Lectures/Trees.ppt
cheers
In Java, trees are used to implement certain sorted data structures, such as the TreeSet:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeSet.html
They are used for data structures where you want the order to be based on some property of the elements, rather than on insertion order.
Here are some examples:
The in-memory representation of a parsed program or expression is a tree. In the case of expressions (excluding ternary operators) the tree will be binary.
The components of a GUI are organized as a tree.
Any "containment" hierarchy can be represented as a tree. (HTML, XML and SGML are examples.
And of course, binary (and n-ary) trees can be used to represent indexes, maps, sets and other "generic" data structures.
An easy example is searching. If you store your list data in a tree, for example, you get O(log(n)) lookup times. A standard array implementation of a list would achieve O(n) lookup time.
XML, HTML (and SGML) documents are trees.

Self-sorted data structure with random access

I need to implement self-sorted data structure with random access. Any ideas?
A self sorted data structure can be binary search trees. If you want a self sorted data structure and a self balanced one. AVL tree is the way to go. Retrieval time will be O(lgn) for random access.
Maintaining a sorted list and accessing it arbitrarily requires at least O(lgN) / operation. So, look for AVL, red-black trees, treaps or any other similar data structure and enrich them to support random indexing. I suggest treaps since they are the easiest to understand/implement.
One way to enrich the treap tree is to keep in each node the count of nodes in the subtree rooted at that node. You'll have to update the count when you modify the tree (eg: insertion/deletion).
I'm not too much involved lately with data structures implementation. Probably this answer is not an answer at all... you should see "Introduction to algorithms" written by Thomas Cormen. That book has many "recipes" with explanations about the inner workings of many data structures.
On the other hand you have to take into account how much time do you want to spend writing an algorithm, the size of the input and the if there is an actual necessity of an special kind of datastructure.
I see one thing missing from the answers here, the Skiplist
https://en.wikipedia.org/wiki/Skip_list
You get order automatically, there is a probabilistic element to search and creation.
Fits the question no worse than binary trees.
Self sorting is a little bit to ambigious. First of all
What kind of data structure?
There are a lot of different data structures out there, such as:
Linked list
Double linked list
Binary tree
Hash set / map
Stack
Heap
And many more and each of them behave differently than others and have their benefits of course.
Now, not all of them could or should be self-sorting, such as the Stack, it would be weird if that one were self-sorting.
However, the Linked List and the Binary Tree could be self sorting, and for this you could sort it in different ways and on different times.
For Linked Lists
I would preffere Insertion sort for this, you can read various good articles about this on both wikis and other places. I like the pasted link though. Look at it and try to understand the concept.
If you want to sort after it is inserted, i.e. on random times, well then you can just implement a sorting algororithm different than insertion sort maybe, bubblesort or maybe quicksort, I would avoid bubblesort though, it's a lot slower! But easier to gasp the mind around.
Random Access
Random is always something thats being discusses around so have a read about how to perform good randomization and you will be on your way, if you have a linked list and have a "getAt"-method, you could just randomize an index between 0 and n and get the item at that index.

Resources