Question

True or False questions: 1. A balanced, unsorted tree is more efficient than a sorted, unbalanced...

True or False questions:

1. A balanced, unsorted tree is more efficient than a sorted, unbalanced tree.

2. You should build a tree first, adding all the required elements before applying a balancing algorithm over the structure.

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

Answer;-

1. A balanced, unsorted tree is more efficient than a sorted, unbalanced tree.

ans: true

expalnation:-

A binary tree doesn't inherently have to be sorted. In this case you'd use the 0th index to create the root, the first to create the left branch, the second to create the right branch, and so forth and so on proceeding left to right or right to left as you prefer down the depth of the tree until your input runs out of numbers.

You could do that, and recently in an interview I was asked a similar question. I ended the interview when the interviewer couldn't provide a satisfactory answer as to why the hell the tree wasn't sorted in the first place, since a significant part of the value of having a binary tree is to use it for sorting values when they are added in a minimal number of computations.

A binary tree doesn't necessarily needs to be sorted, so you can follow any pattern you want and choose a root point and start building the tree through iteration or recursion of the array.

However, if you want a binary search tree (in which for every node, left child is smaller and right child is bigger in value), you can use different tree construction algorithms. The most simple one would be to sort the array and choose the mid-point to be the root, and start constructing the tree by continuously dividing the array into two parts and choosing the mid-point to be the root. In the end, you'll have a tree, which is made up of smaller trees and the roots of each tree were the mid-points of the continuously divided arrays, and where each node is bigger in value than it's left child, while smaller in value than the right child

You should build a tree first, adding all the required elements before applying a balancing algorithm over the structure.

ans:true

explanation:-

AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes.

An Example Tree that is an AVL Tree

The above tree is AVL because differences between heights of left and right subtrees for every node is less than or equal to 1.

An Example Tree that is NOT an AVL Tree

The above tree is not AVL because differences between heights of left and right subtrees for 8 and 18 is greater than 1.


Why AVL Trees?
Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST. The cost of these operations may become O(n) for a skewed Binary tree. If we make sure that height of the tree remains O(Logn) after every insertion and deletion, then we can guarantee an upper bound of O(Logn) for all these operations. The height of an AVL tree is always O(Logn) where n is the number of nodes in the tree (See this video lecture for proof).


Insertion
To make sure that the given tree remains AVL after every insertion, we must augment the standard BST insert operation to perform some re-balancing. Following are two basic operations that can be performed to re-balance a BST without violating the BST property (keys(left) < key(root) < keys(right)).
1) Left Rotation
2) Right Rotation

T1, T2 and T3 are subtrees of the tree 
rooted with y (on the left side) or x (on 
the right side)           
     y                               x
    / \     Right Rotation          /  \
   x   T3   - - - - - - - >        T1   y 
  / \       < - - - - - - -            / \
 T1  T2     Left Rotation            T2  T3
Keys in both of the above trees follow the 
following order 
 keys(T1) < key(x) < keys(T2) < key(y) < keys(T3)
So BST property is not violated anywhere.

Steps to follow for insertion
Let the newly inserted node be w
1) Perform standard BST insert for w.
2) Starting from w, travel up and find the first unbalanced node. Let z be the first unbalanced node, y be the child of z that comes on the path from w to z and x be the grandchild of z that comes on the path from w to z.
3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z. There can be 4 possible cases that needs to be handled as x, y and z can be arranged in 4 ways. Following are the possible 4 arrangements:
a) y is left child of z and x is left child of y (Left Left Case)
b) y is left child of z and x is right child of y (Left Right Case)
c) y is right child of z and x is right child of y (Right Right Case)
d) y is right child of z and x is left child of y (Right Left Case)

Following are the operations to be performed in above mentioned 4 cases. In all of the cases, we only need to re-balance the subtree rooted with z and the complete tree becomes balanced as the height of subtree (After appropriate rotations) rooted with z becomes same as it was before insertion. (See this video lecture for proof)

