AVL Tree Neil Tang 02032010 CS 223 Advanced

  • Slides: 16
Download presentation
AVL Tree Neil Tang 02/03/2010 CS 223 Advanced Data Structures and Algorithms 1

AVL Tree Neil Tang 02/03/2010 CS 223 Advanced Data Structures and Algorithms 1

Class Overview Ø Definition Ø Tree height Ø Tree rotation: single and double Ø

Class Overview Ø Definition Ø Tree height Ø Tree rotation: single and double Ø Insertion with rotations CS 223 Advanced Data Structures and Algorithms 2

Definition Ø An AVL tree is a special binary search tree in which for

Definition Ø An AVL tree is a special binary search tree in which for each node, the heights of its left and right subtree can differ by at most 1. CS 223 Advanced Data Structures and Algorithms 3

Definition CS 223 Advanced Data Structures and Algorithms 4

Definition CS 223 Advanced Data Structures and Algorithms 4

Height Ø Upper bound: 1. 44 log(N+2)-1. 328 Ø In practice, slightly more than

Height Ø Upper bound: 1. 44 log(N+2)-1. 328 Ø In practice, slightly more than log. N. Ø The complexity of an AVL tree operation is usually O(height). CS 223 Advanced Data Structures and Algorithms 5

Cases Causing Violation Ø Case 1 (left-left): An insertion to the left subtree of

Cases Causing Violation Ø Case 1 (left-left): An insertion to the left subtree of the left child of a node. Ø Case 2 (left-right): An insertion to the right subtree of the left child of a node. Ø Case 3 (right-left): An insertion to the left subtree of the right child of a node. Ø Case 4 (right-right): An insertion to the right subtree of the right child of a node. CS 223 Advanced Data Structures and Algorithms 6

Single Rotation To Fix Case 1 CS 223 Advanced Data Structures and Algorithms 7

Single Rotation To Fix Case 1 CS 223 Advanced Data Structures and Algorithms 7

Single Rotation To Fix Case 1 CS 223 Advanced Data Structures and Algorithms 8

Single Rotation To Fix Case 1 CS 223 Advanced Data Structures and Algorithms 8

Single Rotation To Fix Case 4 CS 223 Advanced Data Structures and Algorithms 9

Single Rotation To Fix Case 4 CS 223 Advanced Data Structures and Algorithms 9

Single Rotation To Fix Case 4 CS 223 Advanced Data Structures and Algorithms 10

Single Rotation To Fix Case 4 CS 223 Advanced Data Structures and Algorithms 10

Left-Right Double Rotation To Fix Case 2 CS 223 Advanced Data Structures and Algorithms

Left-Right Double Rotation To Fix Case 2 CS 223 Advanced Data Structures and Algorithms 11

Right-Left Double Rotation To Fix Case 3 CS 223 Advanced Data Structures and Algorithms

Right-Left Double Rotation To Fix Case 3 CS 223 Advanced Data Structures and Algorithms 12

Right-Left Double Rotation To Fix Case 3 CS 223 Advanced Data Structures and Algorithms

Right-Left Double Rotation To Fix Case 3 CS 223 Advanced Data Structures and Algorithms 13

Right Single Rotation CS 223 Advanced Data Structures and Algorithms 14

Right Single Rotation CS 223 Advanced Data Structures and Algorithms 14

Left-Right Double Rotation CS 223 Advanced Data Structures and Algorithms 15

Left-Right Double Rotation CS 223 Advanced Data Structures and Algorithms 15

Insertion with Rotations CS 223 Advanced Data Structures and Algorithms 16

Insertion with Rotations CS 223 Advanced Data Structures and Algorithms 16