Cấu Trúc Dữ Liệu AVL_Tree

9 126 0
Cấu Trúc Dữ Liệu AVL_Tree

Đang tải... (xem toàn văn)

Thông tin tài liệu

AVL Trees • Balanced binary search trees • For every node in an AVL tree, the heights of its left and right subtrees can differ by no more than 1. N L R N L R N L R hh hh hh h+1h+1 h+1h+1 hh Balance Factors • Every node in an AVL tree has a balance factor. • bf N = height(R) – height(L) N L R N L R N L R N L R hh hh 0 hh h+1h+1 1 h+1h+1 hh -1 0 1 -1 Balancing Operations • Only insertion and deletion can affect balance. • All imbalances in an AVL tree involve some local three-node neighborhood and can be repaired within the confines of that neighborhood. A B C Left rotation B A C A B C Right rotation B C A A B C Right rotation C A B A C B Left rotation A B C Left rotation A C B Right rotation C B A Balancing Operations • Only insertion and deletion can affect balance. • All imbalances in an AVL tree involve some local three-node neighborhood and can be repaired within the confines of that neighborhood. 5 10 15 Left rotation 10 5 15 15 10 5 Right rotation 10 5 15 5 15 10 Right rotation 10 5 15 5 10 15 Left rotation 15 5 10 Left rotation 15 10 5 Right rotation 10 5 15 Subtree Displacement During Rotations A B C Left rotation B A C T1 T2 T3 T4 0 1 2 T1 T2 T3 T4 0 0 0 A B C Left rotation A C B Right rotation T1 T2 T3 T4 T1 T2 T3 T4 C B A T1 T2 T3 T4 0 0 0 0 1 -2 Building an AVL Tree Insert: 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55 Insert 10 10 Insert 85 85 10 Insert 15 15 85 10 15 10 85 15 10 85 0 -1 2 Insert 70 15 10 85 70 Insert 20 15 10 85 70 20 15 10 70 20 85 0 -1 -2 Insert 60 15 10 70 20 85 60 15 10 20 85 70 60 1 -1 2 15 10 20 85 70 60 Final Tree: 60 20 70 10 40 65 85 5 15 30 50 80 90 55 Deleting from an AVL Tree • After any insertion, at most one balancing repair would be needed. (This may be a double rotation, but it’s just one repair.) • After a deletion, there could be several repairs needed, but they will all be on the path from the deletion point to the root. 60 20 70 10 40 65 85 5 15 30 50 80 90 55 Delete 70: 60 20 65 10 40 85 5 15 30 50 80 90 55 60 20 85 10 40 90 5 15 30 50 65 80 55 Delete 80: 60 20 85 10 40 90 5 15 30 50 65 55 1 -2 60 40 85 20 50 90 10 30 5 55 65 15 40 20 60 10 30 85 5 15 3050 50 55 Delete 20 from the following AVL Tree: 13 5 16 3 8 15 19 1 4 7 11 17 20 122 6 10 9 14 18 After two imbalance repairs (both double rotations): 8 135 161173 1815121041 9 1714 19 6 2 . AVL Trees • Balanced binary search trees • For every node in an AVL tree, the heights of its left and right subtrees can differ by no more than 1. N L. 70 20 85 60 15 10 20 85 70 60 1 -1 2 15 10 20 85 70 60 Final Tree: 60 20 70 10 40 65 85 5 15 30 50 80 90 55 Deleting from an AVL Tree • After any insertion, at most one balancing repair would. R hh hh 0 hh h+1h+1 1 h+1h+1 hh -1 0 1 -1 Balancing Operations • Only insertion and deletion can affect balance. • All imbalances in an AVL tree involve some local three-node neighborhood and can be repaired within the confines of that

Ngày đăng: 03/07/2014, 15:00

Tài liệu cùng người dùng

Tài liệu liên quan