Download presentation
Presentation is loading. Please wait.
Published bySusanto Makmur Modified over 6 years ago
1
Red-Black Trees 2018年11月26日3时46分 AVL Trees 6 3 8 4 v z AVL Trees
2
Time Complexity of Insert, Deletion, and Search operations
Red-Black Trees 2018年11月26日3时46分 Time Complexity of Insert, Deletion, and Search operations Insert Deletion Search Vector/List (unsorted) O(1) O(n) Binary Search Tree O(h) Balanced Binary Tree O(log n) AVL Trees
3
Balanced Binary Tree There are many kinds of balanced tree
Red-Black Trees 2018年11月26日3时46分 Balanced Binary Tree There are many kinds of balanced tree The definition of balanced binary tree A binary tree with height O(log n) or exactly log n AVL Trees
4
AVL Tree Definition AVL trees are balanced.
Red-Black Trees 2018年11月26日3时46分 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children o f v can differ by at most 1. An example of an AVL tree where the heights are shown next to the nodes: AVL Trees
5
Red-Black Trees 2018年11月26日3时46分 3 4 n(1) n(2) Height of an AVL Tree Fact: The height of an AVL tree storing n keys is O(log n). Proof: Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h. We easily see that n(1) = 1 and n(2) = 2 For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height h-1 and another of height h-2. That is, n(h) = 1 + n(h-1) + n(h-2) Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction), n(h) > 2in(h-2i) Solving the base case we get: n(h) > 2 h/2-1 Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL tree is O(log n) AVL Trees
6
Insertion in an AVL Tree
Red-Black Trees 2018年11月26日3时46分 Insertion in an AVL Tree Insertion is as in a binary search tree Always done by expanding an external node. Example: 44 17 78 32 50 88 48 62 44 17 78 32 50 88 48 62 54 Inserted node before insertion after insertion AVL Trees
7
Insertion in an AVL Tree
Red-Black Trees 2018年11月26日3时46分 Insertion in an AVL Tree Let z be the first unbalanced node encountered while traveling up the tree from w. y be the child of z with the larger height, and x be the child of y with the larger height. 44 17 78 32 50 88 48 62 54 c=z a=y b=x w AVL Trees
8
Trinode Restructuring
Red-Black Trees 2018年11月26日3时46分 Trinode Restructuring let (a,b,c) be an inorder listing of x, y, z perform the rotations needed to make b the topmost node of the three (other two cases are symmetrical) c=y b=x a=z T0 T1 T2 T3 Case 2: double rotation (a right rotation about c, then a left rotation about a) b=y a=z c=x T0 T1 T2 T3 b=y a=z c=x T0 T1 T2 T3 b=x c=y a=z T0 T1 T2 T3 Case 1: single rotation (a left rotation about a) AVL Trees
9
Restructuring (as Single Rotations)
Red-Black Trees 2018年11月26日3时46分 Restructuring (as Single Rotations) Single Rotations: T 3 2 a = x b = y c = z 1 single rotation AVL Trees T 1
10
Restructuring (as Double Rotations)
Red-Black Trees 2018年11月26日3时46分 Restructuring (as Double Rotations) double rotations: T 1 a = z b = x 2 3 c = y a = z b = x c = y T 2 1 3 T 2 3 1 a = z b = x c = y double rotation AVL Trees
11
Restructuring (as Double Rotations)
Red-Black Trees 2018年11月26日3时46分 Restructuring (as Double Rotations) double rotations: AVL Trees
12
Insertion Example, continued
Red-Black Trees 2018年11月26日3时46分 Insertion Example, continued unbalanced... 4 T 1 44 x 2 3 17 62 y z 1 2 2 32 50 78 1 1 1 ...balanced 48 54 88 T 2 T T 1 T AVL Trees 3
13
Red-Black Trees 2018年11月26日3时46分 Removal in an AVL Tree Removal begins as in a binary search tree, which means the node removed will become an empty external node. Its parent, w, may cause an imbalance. Example: 44 17 78 32 50 88 48 62 54 44 17 62 50 78 48 54 88 before deletion of 32 after deletion AVL Trees
14
Rebalancing after a Removal
Red-Black Trees 2018年11月26日3时46分 Rebalancing after a Removal Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with the larger height, and let x be the child of y with the larger height. We perform restructure(x) to restore balance at z. As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached 62 a=z 44 44 78 w 17 62 b=y 17 50 88 50 78 c=x 48 54 48 54 88 AVL Trees
15
Running Times for AVL Trees
Red-Black Trees 2018年11月26日3时46分 Running Times for AVL Trees a single restructure is O(1) using a linked-structure binary tree find is O(log n) height of tree is O(log n), no restructures needed insert is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n) remove is O(log n) AVL Trees
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.