DS286 | 2016-10-14 L18: Balanced & AVL...

20
Indian Institute of Science Bangalore, India भारतीय विान संथान बंगलौर, भारत Department of Computational and Data Sciences ©Department of Computational and Data Science, IISc, 2016 This work is licensed under a Creative Commons Attribution 4.0 International License Copyright for external content used with attribution is retained by their original authors CDS Department of Computational and Data Sciences DS286 | 2016-10-14 L18: Balanced & AVL Trees Yogesh Simmhan [email protected] Slides courtesy Venkatesh Babu, CDS

Transcript of DS286 | 2016-10-14 L18: Balanced & AVL...

Page 1: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

Indian Institute of ScienceBangalore, India

भारतीय विज्ञान संस्थान

बंगलौर, भारत

Department of Computational and Data Sciences

©Department of Computational and Data Science, IISc, 2016This work is licensed under a Creative Commons Attribution 4.0 International LicenseCopyright for external content used with attribution is retained by their original authors

CDSDepartment of Computational and Data Sciences

DS286 | 2016-10-14

L18: Balanced & AVL Trees

Yogesh Simmhans immhan@cds . i i sc . ac . in

Slides courtesy Venkatesh Babu, CDS

Page 2: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

Need for Balancing

BST have search, insert, delete complexity of O(h)

But h=n in worst case, i.e., operations are O(n)

Balancing scheme to limit the height of the tree

Balance Factor = |height(left subtree) – height(right subtree)|

21-Oct-16 2

Page 3: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

Approaches

Balance Factor of root is 0 or 1

21-Oct-16 3SE-286: Data Structures, Virendra Singh, IISc, 2010

Page 4: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

Approaches

Balance factor at every node is 0

21-Oct-16 4SE-286: Data Structures, Virendra Singh, IISc, 2010

Page 5: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

Approaches

Balance factor at every node is 0 or 1 … AVL Tree

21-Oct-16 5SE-286: Data Structures, Virendra Singh, IISc, 2010

Page 6: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

Approaches Skip Lists (!)

‣ Probabilistic, expect same # of nodes in each level‣ Simpler than tree balancing

“Self Balancing Trees”

Red-Black Tree‣ Nodes colored red or black. Root is black.‣ Red node must have black children.‣ Number of black nodes from root to leaf is same

AVL Trees (today)

B-Trees (next class)

21-Oct-16 6

Page 7: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

AVL Trees

Height Balanced Tree

Georgy Adelson-Velsky and Evgenii Landis‣ Soviet Scientists

‣ "An algorithm for the organization of information“, 1962

O(log(n)) insert, delete, search complexity

21-Oct-16 7https://en.wikipedia.org/wiki/AVL_tree

Page 8: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

Height Balancing

Balance Factor = |height(left subtree) – height(right subtree)|

AVT trees have a balance factor of 1

Store the heights of subtrees at each level

21-Oct-16 8https://www.cs.purdue.edu/homes/ayg/CS251/

Page 9: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

Height of an AVL Tree h=O(log n) for an AVL tree with n nodes

N(h) is the minimum number of nodes in an AVL tree of height h

N(1) = 1, N(2) = 2

Since the heights of a node’s children differ at most by 1

N(h) = 1 + N(h-1) + N(h-2)

Since N(h-1) > N(h-2)

N(h) > 2.N(h-2)

N(h) > 4.N(h-4)

N(h) > 2i.N(h-2i)

N(h) > 2h/2

h<2.log(N(h))

So, h = O(log(n))

21-Oct-16 9https://www.cs.purdue.edu/homes/ayg/CS251/

Page 10: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

Repairing AVL Trees

Insertions or deletions can cause the height balance property to be violated‣ Balance factor may become 2

‣ Not more than that for a single insert/delete

Repair the tree when imbalance is encountered‣ Go up the tree to parent or grandparent

‣ “Rotate the nodes” to restore balanced property

21-Oct-16 10

Page 11: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

Repairing AVL Trees

21-Oct-16 11https://www.cs.purdue.edu/homes/ayg/CS251/

Page 12: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

Repairing AVL Trees

21-Oct-16 12https://www.cs.purdue.edu/homes/ayg/CS251/

Page 13: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

AVL Rotations: Outside Case, Single Rotation

After single rotation, the new height of the entire subtree is exactly the same as the height of the original subtree prior to the insertion of the new data item

21-Oct-16 13https://www.cs.purdue.edu/homes/ayg/CS251/SE-286: Data Structures, Virendra Singh, IISc, 2010

Page 14: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

AVL Rotations: Outside Case, Single Rotation

21-Oct-16 14https://www.cs.purdue.edu/homes/ayg/CS251/

Page 15: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

AVL Rotations: Inside Case, Double Rotation

21-Oct-16 15https://www.cs.purdue.edu/homes/ayg/CS251/

Page 16: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

AVL Rotations: Inside Case, Double Rotation

21-Oct-16 16https://www.cs.purdue.edu/homes/ayg/CS251/

As with the single rotations, double rotations restore the height of the subtree to what it was before the insertion.

SE-286: Data Structures, Virendra Singh, IISc, 2010

Page 17: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

AVL Rotations: Double Rotation = 2 Single Rotations

21-Oct-16 17SE-286: Data Structures, Virendra Singh, IISc, 2010

Page 18: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

AVL Rotations: Deletion

21-Oct-16 18https://www.cs.purdue.edu/homes/ayg/CS251/

Page 19: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

CDS.IISc.ac.in | Department of Computational and Data Sciences

Tasks Self study‣ Read: AVL Rotations (online sources)

Finish Assignment 4 by Wed Oct 26 (75 points)

Make progress on CodeChef (100 points)

21-Oct-16 19

Page 20: DS286 | 2016-10-14 L18: Balanced & AVL Treescds.iisc.ac.in/wp-content/uploads/DS286.AUG2016.L18.AVLTrees.pdfSkip Lists (!) ‣Probabilistic, expect same # of nodes in each level ‣Simpler

©Department of Computational and Data Science, IISc, 2016This work is licensed under a Creative Commons Attribution 4.0 International LicenseCopyright for external content used with attribution is retained by their original authors

CDS.IISc.ac.in | Department of Computational and Data Sciences

Department of Computational and Data Sciences

Questions?

21-Oct-16 20