what's the name of this binary tree structure? - data-structures

I am trying to implement the following data structure by js, but I was wondering if anyone knows the name of such binary tree? thanks.
It is a full binary tree T of unlimited height. Suppose root is at level 0.
From level 1, the number of nodes at level i and level i+1 are both 2^i,
where i is in {1, 2, 3,...}.
The left subtree of any node is complete.
As shown in Figure, denote the right most node in each
level i which is in {0, 1, 2,...} as v_i, then the left
subtree of each node v_i is a perfect binary tree, denoted as T_i.
Suppose the leaf nodes in T_i have height zero, then each subtree T_i
is of height i and has 2^i leaves.

Related

Finding Height of a node in an AVL Tree for Balance Factor calculation

AVL tree is a binary search tree that is balanced i.e height = O(log(n)). This is achieved by making sure every node follows the AVL tree property:
Height of the left subtree(LST) - Height of the right subtree(RST) is
in the range [-1, 0, 1]
where Height(LST) - Height(RST) is called Balance factor(BF) for a given node.
The height of the node is usually defined as, "length of the path(#edges) from that node to the deepest node"
eg:
By this definition, height of leaf is 0.
But almost everytime while discussing AVL trees, people consider height of the leaf to be 1.
My question is, can we take the height of leaf to be 0? This will make following BSTs also AVL trees, right?
Height concept confuses me because of these articles:
https://www.geeksforgeeks.org/minimum-number-of-nodes-in-an-avl-tree-with-given-height/
https://www.tutorialspoint.com/minimum-number-of-nodes-in-an-avl-tree-with-given-height-using-cplusplus
Firstly, they start the height with 0.
Then they say, minimum number of nodes required for avl tree of height 2 to be 4 BUT if height is starting with zero, I can have the following AVL trees too, right?
By this definition, height of leaf is 0.
This is correct.
BUT if height is starting with zero, I can have the following AVL trees too, right?
No, the parent of the leaf has height 1, as the path from that node to the leaf has 1 edge.
So it should be:
O -- height 2, balance factor 2
/
O -- height 1, balance factor 1
/
O -- height 0
The balance factor of the root is 2 because the height of its left subtree is 1 and that of its right subtree is -1 (!). Note that if a single node has height 0, then an empty tree has height -1. This is also what is mentioned in Wikipedia:
The height of a node is the length of the longest downward path to a leaf from that node. [...] The root node has depth zero, leaf nodes have height zero, and a tree with only a single node (hence both a root and leaf) has depth and height zero. Conventionally, an empty tree (tree with no nodes, if such are allowed) has height −1.
And so these are not valid AVL trees: they need to be rebalanced.

Given a tree, find the kth node in the path from node 'a' to node 'b' in Log(n)?

Given a tree, I need to find the 'k'th node in the path from 'a' to 'b'. That means that I need to find the node at the 'kth' position in the path from node 'a' to node 'b'. I was thinking on the lines of Lowest-common-ancestor and/or heavy-light decomposition but I'm not sure if that's the way to do it. Any guidance in the right direction is appreciated.
Details:
The tree is NOT a binary tree. It's an undirected graph having n-1 edges, n vertices and no cycles. Just your regular tree
The tree has vertices numbered from 1 to n. There are n-1 undirected edges connecting n-1 pairs of these vertices
'a' and 'b' are any 2 vertices numbered from 1 to n in the tree. We are to find the 'k'th node in the path from 'a' to 'b'. It is guaranteed that the value of 'k' is <= number of nodes in the path from 'a' to 'b'
A BFS applied on every query (kth node from 'a' to 'b') is not an option as the number of queries are large.
Do the lowest-common-ancestor, and keep for every node it's depth (distance to the root).
Next figure out if the k'th node is on the a to lca or lca to b part. The difference in depth is the number of nodes between them, so if depth[a] - depth[lca] > k then the node is on lca-b part.
If the node is on the a to lca part, just find the k'th ancestor of a. Should be log(N) using the LCA data you precalculated.
If the node is on the b to lca part, then you can compute k' which is the distance of the kth node from b (k' = 1 + depth[a] + depth[b] - 2*depth[lca] - k)) and then get k' ancestor of b (again easy with the the LCA data).
Overall all the lookups are logN and the other steps are constant so you do O(LogN) per query.

