Finding the number of ways numbers can be inserted into the BST - data-structures

The number of ways in which the numbers 1,2,3,4,5,6,7 can be inserted in an empty binary search tree, such that the resulting tree has height 4.
(Height of root is 0).

Related

Printing a binary tree in Prolog based on a matrix

I wanted to print a binary tree by making a matrix based on coordinates which will corresponding nodes have in the tree. The binary tree is balanced and for every node goes that the nodes on the left are less than it and the nodes on the right are greater than it.
My tree is represented by a structure s(L,K,D), where L denotes the left branch N the node, and D the right branch. When it comes to an end the tree finishes with nil. For example, a simple tree with 1 as its first node and 2 and 3 its left and right node, will be represented as s(s(nil,2,nil),1,s(nil,3,nil)).
Binary tree example
In this example, node 7 will have coordinates (3,2). 2 because that is its depth in the binary tree (counting from 0), and 3 because of its index in the monotonically increasing list of nodes of the tree.( the list is [1,2,3,7...]).
Basically I can pack a tree in a matrix of dimensions NxM (N being the number of nodes, and M being the absolute depth of the tree[in this case 3]).
For this example the desired matrix would be:
Matrix example
, where I put ' ' character - where the blanks are, in order for the printing to be achieved in the desired way.
Is there any way to code a function which will generate this matrix?

Special Binary Tree, a tricky questions?

We have a binary tree with n nodes. this tree is not necessarily balanced. for any node such x of this tree, we calculate the size (i.e: number of nodes) of left and right subtree of this node and set the label of this node as the minimum of these two values (values of right size and left size subtree). if any subtree has zero nodes, this size is equal to 0. Which of the following is True:
I) sum of labels belongs to order O(n log n).
II) there is a tree that sum of it's label belongs to order O(n). (i.e: is it possible to get a tree with sum of it's label be O(n)?)
III) there is a tree that sum of it's label belongs to order O(n^2).
My TA says two of these is true. My problem is with these sentences, anyone could describe it for me?
My guess is that it's 1 & 2.
1.) Consider the height of the tree. The height and the number of nodes n have the relationship n = (2^h)-1. From this relation, we can derive that h =logn. Now, Let's move to the number of nodes in each level of the binary tree. The maximum number of nodes that a level could have is (n/2) which is the last level (in a full binary tree, the last level will have n/2 nodes). So, the worst case of calculating the minimum is (number of levels)*(number of nodes in each level) => n/2logn => O(nlogn).
2.) It's possible to get an 0(n) solution by changing the height of the tree. For example of if considering a subset of the all nodes such that the height of the tree is zero/one, then it's possible to get an O(n) solution - for a tree with a total level of two, there can be a maximum of three nodes (root, left, right), and therefore, there's no minimum calculation involved in this. In this case, we don't have the additional logn in running time, and we end up with O(n).

Data structures questions

Can we sort 7 numbers in 10 comparisons?
Depth of a binary tree with n node is? log(n)+1 or something else
If every node in a binary tree has either 0 or 2 children then the height of the tree is log(n): is it true or false?
Inserting an element into a binary search tree of size n takes time proportional to ------?
A binary tree not balanced can have all children at the right (for example), so the maximum height of a tree with n nodes is n
Same as 2.
If the tree is not balanced the insertion is proportional to the number of nodes that you need to traverse to find the correct position. Potentially n nodes.

Sequence of insertions when generating B-Tree / 2-3-4 tree

Does anyone know about how the sequence of insertions matter for 2-3-4 Trees? Or B-Trees?
It seems the formula for minimum height is logm(k+1) where m is the max no. of children and k is the number of keys
And the formula for max height is: logn((k+1)/2) where n is the min no. of children an internal node can have.
But what sequence of insertions actually get these results?! I don't know.
It has been suggested to minimise the height of the 2-3-4 tree, you would take the median of the linear sequence eg. 1,2,3,4,5,6,7,8 it being 4, and insert that, before rinse repeating for the sub lists either side of the median. Is this true? And if so, what sequence maximises the height?
Yes, the sequence of insertions matters. Obviously, the tree will be taller for the same number of keys if more nodes are 1-nodes. They way to maximise the number of 1-nodes in the tree is to continually expand one branch of the tree to 4-nodes, increasing the height of the tree while leaving many nodes as 1-nodes. Essentially, insert the keys pre-sorted. 1,2,3,..,k. For a minimum height tree, you want to expand in all branches evenly so as to fill up each layer of the tree. So, you insert the median of the keys, split up the insertion list at this key and then insert the medians from the two halves of the list and so on..

Ordering binary search trees

When trying to order a set of numbers into a binary search tree, is there always exactly one way to order them so the tree has the shortest height, in other words most efficient?
A set of numbers can be converted to a BST by taking one element as the root of the tree and arranging all other numbers around it. I could see the following situation contradicting this theory:
Picking one root leads to a tree of height h, with the left subtree being 'taller' than the right subtree.
Picking another root leads to a different tree, also of height h, with the right subtree being 'taller' than the left subtree.
Another simple example involves swapping the order of insertion of two consecutive elements that are not directly related, and thus do not affect each other's position in the tree.
Disproof by counter-example.
Let the set S = {0, 1, 2, 3}.
Insert the elements into a binary search tree in the following order: 1, 0, 2, 3
1
/ \
0 2
\
3
Insert the elements into a binary search tree in the following order: 1, 2, 0, 3
1
/ \
0 2
\
3
Because these two trees have different orders of insertion, and yet both have minimum height, the statement that there is only one order of insertion that provides a binary search tree of minimum height is false.
If the actual ordering of elements on the tree is what you're concerned about, insert the elements of the set in the following order: 2, 1, 0, 3
2
/ \
1 3
/
0
Again, this tree has the same height as the previous trees, thus showing that a different ordering of items in the tree can also produce a tree of minimum height.
(An aside)
You can always build a minimum height tree by first sorting the elements of the set, then continually subdividing the sorted set to ensure balance and complete filling of each row.
Take the median element of the set. In the case of an even number of elements, take the larger of the two 'middle' elements. This will become the root of the tree.
Take all the elements below the median. This will become the left subtree of the root.
Take all the elements above the median. This will become the right subtree of the root.
Recursively create the left and right subtrees from these sets.
This should ensure that you have a complete binary tree, which will always be of minimum height.

Resources