Binary Tree : pre order , inorder and post order - data-structures

can someone tell what is easy way to Traverse, if pre order is given and you have to write post and pre order ? e.g if in order is EACKFHDBG have to traverse in pre and post order?

Just one of the traversals in not sufficient to construct a tree. It will lead to construct many possible trees.
We would need the in-order and one of pre-order/post-order/ level-order together to construct a binary tree. Refer http://www.geeksforgeeks.org/if-you-are-given-two-traversal-sequences-can-you-construct-the-binary-tree/
Example:- in-order and pre-order are given we can print post-order. Refer http://www.geeksforgeeks.org/print-postorder-from-given-inorder-and-preorder-traversals/
Some other data along with in-order can also help in constructin unique binary tree and hence can be used to derive the other traversals. Refer http://www.quora.com/Given-an-in-order-traversal-of-a-special-binary-tree-having-property-that-the-node-is-always-greater-than-its-left-and-right-child-How-can-we-reconstruct-the-tree-efficiently

Related

While serializing BST why is in-order essential?

I learnt that, to retain the structure of a BST while serialiing it, one needs to store in-order and one of either pre-order or post-order notations of the tree.
What makes in-order notation essential?
Note: Rewrote the answer, the previous version was incorrect.
For a general binary tree (with unique elements) your statement would be correct. Consider these two inputs (not very prettily drawn ;-) ):
If you serialize these using in-order traversal, both yield ABC. Similar cases exist for the other traversal types.
So why is a combination of in-order and pre-order enough?
The serialized shape of pre-order is [root][left subtree][right subtree]. The root is easy to identify, but you don't know where the left subtree ends and the right subtree begins.
Now consider in-order serialized: [left subtree][root][right subtree]. You know what the root is (thanks to pre-order), so it is really easy to identify the left and right subtrees.
Note that this is still not enough if the weights are not unique. If in the above example we change B into A, both trees would yield [AAC] for both traversal types.
For binary search trees deserialization is much easier. Why? Well, every subtree has the property that the nodes in the left subtree are smaller than the root, whereas the nodes in the right subtree are bigger. Therefore, the pre-order serialization [root][left subtree][right subtree] can easily and unambiguously be parsed again. So, in conclusion, the person who told you that at least two serialization approaches are needed for a BST was mistaken (maybe he also forgot about the properties of a BST).
Storing BSTs in some sort of order while serializing likely makes it simpler to build upon retrieval. Imagine that you have your BST and just pick nodes at random to serialize and store. When retrieving, it will retrieve in the order stored and then after the fact, something would have to go through and connect all of the nodes. While that should be possible - all the information is there - it seems unnecessary. Each node is just sort of floating; the deserialization process/program has to maintain a list of all the nodes (or similar) while it walks through the list connecting piece by piece.
On the other hand, if you store them in some sort of prescribed order, it can build the tree while reading in each node - it knows where to connect the nodes since they are in order (for clarity: this doesn't imply the next node must be connected to the previously-read node, in the case of adjacent leaves; it's just much simpler to hop up enough levels to the appropriate branch). This should be faster, and potentially use less memory (no list/container while building).

Threaded Binary Search Trees Advantage

An explanation about Threaded Binary Search Trees (skip it if you know them):
We know that in a binary search tree with n nodes, there are n+1 left and right pointers that contain null. In order to use that memory that contain null, we change the binary tree as follows -
for every node z in the tree:
if left[z] = NULL, we put in left[z] the value of tree-predecessor(z) (i.e, a pointer to the node which contains the predecessor key),
if right[z] = NULL, we put in right[z] the value of tree-successor(z) (again, this is a pointer to the node which contains the successor key).
A tree like that is called a threaded binary search tree, and the new links are called threads.
And my question is:
What is the main advatage of Threaded Binary Search Trees (in comparison to "Regular" binary search trees).
A quick search in the web has told me that it helps to implement in-order traversal iteratively, and not recursively.
Is that the only difference? Is there another way we can use the threads?
Is that so meaningful advantage? and if so, why?
Recursive traversal costs O(n) time too, so..
Thank you very much.
Non-recursive in-order scan is a huge advantage. Imagine that somebody asks you to find the value "5" and the four values that follow it. That's difficult using recursion. But if you have a threaded tree then it's easy: do the recursive in-order search to find the value "5", and then follow the threaded links to get the next four values.
Similarly, what if you want the four values that precede a particular value? That's difficult with a recursive traversal, but trivial if you find the item and then walk the threaded links backwards.
The main advantage of Threaded Binary Search Trees over Regular one is in Traversing nature which is more efficient in case of first one as compared to other one.
Recursively traversing means you don't need to implement it with stack or queue .Each node will have pointer which will give inorder successor and predecessor in more efficient way , while implementing traversing in normal BST need stack which is memory exhaustive (as here programming language have to consider implementation of stack) .

