COP2532 – Chapter 1: Introduction

Download Report

Transcript COP2532 – Chapter 1: Introduction

INTRODUCTION TO AVL
TREES
P. 839 – 854
INTRO
Review of Binary Trees:
– Binary Trees are useful for quick retrieval of items
stored in the tree
– order of items in the tree is important factor of how
quickly this retrieval is done
• if well organized, items can be handled quickly
• if not well structured, retrieval can be cumbersome
• ex of good tree:
3
1
0
5
4
8
INTRO (cont.)


since items well-distributed, search time is O(log n), with n being
number of nodes in tree
ex of not-so-good tree:
0
2
4
8


since items are all right-children of previous node, search time is
O(n) in worst case
there exists a method of developing binary-trees that maintain the
O(log n) retrieval time by making sure that the nodes of the tree are
balanced (height is maintained)
–
these are called AVL Trees (or height-balanced)
AVL TREES (dictionary)
-
AVL Tree:
- binary tree where each node has a balance factor of 0, 1 or -1
-
Balance Factor of node x:
- (height of lft subtree of x) - (height of rt subtree of x)
-
Constructor of AVL Tree:
- same as before (set root to 0)
-
DeleteComplete Tree:
- same as before (remove all nodes)
-
Search AVL Tree:
- same as before
-
Insert Function:
- now must be change to ensure the tree remains height-balanced after
item is inserted
-
Delete Function:
- must be designed to ensure the tree is balanced after item is removed
AVL TREES (cont.)
Example of a Balanced AVL Tree:
-
- Balance factor shown as node
0
-1
+1
0
+1
0
0
+1
0
0
0
+1
0
-1
0
AVL TREES (cont.)
• Example of an unbalanced AVL-Tree:
+2
+1
0
-2
0
-2
-1
0
IMPLEMENTATION OF AVL
SEARCH TREE
• will use same structure as binary tree:
typedef int Item;
struct TreeNode {
Item Value; //data
TreeNode *left; //pointer to left child
TreeNode *right; //pointer to right child
};
AVL TREE IMPLEMENTATION
(CONT.)
class AVLTree {
public:
AVLTree();
~AVLTree();
void Display();
void Insert(const Item &anItem);
bool Search(const Item &anItem);
bool Delete(const Item &anItem);
private:
TreeNode *root;
int balance_factor;
void InsertTree(TreeNode &root, const Item &anItem);
void DisplayTree(TreeNode root);
bool SearchTree(TreeNode root, const Item &anItem);
void DeleteCompleteTree();
bool DeleteTreeValue(TreeNode &root, const Item &anItem);
};
AVL MEMBER FUNCTIONS
- Constructor
- Same as before, but also sets balance factor; sets root
to null, indicating the tree is empty
AVLTree::AVLTree() {
root = 0;
balance_factor = 0;
}
- Search, Display and Delete:
- All same as before
AVL Member Functions (cont.)
- Insertion
- must now perform rotations on the tree to ensure that the balance
factor remains valid
- types of rotations:
- right rotation:
- used to insert a new item into left subtree of left child of parent with
balance factor +2 after the add
- steps:
1) reset link from parent of A to B
2) set left link of A = right link of B
3) set right link of B = A
- Example:
- Suppose the tree contains:
10
(+1
)
4
(0)
AVL TREES – INSERT (cont.)
- If we insert a node
containing the value 1:
10
(+2
)
4
(+1
)
1
(0)
- Tree now unbalanced
- Applying right-rotation:
4
(0)
1
(0)
10
(0)
AVL TREES-INSERT
(cont.)
•
Left-Rotation:
•
used when inserting new item into right subtree of right child B
making parent of B have balance factor –2
Steps:
•
1)
2)
3)
•
reset link of parent of A to B
set the right link of A = left link of B
set left link of B = A
Example:
•
Suppose the tree contains:
4
(0)
1
(0)
•
And we add: 2
3
10
(0)
AVL TREES – INSERT (cont.)
4
(+2
)
1
(2)
10
(0)
2
(1)
3
(0)
• Tree now unbalanced, so
4
(+1
)
do left-rotation:
2
(0)
• Balanced tree
1
(0)
10
(0)
3
(0)
AVL TREES – INSERT (cont.)
Left-Right Rotation:
-
left rotation followed by a right rotation
done when adding item into right subtree of left child B of
parent A produces +2
Steps:
1)
2)
3)
4)
5)
6)
left link of A = root C of right subtree of B
right link of B = left link of C
left link of C = B Then:
set link from parent of A to point to C
left link of A = right link of C
right link of C = A
AVL TREES – INSERT (cont.)
- Example: consider:
10
(+1
)
3
(0)
- We add 6, tree is unbalanced
10
(+2
)
3
(1)
6
(0)
AVL TREES – INSERT (cont.)
-
so must first do left-rotation:
10
(+2
)
6
(+1
)
3
(0)
-
Then, right rotation:
6
(0)
3
(0)
-
Tree now balanced
10
(0)
AVL TREES – INSERT (cont.)
-
-
left-right rotation may also occur when:
a) B has no right child before new node inserted as right child
of B (seen above)
b) B has a right child C, and new node inserted in left subtree of
C
c) B has a right child C, and new node inserted into right
subtree of C
example of b)
- Initial tree:
10
(+1
)
3
(0)
1
(0)
12
(0)
7
(0)
AVL TREES – INSERT (cont.)
-
we add 5:
10
(+2
)
3
(1)
1
(0)
12
(0)
7
(+1
)
5
(0)
AVL TREES – INSERT (cont.)
-
Tree now unbalanced, so do left-rotation:
10
(+2
)
7
(+2
)
3
(0)
1
(0)
5
(0)
12
(0)
AVL TREES – INSERT (cont.)
- Then a right-rotation (of entire tree):
7
(0)
3
(0)
1
(0)
10
(1)
5
(0)
12
(0)
- Tree is now balanced
- Right-left rotation is basically the same as above, except right
rotation occurs before left
AVL-TREES (SUMMARY)
- using AVL tree keeps the search time at O(log n) even as
tree grows
- may be costly if # of insertions is much larger than # of
searches, due to overhead in rebalancing trees
- studies have shown that:
1) on average, rebalancing required for 45% of insertions
2) half of those are double rotations
QUESTIONS?
• Next Section: Multiway Trees (B-Trees)
P. 855 - 873 (pay attention to 2-3-4 trees)