Question

I need Help Plz in java search trees in Java Design an algorithm that converts any search tree into an AVL tree in linea...

I need Help Plz in java

search trees in Java
Design an algorithm that converts any search tree into an AVL tree in linear time pleas with comments. You can assume that the search tree 2k-1. k ∈ N, Knot owns

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

class BST( object ):

def __init__( self ):

self.root = None

def getRoot( self ):

return self.root

def DSW( self ):

if None != self.root:

self.createBackbone() # effectively: createBackbone( self.root)

self.createPerfectBST() # effectively: createPerfectBST( self.root)

#=====================================================================

# Time complexity: O(n)

#=====================================================================

def createBackbone( self ):

grandParent = None

parent = self.root

leftChild = None

while None != parent:

leftChild = parent.left

if None != leftChild:

grandParent = self.rotateRight( grandParent, parent, leftChild )

parent = leftChild

else:

grandParent = parent

parent = parent.right

#=======================================================================

# Before After

# Gr Gr

# \ \

# Par Ch

# / \ / \

# Ch Z X Par

# / \ / \

# X Y Y Z

#=======================================================================

def rotateRight( self, grandParent, parent, leftChild ):

if None != grandParent:

grandParent.right = leftChild

else:

self.root = leftChild

parent.left = leftChild.right

leftChild.right = parent

return grandParent

#=======================================================================

# Time complexity: O(n)

#=======================================================================

def createPerfectBST( self ):

n = self.size()

# m = 2^floor[lg(n+1)]-1, ie the greatest power of 2 less than n: minus 1

m = self.greatestPowerOf2LessThanN( n + 1 ) - 1

self.makeRotations( n - m )

while m > 1:

m /= 2

self.makeRotations( m )

#=======================================================================

# Time complexity: log(n)

#=======================================================================

def greatestPowerOf2LessThanN( self, n ):

x = self.MSB( n ) # MSB

return ( 1 << x ) # 2^x

#=======================================================================

# Time complexity: log(n)

# return the index of most significant set bit: index of

# least significant bit is 0

#=======================================================================

def MSB( self, n ):

ndx = 0

while 1 < n:

n = ( n >> 1 )

ndx += 1

return ndx

def makeRotations( self, bound ):

grandParent = None

parent = self.root

child = self.root.right

while bound > 0:

try:

if None != child:

self.rotateLeft( grandParent, parent, child );

grandParent = child;

parent = grandParent.right;

child = parent.right;

else:

break

except AttributeError: # TypeError

break

bound -= 1

def rotateLeft( self, grandParent, parent, rightChild ):

if None != grandParent:

grandParent.right = rightChild

else:

self.root = rightChild

parent.right = rightChild.left

rightChild.left = parent

PSEUDO CODE:

1.Allocate a node, the "pseudo-root", and make the tree's actual root the right child of the pseudo- root.
2.Call tree-to-vine with the pseudo-root as its argument.
3.Call vine-to-tree on the pseudo-root and the size (number of elements) of the tree.
4.Make the tree's actual root equal to the pseudo-root's right child.
5.Dispose of the pseudo-root.

Add a comment
Know the answer?
Add Answer to:
I need Help Plz in java search trees in Java Design an algorithm that converts any search tree into an AVL tree in linea...
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
  • just need to answer the second question 3 AVL Trees Assume the following notation/operations on AVL trees....

    just need to answer the second question 3 AVL Trees Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes: . The key T.key is the root node's key. The left child T.left is T's left subtree, which is an AVL tree (possibly E). The right child T.right is T's right subtree, which is an AVL tree (possibly E) [3 marks] Describe an alternative version of the RANGECOUNT(T,...

  • Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three...

    Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes The key T.key is the root node's key. The left child T.left is Ts left subtree, which is an AVL tree (possibly E). The right child T.right is T's right subtree, which is an AVL tree (possibly E). (a) 5 marsl Write a function RANGECOUNT(T, lo, hi) to count the number of nodes in an AVL tree with...

  • Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three...

    Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes: • The key T.key is the root node’s key. • The left child T.left is T’s left subtree, which is an AVL tree (possibly E). • The right child T.right is T’s right subtree, which is an AVL tree (possibly E). (a) [5 marks] Write a function RangeCount(T, lo, hi) to count the number of nodes in an...

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

  • Instead of using a linked list to resolve collisions, as in separate chaining, use a binary search tree. That is, create...

    Instead of using a linked list to resolve collisions, as in separate chaining, use a binary search tree. That is, create a hash table that is an array of trees. To display a small tree-based hash table, you could use an inorder traversal of each tree. The advantage of a tree over a linked list is that it can be searched in O(logN) instead of O(N) time. This time savings can be a significant advantage if very high load factors...

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

  • I need help with my programming assignment. The language used should be java and the algorithm...

    I need help with my programming assignment. The language used should be java and the algorithm should use search trees so that you play against the computer and he chooses the best move. The tree should have all possibilities on the leaves and you could use recursion to so that it populates itself. The game can be a 3*3 board (no need the make it n*n). Please put comments so that I can understand it. Thanks The game of ‘Walls’...

  • I need Help Plz In a tree, the leaves are called external nodes. Accordingly, internal nodes...

    I need Help Plz In a tree, the leaves are called external nodes. Accordingly, internal nodes are exactly the nodes that are not external nodes. An edge or connection exists between two nodes if the two nodes are in `` father-child relationship ''. A true binary tree is a tree with the property that every internal node has exactly two children. Prove the following two sentences for nonempty real binary trees: a) A non-empty real binary tree with N internal...

  • Need help with 5.25. I have attached the definition of the i-th binomial tree. fundamentals of...

    Need help with 5.25. I have attached the definition of the i-th binomial tree. fundamentals of algorithmics.pdf (page 200 of 530) Q Search Protiom 8.24. For heapsort, what are the best and the worst initial arrangements of the elements to be sorted, as far as the execution time of the algorithm is concerned? Justify your answer Problem 5.25. Prove that the binomial tree B defined in Section 5.8 contains 2 nodes, of which (i) are at depth k, 0 s...

  • I need it in Java please. please help asap. TIA. Give a linear-time algorithm for reversing...

    I need it in Java please. please help asap. TIA. Give a linear-time algorithm for reversing a string.

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