# Lecture13: Tree III Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

## Presentation on theme: "Lecture13: Tree III Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)"— Presentation transcript:

Lecture13: Tree III Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Order matters Difference between these binary search trees?  Insert: 4, 2, 1, 3, 6, 5, 7  Insert: 1, 2, 3, 4, 5, 6, 7 2 4 2 31 6 75 1 5 3 2 4 7 6

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Complexity of Operations 3

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Self‐balancing Trees Variants of binary search tree Self‐balancing  Additional rules for how to structure tree  Needs operations to move nodes around Constraints on height differences of leaves Ensures that tree does not degenerate to list. 4

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 AVL Tree 5 44 17 32 48 78 8850 62 4 2 1 3 1 2 11 AVL tree

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Insertion is as in a binary search tree. 6 44 17 32 48 78 8850 62 4 2 1 3 1 2 11 Before insertion After insertion 44 17 32 48 78 8850 62 5 2 1 4 1 3 21 54 1

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion 7 44 17 32 48 78 8850 62 5 2 1 4 1 3 21 54 1 b=x a=y c=z

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Restructuring Single Rotation: 8 h h-1 h-2 h-3 h-1 h-2 h-3 h-2

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Restructuring Double rotations: 9 h h-1 h-2 h-3 h-2 h-1 h-3 ≤h-3 h-2 ≤h-3

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Example 10 44 17 32 48 78 8850 62 5 2 1 4 1 3 21 54 1 b=x a=y c=z After insertion unbalanced After the first rotation 44 17 32 50 78 8862 54 5 2 1 4 1 3 1 2 48 1 a=y b=x c=z

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Insertion Example 11 44 17 32 48 62 88 50 54 4 2 1 3 2 2 11 78 1 c=z a=y b=x After the first rotation After the second rotation 44 17 32 50 78 8862 54 5 2 1 4 1 3 1 2 48 1 a=y b=x c=z

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Removal 12 44 17 32 48 62 88 50 54 4 2 1 3 2 2 11 78 1 Before deletion of 32 44 17 48 62 88 50 54 4 1 3 2 2 11 78 1 After deletion of 32

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Reconstructing after a Removal 13 44 17 48 62 88 50 54 4 1 3 2 2 11 78 1 z x y w

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Reconstructing after a Removal Recursive reconstructing  Restructuring may upset the balance of another node higher in the tree.  We must continue checking for balance until the root is reached. 14 44 17 48 62 88 50 54 4 1 3 2 2 11 78 1 z w y w 62 44 48 78 50 54 4 3 2 1 2 11 88 1 y x z 17 x

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 AVL Tree Performance 15

16

Download ppt "Lecture13: Tree III Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)"

Similar presentations