Finding the minimum and maximum height in a AVL tree, given a number of nodes?

Is there a formula to calculate what the maximum and minimum height for an AVL tree, given a certain number of nodes?
For example:
Textbook question:
What is the maximum/minimum height for an AVL tree of 3 nodes, 5 nodes, and 7 nodes?
Textbook answer:
The maximum/minimum height for an AVL tree of 3 nodes is 2/2, for 5 nodes is 3/3, for 7 nodes is 4/3
I don't know if they figured it out by some magic formula, or if they draw out the AVL tree for each of the given heights and determined it that way.
The solution below is appropriate for working things out by hand and gaining an intuition, please see the exact formulas at the bottom of this answer for larger trees (54+ nodes).1
Well the minimum height2 is easy, just fill each level of the tree with nodes until you run out. That height is the minimum.
To find the maximum, do the same as for the minimum, but then go back one step (remove the last placed node) and see if adding that node to the opposite sub-tree (from where it just was) violates the AVL tree property. If it does, your max height is just your min height. Otherwise this new height (which should be min height+1) is your max height.
If you need an overview of what the properties of an AVL tree are, or just a general explanation of an AVL tree, Wikipedia is a great place to start.
Example:
Let's take the 7 node example case. You fill in all levels and find a completely filled tree of height 3. (1 at level 1, 2 at level 2, 4 at level 3. 1+2+4=7 nodes.) That means 3 is your minimum.
Now find the max. Remove that last node and place it on the left subtree instead of the right. The right subtree still has height 3, but the left subtree now has height 4. However these values differ by less than 2, so it is still an AVL tree. Therefore your max height is 4. (Which is min+1)
All three examples worked out below (note that the numbers correspond to order of placement, NOT value):
Formulas:
The technique shown above doesn't hold if you have a tree with a very large number nodes. In this case, one can use the following formulas to calculate the exact min/max height2.
Given n nodes3:
Minimum: ceil(log2(n+1))
Maximum: floor(1.44*log2(n+2)-.328)
If you're curious, the first time max-min>1 is when n=54.
1Thanks to Jamie S for bringing this failure at larger node counts to my attention.
2Technically, the height of a tree is the longest path length (in edges) between the root and any leaf node. However the OP's textbook uses a common alternate definition of height as the number of levels in a tree. For consistency with the OP and Wikipedia, we use that definition in this post as well.
3These formulas are from the Wikipedia AVL page, with constants plugged in. The original source is Sorting and searching by Donald E. Knuth (2nd Edition).
It's important to note the following defining characteristics of an AVL Tree.
AVL Tree Property
The nodes of an AVL tree abide by the BST property
AND The heights of the left and right sub-trees of any node differ by no more than 1.
Theorem: The AVL property is sufficient to maintain a worst case tree height of O(log N).
Note the following diagram.
- T1 is comprised of a T0 + 1 node, for a height of 1.
- T2 is comprised of T1 and a T0 + 1 node, giving a height of 2.
- T3 is comprised of a T2 for the left sub-tree and a T1 for the right
sub-tree + 1 node, for a height of 3.
- T4 is comprised of a T3 for the left sub-tree and a T2 for the right
sub-tree + 1 node, for a height of 4.
If you take the ceiling of O(log N), where N represents the number of nodes in an AVL tree, you get the height.
Example) T4 contains 12 nodes. [ceiling]O(log 12) = 4.
See the pattern developing here??
**The worst-case height is
Lets assume the number of nodes is n
Trying to find out the minimum height of an AVL tree would be the same as trying to make the tree complete i.e. fill all the possible nodes at each level and then move to the next level.
So at each level the number of eligible nodes increases by 2^(h-1) where h is the height of the tree.
So at h=1, nodes(1) = 2^(1-1) = 1 node
for h=2, nodes(2) = nodes(1)+2^(2-1) = 3 nodes
for h=3, nodes(3) = nodes(2)+2^(3-1) = 7 nodes
so just find the smallest h, for which nodes(h) is greater than the given number of nodes n.
Now for the problem of maximum height of an AVL tree:-
lets assume that the AVL tree is of height h, F(h) being the number of nodes in the AVL tree,
for its height to be maximum lets assume that its left subtree FL and right subtree FR have a difference in height of 1(as it satisfies the AVL property).
Now assuming FL is a tree with height h-1 and FR be a tree with height h-2.
now the number of nodes in
F(h)=F(h-1)+F(h-2)+1 (Eq 1)
Adding 1 on both sides :
F(h)+1=(F(h-1)+1)+ (F(h-2)+1) (Eq 2)
So we have reduced the maximum height problem to a Fibonacci sequence. And these trees F(h) are called Fibonacci Trees.
So, F(1)=1 and F(2)=2
so in order to get the maximum height just find the index of the the number in the fibonacci sequence which is less than or equal to n.
So applying (Eq 1)
F(3)= F(2) + F(1)+ 1=4, so if n is between 2 and 4 tree will have height 3.
F(4)= F(3)+ F(2)+ 1 = 7, similarly if n is between 4 and 7 tree will have height 4.
and so on.
http://lcm.csa.iisc.ernet.in/dsa/node112.html
It is roughly 1.44 * log n, where n is the number of nodes.
For a more detailed description on how that was derived. You can refer to this link starting on the middle of page 13: http://www.compsci.hunter.cuny.edu/~sweiss/course_materials/csci335/lecture_notes/chapter04.2.pdf