Big O of pre order traversal, in order traversal ,post order, level order for tree

Please let me know the Big Oh of the above.
Think about how an algorithm executing these traversals would look like - what data structure would you use (stack, queue, something else?) and how many operations would you need to execute for processing each node in the tree? Would you ever have to process a node in the tree twice at all?

Question on Binary Search Trees

I was thinking of implementing a binary search trees. I have implemented some very basic operations such as search, insert, delete.
Please share your experiences as to what all other operations i could perform on binary search trees, and some real time operations(basic) that is needed every time for any given situation.. I hope my question was clear..
Thanks.
Try a traversal operation (e.g., return the elements in the tree as a List, in order) and make sure that the tree remains balanced when elements are inserted/deleted.
You may want to look at the different ways of returning the tree:
Depth-first (going all the way down a branch and back up, repeat)
In-order (going around the tree)
Level-order (each level as drawn in a diagram)
Returning as a flat array.
And if you're feeling particularly adventurous, take an array and import it in as a tree. There is a specific format for this that goes something like (1(2(3)),(5) - that example isn't balanced but you get the idea, and it's on Wikipedia.
You might also want to implement a rotation operation. A rotation changes the structure without change the order of the elements. This is usually used to balance the tree (to make sure the leaves are all close to the same depth) and can also be used to change the root to a given element if you know it will be showing up in the search more often.
My ASCII art is not great, but a rotation can turn this tree:
f
d g
b e
a c
into this tree:
d
b f
a c e g
The second tree being balanced will make searches for f and g slower, and searches for d,a,b,c faster with e staying the same.
If this is homework, Good luck!
If this is curiousity, have fun!
If you want to implement this in production code without even knowing the basic operations, Don't do it!
http://www.boost.org/doc/libs/1_38_0/boost/graph/detail/array_binary_tree.hpp
At the very least, a binary search tree should have an insert, delete, and search operation. Any other operations will depend on what you intend to do with your tree, although some generic suggestions are: return parent of a given node, find left and right child of a given node, return the root node, preorder, inorder, and postorder traversals, as well as a breadth-first traversal.
If you really just want a list of stuff that might be useful or fun to implement...
Reverse the order of everything in the tree. This is O(N) I think?
Subtree, elements between x and y as a binary search tree themselves -- should be O(log N) I think?
Minimum, maximum? Yeah, trivial but I'm out of ideas!
I think I've seen somewhere "map" operation. When you change all elements of tree with monotonic function. I.e. function with property to always ascend ( f(x+dx) >= f(x) ) or always descend ( f(x+dx) <= f(x) ). In one case you'll need to apply that function to each node in other you'll need also to mirror tree (swap "left" and "right" nodes) because order of resulted values will be reversed.

How do you display a binary search tree?

I'm being asked to display a binary search tree in sorted order. The nodes of the tree contain strings.
I'm not exactly sure what the best way is to attack this problem. Should I be traversing the tree and displaying as I go? Should I flatten the tree into an array and then use a sorting algorithm before I display?
I'm not looking for the actual code, just a guide where to go next.
Check out your options for Tree Traversal, it's easier than you might think. Good luck :)
Is this a binary search tree (BST)? A "just" binary tree (not search) has no properties that would in any way help you (indeed, there may not be any order defined among the payloads!), but for a BST the situation is totally different (indeed the first wikipedia page I pointed to gives concise pseudocode (well, OK, Python;-) for in-order traversal of a BST -- not for just any binary tree of course.
So, did you omit the absolutely-crucial word search between "binary" and "tree" in your question and tag?
Binary Search Tree is a just a tree that you can print as a classic ways (preorder, inorder, preorder)
for example :
print(node){
if(node != null){
printOut(root.value);
print(node.left);
print(node.right);
}
}

Resources