Inorder binary non-sorted tree traversal - data-structures

Does inorder tree traversal on a binary integer non-sorted tree produce a sorted array of integers?
I know that if it's a true binary search tree, the result is a sorted array.
For example, would this produce a sorted array:
6
1
3
4 5
22

Related

Can a binary search tree be constructed from only the inorder traversal?

Wanted to check if there is a way to construct a binary search tree from only the inorder traversal which will be in sorted order. I am thinking there might be some way we can do it recursively, but not able to figure it out. Any pointers would be appreciated.
A BST has exactly one in-order traversal, but more than one BST can be constructed with a given in-order traversal. Hence, Yes, it is possible to construct a BST with a given in-order traversal, but you may not end up with the same tree whose in-order traversal you've started with.
Check this article for more info: https://www.geeksforgeeks.org/find-all-possible-trees-with-given-inorder-traversal/
Yes it is possible to construct a binary search tree from an inorder traversal.
Observe that an inorder traversal of a binary search tree is always sorted.
There are many possible outcomes, but one can be particularly interested in producing a tree that is as balanced as possible, so to make searches in it efficient. One way to produce an inefficient binary search tree, is to keep adding new nodes to the right of the last inserted node. The resulting binary search tree is then skewed.
If for example the inorder traversal is 1,2,3,4,5,6, we would get this tree:
1
\
2
\
3
\
4
\
5
\
6
That is not very helpful as binary search tree, as it really has degenerated into a single chain, and a search in that tree is no better than performing a search from left to right in the input array.
A much more efficient alternative would be this binary search tree, which has the same inorder traversal:
3
/ \
2 5
/ / \
1 4 6
Algorithm
The algorithm to produce a rather balanced binary search tree can indeed be a recursive one:
If the given array (traversal) is empty return null (emtpy tree)
Take the middle value of the given array (traversal) and create a node for it.
Take the subarray at the left of the selected array value, and perform the algorithm recursively for it. This returns a node (rooting a subtree), which should be attached as left child of the node created in step 2.
Take the subarray at the right of the selected array value, and perform the algorithm recursively for it. This returns a node (rooting a subtree), which should be attached as right child of the node created in step 2.
Return the node created in step 2.
TreeNode* solve(vector<int>& v, int l, int r){
if(l>r){
return NULL;
}
int m = (l+r)/2;
TreeNode* root = new TreeNode(v[m]);
root->left = solve(v, l, m-1);
root-> right = solve(v, m+1,r);
return root;
}
TreeNode *constructFromInOrder(vector<int> &inorder)
{
return solve(inorder, 0, inorder.size()-1);
}
c++ code to convert Inorder to BST

Updating multiple nodes in AVL tree efficently?

I have an AVL tree with n nodes, where they are sorted by index 1,2,3,4...,n
I want to increase the index of all nodes in [i,j] by d, how can I do this in O(log n)?
For example:
I my tree had 1,2,3,4 and d=3 and I got [2,4] then the new AVL tree will hold 1,5,6,7

Preorder and relation to Inorder Traversal

I find that if we have Preorder and Inorder Traversal, we have a unique tree.
can i conclude:
For Each Preorder Traversal, we have multiple Inorder Traversal. this is True or False Conclusion? every one would help me and add some detail.
thanks again.
Yes because from a single preorder sequence you can create multiple trees. For instance: [4,3,1, 2] can be repesented as a tree with root 4, children 3 and 2. Then you can insert 1 as either left or right child of 3. Depending on where you will insert it the inorder sequence will change.
You can also reason about it in this way: you have n numbers, with them you can get n! permutations. Generating a tree from your traversal would be possible if the number of permutations was equal to the number of possible trees you can create usin those n numbers. This is not the case, though as you can for instance create many trees, where every node has only a left child or only a right child, this gives you 2*n! and there are more so there has to be a permutation that can generate more than 1 tree => more than 1 inorder traversal.
This is of course true in general case, BSTs have a unique inorder sequence.
True,
for example:
given a preorder traversal
abdec
a
/ \
b c
/ \
d e
many inorder traversals are possible:
baedc,
dbeac
and so on...

Binary Tree : Advantages of pre-order ,post-order traversals in Binary Tree?

Inorder traversal of a Binary Search Tree yields nodes in increasing order. But what advantages do pre order and post order traversals have on any binary tree?
EDIT:What I mean by advantages is : "any situation where applying pre-order or post-order traversal is specifically suited".
Not all binary trees have numbers in them. You can use a binary tree to represent things that exhibit tree structure, such as expressions. For example, 2 * 3 + 4 can be represented as
+
/ \
* 4
/ \
2 3
If you represent an expression like that, the in-order traversal would yield your "normal" infix notation of
2 * 3 + 4
but a post-order traversal would yield a Reverse Polish Notation of the expression:
2 3 * 4 +

Binary Tree array list representation

I have been doing some research on Binary trees, and the array list representation. I am struggling to understand that the worst case space complexity is O(2^n). Specifically, the book states, the space usage is O(N) (N = array size), which is O(2^n) in the worst case . I would have thought it would have been 2n in the worst case as each node has two children (indexes) not O(2^n), where n = no. of elements.
an example, if I had a binary tree with 7 nodes, then the space would be 2n = 14 not 2^n = 128.
This is Heap implementation on an array. Where
A[1..n]
left_child(i) = A[2*i]
right_child(i) = A[2*i+1]
parent(i) = A[floor(i/2)]
Now, come to space. Think intuitively,
when you insert first element n=1, location=A[1], similarly,
n=2 #A[2] left_child(1)
n=3 #A[3] right_child(1)
n=4 #A[4] left_child(2)
n=5 #A[5] right_child(2)
You see, nth element will go into A[n]. So space complexity is O(n).
When you code you just plug-in the element to be inserted in the end say at A[n+1], and say that it's a child of floor((n+1)/2).
Refer: http://en.wikipedia.org/wiki/Binary_heap#Heap_implementation
Heap is a nearly complete tree, so total number of elements in the tree would be 2h-1 < n <= 2h+1-1 and this is what the length of array you will need. Refer: this
The worst case space complexity of a binary tree is O(n) (not O(2^n) in your question), but using arrays to represent binary trees can save the space of pointers if it's nearly a complete binary tree.
See http://en.wikipedia.org/wiki/Binary_tree#Arrays
I think this refers to storing arbitrary binary trees in an array representation, which is normally used for complete or nearly complete binary trees, notably in the implementation of heaps.
In this representation, the root is stored at index 0 in the array, and for any node with index n, its left and right children are stored at indices 2n+1 and 2n+2, respectively.
If you have a degenerate tree where no nodes have any right children (the tree is effectively a linked list), then the first items will be stored at indices 0, 1, 3, 7, 15, 31, .... In general, the nth item of this list (starting from 0) will be stored at index 2n-1, so in this case the array representation requires θ(2n) space.

Resources