There are at most ceiling(n/2^(h+1)) nodes of height h in any n-element tree

The reference is from Intro to Algorithms, pg 157. The image has 10 nodes and the height of the tree is 3.
My question is how does this hold when h=1?
ceiling(n/2^(h+1))= ceiling(10/2^(1+1))=ceiling(10/4)=ceiling(2.5)=3 nodes. However h=1 has 4 nodes.
The height of a node is the number of edges on the longest downward path between the node and a leaf.
With a binary tree of height 1, there can only be the root node, the left leaf, and the right leaf. That is a grand total of 3.
in the image, only 2, 16 and 3 are nodes of height 1.

Number of nodes in a B tree

In a B tree with minimum degree t, each non leaf node other than root has at least t children and at most 2*t children. Suppose that the keys {1,2,3...,n} are inserted into an empty B tree with minimum degree 2 in the sequence 1,2,3.....,n. How many nodes does the final B tree have?
From what I have understood, I feel it would be n/t since minimum number of keys each node can have is k, and the total number of keys is n. Am I correct?? If not Tell me where am I going wrong and how should I do this?
The answer is (n-2)*log(n-2) with t=2
We know that every node except the root must have at least t−1=1 keys, and at most 2t−1=3 keys. The final tree can have at most n−1 nodes when n≥2. Unless n=1 there cannot ever be n nodes since we only ever insert a key into a non-empty node, so there will always be at least one node with 2 keys. Next observe that we will never have more than one key in a node which is not a right spine of our B-tree. This is because every key we insert is larger than all keys stored in the tree, so it will be inserted into the right spine of the tree. The fewest possible number of nodes occurs when every node except the deepest node in the right spine has 2 keys and the deepest node in the right spline has 3 keys. So at height 1, 1 nodes, at height 2, 3 nodes, …, at level h, 2h−1 nodes. In this case, n =∑(i=1)^h▒2^i +1=2h+1−1 where h is the height of the B-tree, and the number of nodes in B-Tree is #nodes = ∑(i=1)^h▒〖(2^i-1)〗 = 2h+1−2−h = n−lg(n+1). So for any n, the final B-Tree must have n−⌊lg(n+1)⌋≤#nodes≤n−1 (if n≥2).

Resources