Find a vertex of size k in an AVL tree - algorithm

Let a be an AVL tree with n vertices.
Each vertex has an extension representing the size of its sub-tree with the vertex himself as a root. I'm trying to implement an algorithm that gets as input a number k s.t. 1<=k<=n and return a vertex of size k in O(logn).
If the tree is a full binary tree it's easy we can just go right/left until we reach the node with the size we need since all nodes in a height h will have the same size. But when the tree is not full i'm getting stuck.
Any help will be much appreciated.
Thanks in advance!
P.S. I assume that each vertex has a different value.

First you check if the left sub-tree is bigger than k, if it is -> continue searching in it.
Otherwise, check if the left sub tree's size + 1 equals k, in that case the current element is the kth so return the current node.
If we've reached this point it means that the the kth element will be in the right sub-tree, so move on to the right sub-tree and search for the k - (left sub tree size + 1) element, since we've eliminated all those nodes already.

Related

Is it possible to determine AVL tree balanced or not if instead of height depth is given?

In question it is given we can use depth only and not height.
(As we know for height we can say if difference between height of left subtree and height of right subtree is is at most one then it will be balanced)
Using depth can we find a way to prove tree balanced or not?
I tried by finding relation between different depth trees
What I got is that
If depth max = n
Then there must be n nodes whose depth is n-1
But this is just one condition I got.
It is not sufficient condition
( You can ignore my approach and try other thing .As there is no condition on approaching the problem)
The principle is the same as with height: use the following logic:
For each node do:
Get the maximum among the depths of all the nodes in the left subtree. Default (when no left subtree is present) is the current node's depth.
Get the maximum among the depths of all the nodes in the right subtree. Default (when no right subtree is present) is the current node's depth.
The difference between these two should not be more than 1.
If you implement this with a post-order traversal through the tree, you can keep track of the maximum depths -- needed in the first two steps -- as you traverse the tree.

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).

Algorithm For Vertex Removal From Tree

When a vertex and its incident edges are removed from a tree, a collection of
subtrees remains. Write an algorithm that, given a graph that is a tree with n
vertices, finds a vertex v whose removal leaves no subtree with more than n/2
vertices.
I have attempted this problem using a modified DFS approach as well as a bridge finding algorithm. Any help would be much appreciated.
Create a recursive function that does a post-order traversal of the tree.
The function returns the subtree size and a vertex, or the vertex can be global (in which case you just set it instead of returning it).
Call the function for the root.
Call the function on each child's subtree.
If one of those calls returned a vertex, return that vertex.
Return the current vertex if these conditions hold:
Each child's subtree has less than or equal to n/2 vertices.
The sum of children's subtree sizes is greater than or equal to (n-1)/2, i.e. the subtree 'above' the current has less than or equal to n/2 nodes.
Return the sum of children's subtree sizes + 1 as the subtree size.
Running time: O(n).
I'm assuming you've already got the size of the tree - n - if not, you'll need to start off with another traversal to get this size (which doesn't affect the O(n) running time).

Proving AVL trees can have children whose number of nodes aren't Θ of one another

Let T be an AVL tree whose left subtree is TL and whose right subtree is TR. Let's let |TL| and |TR| be the number of nodes in the left and right subtrees, respectively.
I need to prove that neither |TR| ≠ Θ(|TR|) and vice-versa but I don't know how. I assume it has to do with the case where one tree is a full AVL tree and the other is a minimal AVL tree (a Fibonacci tree), but I don't know what to do from there.
In an AVL tree of height h, the number of nodes ranges between Fh+2 - 1 and 2h - 1. This first quantity is Θ(φh) and the second is Θ(2h), where φ is the golden ratio, approximately 1.61. This means that you can construct AVL trees where the number of nodes in the left subtree is Θ(φh) and the right subtree is Θ(2h), meaning that the left subtree has asymptotically fewer nodes than the right subtree. You can then reverse left and right to show that the right subtree can't be Θ of the left subtree either.
Hope this helps!

Trouble finding the successor of a key in a balanced binary search tree

As the question states, I'm trying to find an algorithm to find the successor of a key 'k' in balanced binary search tree. I think a balanced BST is the same as an AVL tree (correct me if I'm wrong). I was hoping I could do this in one pass in O(log n) time, but everything I've found says I need to go down the right side of the tree, then the left. I'm new at the whole trees thing and still find it a little confusing. Any help would be greatly appreciated!
Thanks.
In a binary search tree, you have two path option to go down : left or right.
Now suppose we have an element k in a node N. We want to find k's successor, which is the smallest element of the tree which is greater than k.
There are 3 use cases here :
N has a non-NULL right child : the leftmost element of the right subtree is k's successor.
N has not such right child and is the left child of its parent P. In this case, P holds the successor of k.
Finally, N is the right child of its parent P. Then, to find its successor you must follow a more elaborate procedure shown below ...
Starting from S = Parent(P) : while S ≠ Root AND P ≠ Left(S)
P ← S
S ← Parent(S)
If S = Root and P = Right(S), then k was the maximum element of the tree ... Otherwise, just perform the following loop after setting S ← Right(S):
While S ≠ NULL :
P ← S
S ← Left(S)
When this loop ends, P holds k's successor.
If the node with key k has a right subtree, it is the leftmost node in that subtree.
Otherwise, if the node with key k is a left child it is the parent of this node.
Otherwise, find the closest ancestor of the node other than its immediate parent that has a right subtree it is the leftmost node in that subtree. If no such ancestor exists, the node was the maximum and does not have a successor.
Since the tree is balanced, you can always find it in O(log n).

Resources