sorted result of inorder traversal on binary tree - data-structures

I have a question about Binary Trees:
There is a binary tree T1 with n members.
When we run inorder traversal on T1 then we get a series from 1 to n (1,2,3,...n).
Now is T1 a BST (Binary Search Tree)?
I know that if T1 is BST an inorder traversal will result a sorted series but is the oposite direction will work also?

In short yes..
Binary Search property is every node on left tree is lesser and every node on right is greater.
Considering that at any sub tree in your case since we are traversing in-order and saw every element before them is lesser and we are ascending when travelling to the right we have the BST...

This sounds too homeworkish so no direct answer. But:
Assume the root has value k.
Now try this: what does is means for a node to be appearing to the left of k in the in order traversal? On the right?
Also, the numbers appearing before k are all smaller than k. What does that help this question here?

Related

Prove or disprove: If you got pre-order traversal of a binary search tree, you can uniquely determine that binary search tree

This is task I found from old exam and I try it for solved because they can ask similar question for me on friday.
For solution I have cheap solution but I think it all matter of definition of binary search tree.
I make first tree:
1
\
1
\
1
and here is second tree
1
/
1
/
1
When you make pre order traversal you have same output for both tree.. because same element, and both have degenerated tree. But you don't have same tree! So statement is false.
Only problem is are my tree binary search tree... I think yes because binary search tree the element can have greater / lower equal element?
Please halp when I asked our teacher he say I can ask him when vacation is over but when vacation is over my exam is over.... No good thing for me.
Your answer is perfectly fine given the standard definition of BSTs. By the standard definition, BSTs can have repeated elements and identical elements can go in either subtree.
If the question intends the case where there are no duplicates, or where duplicates must exist in only the left (or only the right) subtree, then a pre-order traversal is sufficient to reconstruct the tree.
If duplicates are not allowed, construct the tree recursively as follows: make the root the first node, and then make the left and right subtrees recursively using as input traversals the portions of the original traversal with nodes less than (for left subtree) and greater than (for right subtree) the root node. If duplicates are allowed but are constrained to either the left or the right subtree, then use the same procedure but adding "or equal" to either the less than or the greater than, but not both.

runtime to find middle element using AVL tree

I have an one lecture slides says following:
To find middle element in AVL tree, I traverse elements in order until It reaches the moddile element. It takes O(N).
If I know correctly, in tree structure, finding element takes base 2 O(logn) since AVL is binary tree that always divided into 2 childs.
But why it says O(N)?
I am just trying to elaborate 'A. Mashreghi' comment.
Since, the tree under consideration is AVL tree - the guaranteed finding of element in O(log n) holds as log as you have the element(key) to find.
The problem is - you are trying to identify a middle element in the given data structure. As it is AVL tree (self balanced BST) in-order travel gives you elements in ascending order. You want to use this property to find the middle element.
Algorithm goes like - have a counter increment for every node traversed in-order and return # n/2th position. This sums to O(n/2) and hence the overall complexity O(n).
Being divided into 2 children does not guarantee perfect symmetry. For instance, consider the most unbalanced of all balanced binary trees: each right child has a depth one more than its corresponding left child.
In such a tree, the middle element will be somewhere down in the right branch's left branch's ...
You need to determine how many nodes N you have, then locate the N/2th largest node. This is not O(log N) process.

Number of Binary Tree with N Node and has same Postorder & Inorder Traversal

I Studying Cataln Number in few days. I Know Tree Traversal from WikiPedia.
http://en.wikipedia.org/wiki/Tree_traversal
But,
I confused with 1 Question. How many Binary Tree with N Node that has the same Postorder & Inorder Traversal, We can construct?
Any Recurrence Relation or Sth Else would be appreciated.
Regards.
A binary tree can have same Postoder and Inorder traversal if there is no right sub-tree anywhere that means every root either has left child or is the final node(leaf). That means this binary tree is just a list, hence total ways of arranging are n!.

BST from Preorder by just inserting the nodes in same order

To construct a BST from the preorder traversal given, if I try to insert in the BST in the same order as given in preorder, I get the BST.
So, we don't to create the in-order by sorting of the elements or perform any other alogrithm?
Is there an example which shows that tree can't be constructed by just inserting the elements ?
You're correct... you can just insert the nodes in the order given by a preorder traversal to rebuild the tree.
The first node inserted must be placed at the right place, since it's the root and a preorder traversal always places the root first. What follows in the preorder traversal is the preorder layout of the left subtree followed by the right subtree. As the left subtree nodes are inserted, they are inserted by going left from the root, then recursively applying the same procedure on that subtree. The right subtree is rebuilt the same way. Using induction, you can formally prove this if you like.
Hope this helps!

How many traversals need to be known to construct a BST

I am very confused by a number of articles at different sites regarding constructing a Binary Search Tree from any one traversal (pre,post or in-order), or a combination of any two of them. For example, at this page, it says that given the pre,post or level order traversal, along with the in-order traversal, one can construct the BST. But here and there, they show us to construct a BST from pre-order alone. Also, here they show us how to construct the BST from given pre and post-order traversals. In some other site, I found a solution for constructing a BST from the post-order traversal only.
Now I know that given the inorder and pre-order traversals, it is possible to uniquely form a BST. As regards the first link I provided, although they say that we can't construct the BST from pre-order and post-order, can't I just sort the post-order array to get its inorder traversal, and then use that and the pre-order array to form the BST? Will that be same as the solution in the 4th link, or different? And given pre-order only, I can sort that to get the in-order, then use that and the pre-order to get the BST. Again, does that have to be different from the solution at links 2 and 3?
Specifically, what is sufficient to uniquely generate the BST? If uniquement is not required, then I can simply sort it to get the in-order traversal, and build one of the N possible BSTs from it recursively.
To construct a BST you need only one (not in-order) traversal.
In general, to build a binary tree you are going to need two traversals, in order and pre-order for example. However, for the special case of BST - the in-order traversal is always the sorted array containing the elements, so you can always reconstruct it and use an algorithm to reconstruct a generic tree from pre-order and in-order traversals.
So, the information that the tree is a BST, along with the elements in it (even unordered) are equivalent to an in-order traversal.
Bonus: why is one traversal not enough for a general tree, (without the information it is a BST)?
Answer: Let's assume we have n distinct elements. There are n! possible lists to these n elements, however - the possible number of trees is much larger (2 * n! possible trees for the n elements are all decayed trees, such that node.right = null in every node, thus the tree is actually a list to the right. There are n! such trees, and another n! trees where always node.left = null ) Thus, from pigeon hole principle - there is at least one list that generates 2 trees, thus we cannot reconstruct the tree from a single traversal.
(QED)
If the values for the nodes of the BST are given then only one traversal is enough because the rest of the data is provided by the values of the nodes. But if the values are unknown then, as per my understanding, constructing a unique BST from a single traversal is not possible. However, I am open to suggestions.

Resources