Download presentation

Presentation is loading. Please wait.

Published byEileen Worman Modified over 3 years ago

1
Rotating Nodes How to balance a node that has become unbalanced after the addition of one new node.

2
Detect Imbalance If ( a new node is added to either of node N's children ) rebalance( N )

3
Node rebalance( Node N ) int diff = N.left.getHeight() - N.right.getHeight(); if ( diff > 1 ) if ( N.left.left.getHeight() > N.left.right.getHeight() ) N = rotateRight( N ) else N = rotateLeftRight( N ) else if ( diff < -1 ) if ( N.right.right.getHeight() > N.right.left.getHeight() ) N = rotateLeft( N ) else N = rotateRightLeft( N ) // else, no rebalancing necessary return N

4
Balanced Search Trees 6060 7575 40 50 705 85 4520 6060 7575 50 705 85 6060 6565 50 5

5
UnBalanced Search Trees 6060 50 5 6060 7575 40 50 20 2525 6060 7575 50 6585 7070 3 2 2

6
Unbalanced as result of adding a new node to the left subtree of the left subtree return results of a single rotate right 45 50 40 unbalanced Node rotateRight( Node nodeN ) nodeC = nodeN.left nodeN.left = nodeC.right nodeC.right = nodeN return nodeC nodeN nodeC balanced 45 50 40

7
Unbalanced as result of adding a new node to the right subtree of the right subtree return results of a single rotate left 45 40 50 unbalanced Node rotateLeft( Node nodeN ) nodeC = nodeN.right nodeN.right = nodeC.left nodeC.left = nodeN return nodeC nodeN nodeC balanced 45 50 40

8
Unbalanced as result of adding a new node to the left subtree of the right subtree return results of a right-left rotate Node rotateRightLeft( Node nodeN ) nodeC = nodeN.right nodeN.right = rotateRight(nodeC) return rotateLeft(nodeN) 50 40 45 unbalanced nodeN nodeC balanced 45 40 50 45 50 40

9
Unbalanced as result of adding a new node to the right subtree of the left subtree return results of a left-right rotate Node rotateLeftRight( Node nodeN ) nodeC = nodeN.left nodeN.left = rotateLeft(nodeC) return rotateRight(nodeN) balanced 40 50 45 unbalanced nodeN nodeC 45 50 40 45 50 40

Similar presentations

OK

Balanced Search Trees Chapter 28. 2 Chapter Contents AVL Trees Single Rotations Double Rotations Implementation Details 2-3 Trees Searching Adding Entries.

Balanced Search Trees Chapter 28. 2 Chapter Contents AVL Trees Single Rotations Double Rotations Implementation Details 2-3 Trees Searching Adding Entries.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on business etiquettes training mask Electronics ieee papers ppt on sustainable buildings Ppt on symbols of elements Ppt on power system protection and switchgear Ppt on instrument landing systems Ppt on nitrogen cycle and nitrogen fixation equation View ppt online Ppt on eye os mean Ppt on eia report on natural gas Ppt on aluminium form work