Tải bản đầy đủ (.pdf) (82 trang)

Data Structure and Algorithms CO2003 Chapter 7 AVL Tree

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.67 MB, 82 trang )

Data Structure and Algorithms [CO2003]
Chapter 7 - AVL Tree

Lecturer: Duc Dung Nguyen, PhD.
Contact:
October 03, 2016
Faculty of Computer Science and Engineering
Hochiminh city University of Technology


Contents

1. AVL Tree Concepts
2. AVL Balance
3. AVL Tree Operations
4. Multiway Trees
5. B-Trees

1


Outcomes

• L.O.3.1 - Depict the following concepts: binary tree, complete binary tree, balanced binary
tree, AVL tree, multi-way tree, etc.
• L.O.3.2 - Describe the strorage structure for tree structures using pseudocode.
• L.O.3.3 - List necessary methods supplied for tree structures, and describe them using
pseudocode.
• L.O.3.4 - Identify the importance of “blanced” feature in tree structures and give
examples to demonstate it.
• L.O.3.5 - Identiy cases in which AVL tree and B-tree are unblanced, and demonstrate


methods to resolve all the cases step-by-step using figures.

2


Outcomes

• L.O.3.6 - Implement binary tree and AVL tree using C/C++.
• L.O.3.7 - Use binary tree and AVL tree to solve problems in real-life, especially related to
searching techniques.
• L.O.3.8 - Analyze the complexity and develop experiment (program) to evaluate methods
supplied for tree structures.
• L.O.8.4 - Develop recursive implementations for methods supplied for the following
structures: list, tree, heap, searching, and graphs.
• L.O.1.2 - Analyze algorithms and use Big-O notation to characterize the computational
complexity of algorithms composed by using the following control structures: sequence,
branching, and iteration (not recursion).

3


AVL Tree Concepts


AVL Tree

Definition
AVL Tree is:
• A Binary Search Tree,
• in which the heights of the left and right subtrees of the root differ by at most 1, and

• the left and right subtrees are again AVL trees.
Discovered by G.M.Adel’son-Vel’skii and E.M.Landis in 1962.

AVL Tree is a Binary Search Tree that is balanced tree.

4


AVL Tree

A binary tree is an AVL Tree if
• Each node satisfies BST property: key of the node is greater than the key of each node in
its left subtree and is smaller than or equals to the key of each node in its right subtree.
• Each node satisfies balanced tree property: the difference between the heights of the left
subtree and right subtree of the node does not exceed one.

5


AVL Tree

Balance factor
• left_higher (LH): HL = HR + 1
• equal_height (EH): HL = HR
• right_higher (RH): HR = HL + 1
(HL , HR : the heights of left and right subtrees)

6



AVL Trees
8

8

8

5

8

5

10
9

5
3

10
6

12

8
5

10

3

1

6
4

5

9
7
7


Non-AVL Trees
8

8

5

10

3

9
8
5

12
8


10

5
12

3
15

1

10
12
15
8


Why AVL Trees?

• When data elements are inserted in a BST in sorted order: 1, 2, 3, ...
BST becomes a degenerate tree.
Search operation takes O(n), which is inefficient.
• It is possible that after a number of insert and delete operations, a binary tree may
become unbalanced and inscrease in height.
• AVL trees ensure that the complexity of search is O(log2 n).

9


AVL Balance



Balancing Trees

• When we insert a node into a tree or delete a node from a tree, the resulting tree may be
unbalanced.
→ rebalance the tree.
• Four unbalanced tree cases:





left of left: a subtree of a tree that is left high has also become left high;
right of right: a subtree of a tree that is right high has also become right high;
right of left: a subtree of a tree that is left high has become right high;
left of right: a subtree of a tree that is right high has become left high;

10


Unbalanced tree cases

(Source: Data Structures - A Pseudocode Approach with C++)
11


Unbalanced tree cases

(Source: Data Structures - A Pseudocode Approach with C++)
12



Rotate Right

Algorithm rotateRight(ref root )
Exchanges pointers to rotate the tree right.
Pre: root is pointer to tree to be rotated
Post: node rotated and root updated
tempPtr = root->left
root->left = tempPtr->right
tempPtr->right = root
Return tempPtr
End rotateRight

13


Rotate Right

(Source: Data Structures - A Pseudocode Approach with C++)
14


Rotate Left

Algorithm rotateLeft(ref root )
Exchanges pointers to rotate the tree left.
Pre: root is pointer to tree to be rotated
Post: node rotated and root updated
tempPtr = root->right

root->right = tempPtr->left
tempPtr->left = root
Return tempPtr
End rotateLeft

15


Balancing Trees - Case 1: Left of Left
Out of balance condition created by a left high subtree of a left high tree
→ balance the tree by rotating the out of balance node to the right.

(Source: Data Structures - A Pseudocode Approach with C++)

16


Balancing Trees - Case 1: Left of Left

(Source: Data Structures - A Pseudocode Approach with C++)

17


Balancing Trees - Case 2: Right of Right

Out of balance condition created by a right high subtree of a right high tree
→ balance the tree by rotating the out of balance node to the left.

(Source: Data Structures - A Pseudocode Approach with C++)


18


Balancing Trees - Case 2: Right of Right

(Source: Data Structures - A Pseudocode Approach with C++)

19


Balancing Trees - Case 3: Right of Left
Out of balance condition created by a right high subtree of a left high tree
→ balance the tree by two steps:
1. rotating the left subtree to the left;
2. rotating the root to the right.

(Source: Data Structures - A Pseudocode Approach with C++)

20


Balancing Trees - Case 3: Right of Left

(Source: Data Structures - A Pseudocode Approach with C++)

21


Balancing Trees - Case 4: Left of Right

Out of balance condition created by a left high subtree of a right high tree
→ balance the tree by two steps:
1. rotating the right subtree to the right;
2. rotating the root to the left.

(Source: Data Structures - A Pseudocode Approach with C++)

22


×