Add a comment
Know the answer?
Add Answer to:
True or False questions: 1. A balanced, unsorted tree is more efficient than a sorted, unbalanced...
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
  • True or false? (a) An insertion in an AVL tree with n nodes requires Θ (log(n))...

    True or false? (a) An insertion in an AVL tree with n nodes requires Θ (log(n)) rotations. (b) A set of numbers are inserted into an empty BST in sorted order and inserted into an empty AVL tree in random order. Listing all elements in sorted order from the BST is O (n), while listing them in sorted order from the AVL tree is O (log(n)). (c) If items are inserted into an empty BST in sorted order, then the...

  • (true/false) All nodes in the right subtree of a node must be smaller in value than...

    (true/false) All nodes in the right subtree of a node must be smaller in value than their parent (true/false) Each node in a binary search tree may contain zero, one, or two children nodes. (true/false) It is possible to recursively process a binary search tree to print all the data in a binary search tree in sorted order. (true/false) The value of a parent must be less than all its children. (true/false) All nodes in the right subtree of a...

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

  • Answer the following short questions: 1. True or False: In the last example, we sorted left...

    Answer the following short questions: 1. True or False: In the last example, we sorted left subarray first, right subarray second. However, the order does not matter. If we wanted to, we could split and merge the right subarray first. 2.Consider array [5,3,4,1]. What does it look like just before the final merging step of Merge Sort? a)5,3,4,1 b)3,5,1,4 c)5,3,1,4 d)1,3,4,5 3.Although on average, Merge Sort is a lot faster than Bubble Sort. Suppose we have an array small enough...

  • Balanced Trees Identify the correctness of each of the following statements by marking either a T...

    Balanced Trees Identify the correctness of each of the following statements by marking either a T for true of F for false 1. (1 point)A balanced tree is exclusively defined as one in which the height of each sub-tree (or child) differs by no more than one (1). 2. (1 point)In a red-black tree, after rotating three nodes, the two children will each be red. 3. (1 point) One will only ever need to perform one rotation or color-flip in...

  • C. 7. True/False Questions. (2 points each) a. Applying Horner's Rule, an n-degree polynomial can be...

    C. 7. True/False Questions. (2 points each) a. Applying Horner's Rule, an n-degree polynomial can be evaluated at a given point using only n multiplications and n additions. b. Quick Sort and Merge Sort are comparison-based sorting algorithms. Heap Sort and Distribution Counting Sort are not comparison-based sorting algorithms. An AVL tree applies four types of rotations: RIGHT, LEFT, RIGHT-LEFT, and LEFT-RIGHT. d. When an AVL tree's left sub-tree is left-heavy, a LEFT rotation is needed. e. When an AVL...

  • A balanced Roulette wheel should come up green 1/19 of the spins. To check whether a wheel is balanced, we'll spin it 40 times, and pronounce it unbalanced if it comes up green more than 3 times....

    A balanced Roulette wheel should come up green 1/19 of the spins. To check whether a wheel is balanced, we'll spin it 40 times, and pronounce it unbalanced if it comes up green more than 3 times. State the appropriate hypotheses. What is the probability that a fair wheel will be found to be unbalanced (you may express your answer as an unsimplified summation)? For a wheel that comes up green 1/15th of the time, what is the probability that...

  • Questions Q/ Some medications have more than one name. Select one: a) True b) False Q/...

    Questions Q/ Some medications have more than one name. Select one: a) True b) False Q/ PRN medications have may be more than one name. Select one: a) True b) False Q/ A topical medication is given by injection. Select one: a) True b) False Q/ Prescription medications can be ordered by a social worker. Select one: a) True b) False Q/ Please match the following abbreviations associated with medication administration to their specific directions HS a) As needed b)...

  • Indicate whether each of the following statements is true or false. Questions with more than one...

    Indicate whether each of the following statements is true or false. Questions with more than one answer selected will be marked as incorrect. 1.Under IFRS, contingent liabilities should be recorded in the accounts if there is a remote possibility that the contingency will actually occur. True False 2. The classification of a liability as current or non-current is important because it may affect the evaluation of a company’s liquidity. True False 3. The liability of a shareholder is usually limited...

  • Implement the Unsorted, sorted and indexed lists using Linked List base structure. (LLNode class under support...

    Implement the Unsorted, sorted and indexed lists using Linked List base structure. (LLNode class under support package) Files required: 1. ListInterface.java (given) 2. LLUnsortedList.java (implements ListInterface.java) 3. LLSortedList.java (extends LLUnsortedList.java, implements ListInterface.java) 4. LLIndexedList.java (implements LLIndexedListInterface.java, extends LLUnsortedList.java) 5. Driver ** Maybe create LLIndexedListInterface.java file too even though it is not required file! I need the bolded Java Files (5) to be written separate  classes please! The interfaces can be implemented from below! Thank you zip all your files and submit....

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