Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling empty Binary tree as Binary search tree without changing structure (Node linkage)

I had an interview today and I was asked to code this.. You have an already created unordered Binary tree with no data in any node. We have an array having equal number of elements. We have to insert data in the binary tree as a binary search tree without changing the structure of binary tree.

The method I came up with was to sort the array and then traverse its element one by one, putting each data element in the first empty inorder node in the tree. But I guess that's incorrect as I wasn't selected.

Sorry if algorithm questions aren't allowed. I'll take this down if there is an issue like this...

like image 910
Napstablook Avatar asked Jun 13 '16 12:06

Napstablook


People also ask

Can a binary search tree be empty?

The formal recursive definition is: a binary tree is either empty (represented by a null pointer), or is made of a single node, where the left and right pointers (recursive definition ahead) each point to a binary tree.

How do you convert a binary tree to a binary search tree?

To convert a binary tree to a binary search tree, you need to first create an array that will store the inorder traversal of a tree, then you need to sort the array, do an inorder traversal of a tree again and finally copy the elements to the tree nodes.

Is an empty tree a full binary tree?

If a binary tree node is NULL then it is a full binary tree. If a binary tree node does have empty left and right sub-trees, then it is a full binary tree by definition. If a binary tree node has left and right sub-trees, then it is a part of a full binary tree by definition.


2 Answers

It is correct, when you sort the array and give it into the not changeable tree in inorder, then the tree is correctly filled. But maybe there is a better way to solve this task... without sorting, or maybe another question was wrong... Sorry

like image 170
Thomas Avatar answered Oct 17 '22 23:10

Thomas


Not only is your solution correct, it's impossible to do better (in the asymptotic sense), assuming that only < or > comparisons are allowed between data items.

Your solution involves sorting the data, which takes time O(n log n), and then inserting it into the tree in an in-order traversal, which takes time O(n), for an overall time complexity of O(n log n). Notice that after building a binary search tree, we can read out all its data in sorted order using an in-order traversal -- that is, solving the interviewer's problem can be used to sort any given sequence of data elements.

Now suppose to the contrary that there is in fact some algorithm that can solve the interviewer's problem in o(n log n) time -- that is, with a strictly better time complexity than the one you gave. Then this algorithm could be used to sort the given data in strictly better than O(n log n) time. But we know that this is not possible -- O(n log n) is a lower bound on the time needed to sort n elements, if all we are allowed to do with them is compare them using < or >. Thus no such better algorithm can exist.

Note that this bound fails to hold if we assume that the input values are small integers bounded by some constant, since then operations like radix sorting can perform sorting in O(n) time.

like image 27
j_random_hacker Avatar answered Oct 18 '22 00:10

j_random_hacker