Question

Show that any binary search tree with n nodes can be transformed into any other search...

Show that any binary search tree with n nodes can be transformed into any other search tree using O(n) rotations. Also show that you need at most n - 1 right rotations to transform a tree into a chain.

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

The idea is to construct an algorithm that converts any n-node binary tree into a left chain in time O(n) and then use the inverse of this algorithm to convert the obtained left chain into the desired binary tree.

Algorithm that converts any n-node binary tree into a left chain in time O(n).

Algorithm:

  1. Perform left rotations on the root node until its right child is empty.
  2. If the left child is now empty we are done. If the left child is not empty we traverse down to the left and go back to step 1 to perform rotations on this subtree.

Time:

There are n nodes in the binary tree. A rotation takes time O(1). Each time we do a rotation the height of the left chain that we are building grows with at least one. Therefore the algorithm will terminate after at most n rotations giving us time O(n).

Correctness:

The algorithm traverses down to the left doing rotations. It traverses down after making sure that the right child is empty. Therefore the above nodes are allways making up a left chain since we do not alter them after traversing downwards. When the algorithm finishes we clearly have a correct left chain.

Algorithm that converts a left chain into any n-node binary tree in time O(n).

Algorithm:

Looking at the above algorithm we can for each n-node binary tree define an algorithm that converts a left chain into this tree. Instead of left rotations we perform right rotations. We perform these rotations in reverse order.

Time:

Clearly O(n) (the same as the algorithm above).

Correctness:

Since the first algorithm is correct its inverse, defined in the way we have defined it, is also correct.

NOTE: As per Chegg policy, I am allowed to answer only 1 long question (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
Show that any binary search tree with n nodes can be transformed into any other search...
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
  • a. How can I show that any node of a binary search tree of n nodes...

    a. How can I show that any node of a binary search tree of n nodes can be made the root in at most n − 1 rotations? b. using a, how can I show that any binary search tree can be balanced with at most O(n log n) rotations (“balanced” here means that the lengths of any two paths from root to leaf differ by at most 1)?

  • Show that the tree height of a height-balanced binary search tree with n nodes is O(log...

    Show that the tree height of a height-balanced binary search tree with n nodes is O(log n). (Hint: Let T(h) denote the fewest number of nodes that a height-balanced binary search tree of height h can have. Express T(h) in terms of T(h-1) and T(h-2). Then, find a lower bound of T(h) in terms of T(h-2). Finally, express the lower bound of T(h) in terms of h.)

  • In regards to binary search tree, can you answer why a BST with N nodes has...

    In regards to binary search tree, can you answer why a BST with N nodes has at least log2N levels and at most N levels. so the runtime complexity is best case 0(logN) and worst case 0(N). Can you explain this with the following numbers in this order? 7,1,64,28,77

  • Problem 2 [35 points (155 15)]: Let Ti and T2 be two binary search trees containing the same elem...

    Problem 2 [35 points (155 15)]: Let Ti and T2 be two binary search trees containing the same elements. In this problem, you will show how to transform Ti into T2 (i.e. Ti is altered to now have the same structure as T2) through a series of rotation operations. (a) Define a binary tree to be a right-going chain if no node in the tree has a left child (in other words, the tree is a linked list formed through...

  • A binary search tree includes n nodes and has an height h. Check all that applies...

    A binary search tree includes n nodes and has an height h. Check all that applies about the space complexity of TREE-MINIMUMX) TREE-MINIMUM () 1 while x. left NIL 2 3 return x x x.left O it is e (lg n) ■ it is 0(h). D it is e (1) ■ It is in place ■ it is Θ (n) A binary search tree includes n nodes and has an height h. Check all that applies about the space complexity...

  • A Binary Search Tree is a binary tree where nodes are ordered in the following way:...

    A Binary Search Tree is a binary tree where nodes are ordered in the following way: each node contains one key (also known as data) the keys in the left subtree are less than the key in its parent node the keys in the right subtree are greater than the key in its parent node duplicate keys are not allowed Create a Binary Search Tree inserting the following list of numbers in order from left to right. 10,          6,           4,            8,            18,          15,          21 Please type...

  • IN JAVA create a binary search tree gui where you can insert and remove nodes, also...

    IN JAVA create a binary search tree gui where you can insert and remove nodes, also move around the tree, thank you. The simpler the better.

  • A collection of nodes is arranged as a binary search tree ordered on the field INFO which contain...

    A collection of nodes is arranged as a binary search tree ordered on the field INFO which contains distinct positive integers for each of the nodes. In addition to INFO, LLINK and RLINK, each node has three other fields CLASS SUCC and PRED CLASS is an information field containing a single letter that denotes the class to which the node belongs (there being up to 26 classes). The nodes in each class are arranged as a doubly-linked circular list 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...

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

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