null pointer arrangements of binary tree - data-structures

I arrange n nodes in a binary tree, there will always be n+1 NULL pointers. for example, if n=3, then below are the possible arrangements of a Binary tree with 3 nodes, all the arrangements have 4 NULL pointers (marked 1,2,3,4)
If I arrange 100 nodes I want to prove arrangement have 101 NULL pointers that's correct.
What approach do I have to prove accurate?

Related

How many binary trees possible with N nodes using Catalan Number concept

So I was understanding Catalan Number Concept and try to implement it in code using topDown DP approach, so the recurrence relation is from what I have learned,
f(N) signifies total count of binary search trees possible by N number of nodes
f(N) = f(i-1).f(N-i)
where,
i = 1.....N (i signifies root node and traverse through all the nodes),
N is total number of nodes present in the binary tree,
f(i-1) signifies number of left subtrees possible when i is a root node,
f(N-i) signifies number of right subtrees possible when i is a root node.
there's a mathematical formulae by which we can find f(N) value,
2NcN/(N+1)
.
And, total count of binary trees possible by N number of nodes,
(factorial of N) * f(N)
My Doubt:
what's the difference between binary trees and binary search trees? I feel like both are defines same meaning but there's a difference that I'm not aware of so help me.
What's the difference between binary trees and binary search trees?
There is no difference in the shape of such trees, but binary search trees put a constraint on the values that its nodes have, which doesn't exist for (just) binary trees:
The value of a node must not be less than any of the values in its left subtree
The value of a node must not be greater than any of the values in its right subtree
Another way to express this constraint is:
The in-order traversal of the values in the tree must be non-decreasing.
Practically this means that when you have been given:
A shape of a binary tree with 𝑛 nodes
A set of 𝑛 unique values that are present in the tree
...then there is only one possibly way to associate those values with the tree's nodes for it to be a binary search tree. If it does not have to be a binary search tree, then there are 𝑛! ways to make that association.
Conclusions:
Given 𝑛, then 𝐢𝑛 (the 𝑛th Catalan number) represents:
the number of binary trees with 𝑛 nodes without values (i.e. number of shapes of binary trees)
the number of binary search trees with 𝑛 distinct values
The number of binary trees with 𝑛 distinct values is 𝑛!𝐢𝑛

Number of nodes in a B-Tree

How many nodes does a resulting B-Tree(min degree 2) have if I insert numbers from 1 to n in order?
I tried inserting nodes from 1 to 20 there was a series for the number of nodes coming but i could not generalize it.
Can anyone please help me derive the formula for this.
It will depend on the order of the B-Tree. The order of a BTree is the maximum number of children nodes a non-leaf node may hold (which is one more than the minimum number of keys such a node could hold).
According to Knuth's definition, a B-tree of order m is a tree which satisfies the following properties:
Every node has at most m children.
Every non-leaf node (except root) has at least ⌈m⁄2βŒ‰ children.
The root has at least two children if it is not a leaf node.
A non-leaf node with k children contains kβˆ’1 keys.
All leaves appear in the same level, and internal vertices carry no information.
So in your case when you are inserting 20 keys if the order is m then based on the conditions mentioned above you can derive a set of inequalities that describes the possible value of m. But there is no equality formula that says the number of internal nodes in a B-Tree.

Given a number n, how many balanced binary trees (not binary search trees) are there?

The definition of balanced in this question is
The number of nodes in its left subtree and the number of nodes in its
right subtree are almost equal, which means their difference is not
greater than one
if given a n as the number of nodes in total, how many are there such trees?
Also what if we replace the number of nodes with height? Given a height, how many height balanced trees are there?
Well the difference will be made only by the last level, hence you can just find how many nodes should be left for that one, and just consider all possible combinations. Having n nodes you know that the height should be floor(log(n)) hence the same tree at depth k = floor(log(n)) - 1 is fully balanced, hence you know that is needs (m = sum(i=0..k)2^i) nodes, hence n-m nodes are left for the last level. Some definition of a balanced binary tree force "all the nodes to be left aligned", in this case it is obvious that there can be only one possibility, without this constraint you have combinations of 2^floor(log(n)) chooses n-m, because you have to pick which of the 2^floor(log(n)) possible slots you will assign with nodes, forcing a total of n-m nodes to be assigned.
For the height story you consider a sum of combinations of 2^floor(log(n)) chooses i as i goes from 1 to 2^floor(log(n)). You consider all possibilities of having either 1 node at the last level, then 2 and so on, until you don't make it a fully balanced binary tree, hence having all 2^floor(log(n)) slots assigned.

Hoffman tree Vs, Binary balanced tree

Given n Real Positive numbers A1,A2....An build a binary tree T such as:
Every number is a leaf.
Weight of T will be minimal as possible. Weight of tree equals to sum of each leaf times its hight.
This question was given to me during a test in Algorithms and Data Structures. My answer in brief was to build a binary tree such that each leaf is A1 to An. Weight of T will be sum of logn*Ai.
I did not get points for this answer. The answer that was awarded full points was to sort the numbers by frequencies and build a Hoffman Tree.
My question is why my answer was ignored?
If A1 to An are all very small numbers, for example ranging from 0 to 1, then the hight of each leaf will become the dominent factor in calculating the weight of the tree.
Help would be apprecieted.
In the original array A there may be some elements with many more occurences than the others. You want to construct the tree in a way, that the most frequent elements are higher in the tree than the somewhat rare ones.
Consider the example on this page - "A quick tutorial on generating a huffman tree".
The generated huffman tree has weight 228, which is optimal.
The best perfectly balanced tree you could get for the same set has weight 241 (5 and 6 with depth 2, other elements with depth 3), the worst one 294 (switching 5 and 6 with 1 and 2).
Your solution would find something between those, rather than the optimum.

Implementation of binary tree

The following text is snippet from algorithms book.
We could draw the
binary trees using rectangular boxes that are customary for linked
lists, but trees are generally drawn as circles connected by lines
because they are actually graphs. We also do not explicitly draw NULL
links when referring to trees because every binary tree with N
nodes would require N+1 NULL links.
My question is what does author mean every binary tree with N nodes would require N+1 null links? how author came with N+1 number?
If you have a tree of 1 node, there are 2 null links (left and right on the root). If you add a node to either left or right, you have filled 1 null and added 2 more. This continues on ad infinitum, therefore for each node added you net 1 extra null leaf.
You could prove this by mathematical induction.
Base case
1 node has 2 NULL links - satisfies the property.
Inductive step
Now assume that all trees with n-1 nodes have n NULL links. Then we wish to show, that all trees with n nodes have n+1 NULL links.
Take any tree with n nodes, and pick one of the leaves. Remove this leaf. We now have a tree with n NULL links, per our assumption. If we add the leaf again, we lose one NULL link, but gain two. Thus, we have n - 1 + 2 = n+1 NULL links on the tree with n nodes.
Observation: Each edge acts as a link. N-1 edges for N nodes.
Totally links: 2N.
Null links: 2N - (N-1) = N+1.

Resources