Question

In a binary tree, the balance ratio of node v, bal(v), is the number of nodes...

In a binary tree, the balance ratio of node v, bal(v), is the number of nodes in the left subtree of node v divided by the sum of the number of nodes in the right and left subtrees of node v. bal(a leaf node) = ½. A tree T is said to be ε-balanced if, for all nodes v in T, ½ - ε < bal(v) < ½ + ε.

Design an efficient recursive algorithm that determines whether a binary tree is ε-balanced. What is the time complexity of your algorithm?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

  • The above algorithm receives a binary tree T and a constant ε.
  • If T is null(tree with no nodes), algorithm returns 0. That is, T is obviously ε-balanced.
  • If T is not null, then the algorithm is recursively called to determine whether the given binary tree is ε-balanced or not.
  • If T is ε-balanced , algorithm returns a non-negative number that represents the number of nodes in the given binary tree. Otherwise, algorithm returns -1.
  • If T contain only one node and it is ε-balanced, then algorithm returns 3, since algorithm considers null nodes also.
  • In each recursive call, algorithm finds the number of nodes in the left sub tree and the number of nodes in the right sub tree.
  • If either the left sub tree and right sub tree is not ε-balanced, then the algorithm returns -1.
  • Otherwise, it calculates balance ration(bal(v)) of the current node(v). if ½ - ε < bal_v < ½ + ε , then the algorithm returns the sum of the number of nodes in the right and left subtrees of node v plus 1. Otherwise, algorithm returns -1.

Time complexity:

  • The above algorithm traverses each node and calculates the balance ration of each node.
  • In the worst case(line 11-13), algorithm is called recursively on left sub tree and right subtree. Thus, the recurrence relation that represents the time complexity of the algorithm is as follows:

T(n)=T(k)+T(n-k-1)+c

Where, n=number of nodes in the binary tree, k=number of nodes in the left sub tree and n-k-1= number of nodes in the right sub tree. Also, T(0)=c

  • Guess the solution for the above recurrence. Assume T(n) ≤ cn =O(n)

Prove this using induction:

Base step: (c)0 + c= c = T(0) as defined above.

Inductive hypothesis: Suppose that T(m) ≤ (c )m + c for all m < n

Inductive step:

T(n) ≤ T(k) + T(nk−1) + c
      = (ck) + c(nk−1) + c
= c(k + nk − 1) + c
= c(n − 1) + c
= cnc+c
= cn

Therefore, the time complexity of the above algorithm is O(n).

Add a comment
Know the answer?
Add Answer to:
In a binary tree, the balance ratio of node v, bal(v), is the number of nodes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Let T be a proper binary tree. Given a node v ∈ T, the imbalance of...

    Let T be a proper binary tree. Given a node v ∈ T, the imbalance of v, denoted imbalance(v), is defined as the difference, in absolute value, between the number of leaves of the left subtree of v and the number of leaves of the right subtree of v. (If v is a leaf, then imbalance(v) is defined to be 0.) Define imbalance(T) = maxv∈T imbalance(v). (a) Provide an upper bound to the imbalance of a proper binary tree with...

  • 2. A regular binary tree is a binary tree whose internal nodes all have two subtrees...

    2. A regular binary tree is a binary tree whose internal nodes all have two subtrees (left and right). In other words, all their nodes have either zero subtrees (in which case they are leaves) or two subtrees (in which case they are internal nodes). Suppose that you have a boolean function that tells you, for each node of the tree, whether it is a leaf or not (call it: leaf(n), for node n). a) Write a recursive function that...

  • A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ

    Data structures C++1- A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ in height by no more than 1 Out of the following choices, which is the minimum set of nodes, if removed, will make the BST balanced?2- Which of the following is true for Red-Black Trees ? Select all choices that apply! Select one or more: a. For each node in the tree, all paths from that node to any leaf nodes contain...

  • /* * struct for a single node in a binary tree. data contains the int *...

    /* * struct for a single node in a binary tree. data contains the int * stored in this node. left and right contain pointers to the left and * right subtrees respectively. * * All of the ints stored in the left subtree is smaller than data. * All of the ints stored in the right subtree is larger than data. */ struct node { int data; struct node *left; struct node *right; }; typedef struct node node; Write...

  • Design a recursive algorithm that determines whether the number of leaf nodes of a Binary Search...

    Design a recursive algorithm that determines whether the number of leaf nodes of a Binary Search Tree (BST) is even or odd. An empty tree has an even number of leaves. A tree consisting of just a root has an odd number of leaves. Your function should return true for an even number of leaves, and false for an odd number of leaves. Analyze the execution time of your algorithm. Giving only the execution time without explanation will not be...

  • Design a recursive algorithm that determines whether the number of leaf nodes of a Binary Search...

    Design a recursive algorithm that determines whether the number of leaf nodes of a Binary Search Tree (BST) is even or odd. An empty tree has an even number of leaves. A tree consisting of just a root has an odd number of leaves. Your function should return true for an even number of leaves, and false for an odd number of leaves. Analyze the execution time of your algorithm. Giving only the execution time without explanation will not be...

  • By definition, the height of a node in a binary tree is the number of edges...

    By definition, the height of a node in a binary tree is the number of edges along the longest path from the node to any leaf. Assume the following node structure struct TreeNode int data; node Type right; // points to right child node Type "Left; // points to left child ) Write a recursive function that takes a pointer to a node in a binary tree and returns its height. Note: the height of a leaf node is 0...

  • (2 points) A full binary tree has a start node, internal nodes, and leaf nodes. The...

    (2 points) A full binary tree has a start node, internal nodes, and leaf nodes. The number of leaf nodes of this binary tree is 256. a) What is the height of the tree? b) How many internal nodes are in this tree?

  • Trees and Heaps 1. Show that the maximum number of nodes in a binary tree of...

    Trees and Heaps 1. Show that the maximum number of nodes in a binary tree of height h is 2h+1 − 1. 2. A full node is a node with two children. Prove that the number of full nodes plus one is equal to the number of leaves in a nonempty binary tree. 3. What is the minimum number of nodes in an AVL tree of height 15? 4. Show the result of inserting 14, 12, 18, 20, 27, 16,...

  • I need question 9-10 answered. Thank you Question 1 iShow the resulting binary search tree if...

    I need question 9-10 answered. Thank you Question 1 iShow the resulting binary search tree if we are to insert following elements into the tree in given order, [34, 12, 23, 27,31,9,11,45, 20, 37. i) Show the resulting balanced binary search tree if we are to insert following sorted elements into the tree, [9,12,21, 23, 29, 31, 34, 45, 48, 52, 55] iii What is the pre-order traversal of the balanced binary search tree? v) What is the post-order traversal...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT