Chapter 45 AVL Trees and Splay Trees
Chapter 45 AVL Trees and Splay Trees
Chapter 45 AVL Trees and Splay Trees
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Objectives
To
know what an AVL tree is (45.1). To understand how to rebalance a tree using the LL rotation, LR rotation, RR rotation, and RL rotation (45.2). To know how to design the AVLTree class (45.3). To insert elements into an AVL tree (45.4). To implement node rebalancing (45.5). To delete elements from an AVL tree (45.6). To implement the AVLTree class (45.7). To test the AVLTree class (45.8). To analyze the complexity of search, insert, and delete operations in AVL trees (45.9).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Balance Factor/Left-Heavy/Right-Heavy
The process for inserting or deleting an element in an AVL tree is the same as in a regular binary search tree. The difference is that you may have to rebalance the tree after an insertion or deletion operation. The balance factor of a node is the height of its right subtree minus the height of its left subtree. A node is said to be balanced if its balance factor is -1, 0, or 1. A node is said to be left-heavy if its balance factor is -1. A node is said to be right-heavy if its balance factor is +1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Balancing Trees
If a node is not balanced after an insertion or deletion operation, you need to rebalance it. The process of rebalancing a node is called a rotation. There are four possible rotations.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
0 or 1
-1 or 0
B T3 h
A h+1 T1
0 or -1
h+1
T1
T2
T3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
0 or -1
B h T3
+1 or 0
0 or +1
A T1 h+1
h
T2 T1 h+1
T3
T2
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
+1
B T4 C -1, 0, or 1 h
0 or -1
0 or 1
T1 T2 and T3 may have different height, but at least one' must have height of h.
T1
T2
T3
T4
T2
T3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
10
-1 h T1 0, -1, or 1 C
0 or -1
0 or 1
T4
T1
T2
T3
T4
T2
T3
T2 and T3 may have different height, but at least one' must have height of h.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
11
BianryTree<E>
m 0
AVLTreeNode<E>
height: int
AVLTree<E>
+A VLTree() +A VLTree(objects: E[]) #createNewNode(): A VLTreeNode<E> +insert(e: E): boolean Creates a default A VL tree. Creates an AVL tree fro m an array of objects. Override this method to create an AVLTreeNode. Returns true if the element is added successfully. Returns true if the element is removed fro m the tree successfully. Resets the height of the specified node. Balances the nodes in the path from the node for the element to the root if needed. Returns the balance factor of the node. Performs LL balance. Performs LR balance. Performs RR balance. Performs RL balance.
1 Link
+delete(e: E): boolean -updateHeight(node: AVLTreeNode<E>): void -balancePath(e: E): void -balanceFactor(node: AVLTreeNode<E>): int -balanceLL(A: TreeNode, parentOfA: TreeNode<E>): void -balanceLR(A: TreeNode<E>, parentOfA: TreeNode<E>): void -balanceRR(A : TreeNode<E>, parentOfA: TreeNode<E>): void -balanceRL(A: TreeNode<E>, parentOfA: TreeNode<E>): void
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
13
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
14
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
15