Minimum and Maximum number of nodes in a 2-3 Tree - data-structures

I'm trying to find out what are the minimum and maximum number of nodes in a 2-3 Tree with n leaves.
I have tried blocking it with inf\sup but I couldnt go further then that the number of nodes in a 2-3 Tree is bigger then the number of nodes in a full-AVL tree.
Thanks in advance

Operating under the definition of a 2-3 tree at wikipedia:
In computer science, a 2–3 tree is a type of data structure, a tree where every node with children (internal node) has either two children (2-node) and one data element or three children (3-nodes) and two data elements. Nodes on the outside of the tree (leaf nodes) have no children and one or two data elements.
It appears to me that the maximum number of nodes in a tree will be when each internal node has 3 children. In order to find the maximum number of nodes in that tree, we must first find the height of the tree.
If there are n leaves in this 3 tree, then the height of the tree is height = log3(n) (log base 3 of n) and so the max number of items would be 3^height.
The smallest tree is one which has the smallest number of elements, which would be a tree with a single node.

Related

Given a number n and k, how many balanced (height-balanced)binary trees with k leaves are there?

We have n nodes and a k number, How can we calculate (e,g, with dynamic programming) all height-balanced tree with n nodes and k leaves ??
(a height-balanced binary tree is a tree where maximum difference of leaves height is 1.)
Assumptions/definitions about these trees:
There are n "nodes"; each has either no children, just a left child, just a right child, or both a left child and a right child.
There are k "leaves", meaning nodes with no children.
If the height of the tree is called h, then the nodes with fewer than two children all have depth equal to either h or h–1. (This is the balancing requirement.)
A tree is fully defined by its structure (which nodes have no children vs. left children vs. right children vs. both left and right children).
Given the above, the tree consists of a full binary tree of height h–1 plus one or more leaf nodes in the bottom level. The total number of leaves is equal to the number of leaves that we add, plus the number of nodes in the previous level that we don't add any children to.
So, at a high level, the algorithm is as follows:
Compute how many nodes are in the bottom level, and how many are in the level above that. (This depends only on n; for example, a full binary tree of height 6 has 26+1–1 = 127 nodes, so if n = 130, then the bottom level has 3 nodes, and the second-to-bottom level has 64 nodes.)
Compute how many nodes in the second-to-bottom level have no children (which is k minus the number of nodes on the bottom level), and how many of the rest have one child vs. two children (using the previously-obtained results).
The total number of possible trees can then be determined by combinatorics. Specifically, it is the product of these three values:
the number of ways to choose, from the nodes on the second-to-bottom row, which ones have no children: (nodes in the second-to-bottom level) nCr (nodes in the second-to-bottom level with no children)
the number of ways to choose, from the remaining nodes on the second-to-bottom row, which ones have one child: (nodes in the second-to-bottom level with one or two children) nCr (nodes in the second-to-bottom level with one child)
the number of ways to choose, from the nodes on the second-to-bottom row with one child, which ones have left-children vs. which ones have right-children: 2(nodes in the second-to-bottom level with one child)
While implementing this, be sure to take into account the possibility that there are no trees with given values of n and k; for any given n, only a certain range of values of k is actually possible for a tree satisfying the above constraints. If k is outside that range, then the implementation needs to return 0.

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.

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.

Relationship between number of nodes and height

I am reading The Algorithm Design Manual. The author states that the height of a tree is:
h = log n,
where
h is height
n = number of leaf nodes
log is log to base d, where d is the maximum number of children allowed per node.
He then goes on to say that the height of a perfectly balanced binary search tree, would be:
h = log n
I wonder if n in this second statement denotes 'total number of leaf nodes' or 'total number of nodes'.
Which brings up a bigger question, is there a mathematical relationship between total number of nodes and the height of a perfectly balanced binary search tree?
sure, n = 2^h where h, n denote height of the tree and the number of its nodes, respectively.
proof sketch:
a perfectly balanced binary tree has
an actual branching factor of 2 at each inner node.
equal root path lengths for each leaf node.
about the leaf nodes in a perfectly balanced binary tree:
as the number of leafs is the number of nodes minus the number of nodes in a perfectly balanced binary tree with a height decremented by one, the number of leafs is half the number of all nodes (to be precise, half of n+1).
so h just varies by 1, which usually doesn't make any real difference in complexity considerations. that claim can be illustrated by remembering that it amounts to the same variations as defining the height of a single node tree as either 0 (standard) or 1 (unusual, but maybe handy in distinguishing it from an empty tree).
It doesn't really matter if you talk of all nodes or just leaf nodes: either is bound by above and below by the other multiplied by a constant factor. In a perfectly balanced binary tree the number of nodes on a full level is the number of all nodes in levels above plus one.
In a complete binary tree number of nodes (n) and height of tree (h) have a relationship like this in below.
n = 2^(h+1) -1
this is the all the nodes of the tree

IOI 2003 : how to calculate the node that has the minimum balance in a tree?

here is the Balancing Act problem that demands to find the node that has the minimum balance in a tree. Balance is defined as :
Deleting any node
from the tree yields a forest : a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting that node from T
For the sample tree like :
2 6 1 2 1 4 4 5 3 7 3 1
Explanation is :
Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The
larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node
1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees
has two nodes, so the balance of node 1 is two.
What kind of algorithm can you offer to this problem?
Thanks
I am going to assume that you have had a looong look at this problem: reading the solution does not help, you only get better at solving these problems by solving them yourself.
So one thing to observe is, the input is a tree. That means that each edge joins 2 smaller trees together. Removing an edge yields 2 disconnected trees (a forest of 2 trees).
So, if you calculate the size of the tree on one side of the edge, and then on the other, you should be able to look at a node's edges and ask "What is the size of the tree on the other side of this edge?"
You can calculate the sizes of trees using dynamic programming - your recurrence state is "What edge am I on? What side of the edge am I on?" and it calculates the size of the tree "hung" at that node. That is the crux of the problem.
Having that data, it is sufficient to iterate through all the nodes, look at their edges and ask "What is the size of the tree on the other side of this edge?" From there, you just pick the minimum.
Hope that helps.
You basically want to check 3 things for every node:
The size of its left subtree.
The size of its right subtree.
The size of the rest of the tree. (size of tree - left - right)
You can use this algorithm and expand it to any kind of tree (different number of subnodes).
Go over the tree in an in-order sequence.
Do this recursively:
Every time you just before you back up from a node to the "father" node, you need to add 1+size of node's total sub trees, to the "father" node.
Then store a value, let's call it maxTree, in the node that holds the maximum between all its subtrees, and the (sum of all subtrees)-(size of tree).
This way you can calculate all the subtree sizes in O(N).
While traversing the tree, you can hold a variable that hold the minimum value found so far.

Resources