AVLTrees Part 2 AVL Trees Slide 2 Review
- Slides: 21
AVL-Trees (Part 2)
AVL Trees / Slide 2 Review * Trees Binary Search Trees AVL trees * Binary trees heaps : sorting and priority queue * Traversal (search, visit), insertion and deletion
AVL Trees / Slide 3 A warm-up exercise … * Create n a BST from a sequence, A, B, C, D, E, F, G, H * Create a AVL tree for the same sequence. . . ?
AVL Trees / Slide 4 More about Rotations When the AVL property is lost we can rebalance the tree via rotations *Single Right Rotation (SRR) n. Performed when A is unbalanced to the left (the left subtree is 2 higher than the right subtree) and B is leftheavy (the left subtree of B is 1 higher than the right subtree of B). T 2 and T 3 are the same height. T 1 is higher than T 2! A B SRR at A T 3 T 1 T 2 B T 1 A T 2 T 3
AVL Trees / Slide 5 Rotations * Single n Left Rotation (SLR) performed when A is unbalanced to the right (the right subtree is 2 higher than the left subtree) and B is right-heavy (the right subtree of B is 1 higher than the left subtree of B). A B SLR at A T 1 B T 2 T 3 A T 3 T 1 T 2
AVL Trees / Slide 6 Rotations * Double Left Rotation (DLR) Performed when C is unbalanced to the left (the left subtree is 2 higher than the right subtree), A is right -heavy (the right subtree of A is 1 higher than the left subtree of A) n Consists of a single left rotation at node A, followed by a single right at node C n C A A is balanced C SLR at A T 4 T 1 B T 2 T 3 B A T 4 T 3 T 1 T 2 Intermediate step, get B B SRR at C A C T 1 T 2 T 3 T 4 DLR = SLR + SRR T 1, T 2, T 3, T 4 are all of the same height!
AVL Trees / Slide 7 Rotations * Double Right Rotation (DRR) Performed when A is unbalanced to the right (the right subtree is 2 higher than the left subtree), C is left-heavy (the left subtree of C is 1 higher than the right subtree of C) n Consists of a single right rotation at node C, followed by a single left rotation at node A n A SRR at C T 1 C C is balanced! B A T 1 B T 4 T 2 T 3 B SLR at A A T 2 C T 3 T 4 C T 1 T 2 T 3 T 4 DRR = SRR + SLR
AVL Trees / Slide 8 Insertion Analysis log. N * Insert the new key as a new leaf just as in ordinary binary search tree: O(log. N) * Then trace the path from the new leaf towards the root, for each node x encountered: O(log. N) Check height difference: O(1) n If satisfies AVL property, proceed to next node: O(1) n If not, perform a rotation: O(1) n * The insertion stops when A single rotation is performed n Or, we’ve checked all nodes in the path n * Time complexity for insertion O(log. N)
AVL Trees / Slide 9 * Insertion Example Sequentially insert 3, 2, 1, 4, 5, 6 to an AVL Tree 3 2 1 2 Insert 3, 2 2 2 3 4 1 3 Single rotation Insert 4 3 3 1 Insert 5, 4 violation at node 3 4 5 4 4 1 5 3 1 1 Single rotation Insert 1 violation at node 3 2 2 5 Insert 6, violation at node 2 1 6 3 Single rotation 6
AVL Trees / Slide 10 * If we continue to insert 7, 16, 15, 14, 13, 12, 11, 10, 8, 9 4 4 5 2 1 3 6 2 1 6 Insert 7, violation at node 5 7 3 4 5 Insert 16, fine Insert 15 violation at node 7 6 2 3 7 Single rotation 4 1 5 1 7 16 15 3 5 Single rotation 15 But…. Violation remains 7 16
AVL Trees / Slide 11 * Restart our example We’ve inserted 3, 2, 1, 4, 5, 6, 7, 16 We’ll insert 15, 14, 13, 12, 11, 10, 8, 9 4 4 6 2 1 3 5 Insert 16, fine Insert 15 violation at node 7 6 2 7 k 1 1 3 16 k 3 Double rotation 15 k 2 5 7 16 k 1 k 3
AVL Trees / Slide 12 4 A 1 k 1 6 15 k 3 5 3 k 2 7 Insert 14 16 1 D 5 1 Insert 13 6 5 Y 16 7 k 2 7 3 14 Double rotation 14 C 2 6 k 1 15 k 3 3 4 k 1 X k 2 7 2 15 14 13 2 16 Z 15 4 1 6 3 5 14 13 16 Single rotation
AVL Trees / Slide 13 7 7 15 4 2 1 14 6 5 3 16 13 2 1 5 3 4 1 3 13 Insert 11 5 12 11 14 7 2 16 14 13 4 15 6 12 16 Single rotation 7 2 13 6 12 Insert 12 15 4 1 6 3 5 12 11 15 14 Single rotation 16
AVL Trees / Slide 14 7 7 13 4 2 1 12 6 5 3 Insert 10 15 11 13 4 14 2 16 1 5 3 11 5 10 8 9 16 13 4 13 6 3 12 14 7 4 1 10 15 Single rotation 10 7 2 11 6 2 15 12 14 16 1 11 6 3 5 8 9 Insert 8, fine then insert 9 15 12 14 10 Single rotation 16
AVL Trees / Slide 15 class AVL { public: Implementation: AVL(); AVL(const AVL& a); ~AVL(); bool empty() const; bool search(const double x); void insert(const double x); void remove(const double x); private: Struct Node { double element; Node* left; Node* right; Node* parent; Node(…) {…}; // constructuro for Node } Node* root; int height(Node* t) const; void insert(const double x, Node*& t) const; void single. Left. Rotation(Node*& k 2); void single. Right. Rotation(Node*& k 2); void double. Left. Rotation(Node*& k 3); void double. Right. Rotation(Node*& k 3); void delete(…) } // recursive function
AVL Trees / Slide 16 Deletion from AVL Tree (not required for this course!) * Delete a node x as in ordinary binary search tree n Note that the last (deepest) node in a tree deleted is a leaf or a node with one child * Then trace the path from the new leaf towards the root * For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. n If yes, proceed to parent(x) n If no, perform an appropriate rotation at x Continue trace the path until we reach the root to
AVL Trees / Slide 17 Deletion Example 1 20 20 10 5 15 35 25 15 18 10 40 30 38 45 50 Delete 5, Node 10 is unbalanced 35 18 25 40 30 38 45 50 Single Rotation
AVL Trees / Slide 18 Cont’d 35 20 15 10 35 18 25 20 40 30 38 15 45 50 Continue to check parents Oops!! Node 20 is unbalanced!! 10 40 38 25 18 30 Single Rotation For deletion, after rotation, we need to continue tracing upward to see if AVL-tree property is violated at other node. Different from insertion! 45 50
AVL Trees / Slide 19 Rotation in Deletion The rotation strategies (single or double) we learned can be reused here * Except for one new case: two subtrees of y are of the same height * rotate with left child rotate with right child
AVL Trees / Slide 20 Deletion Example 2 Right most child of left subtree Double rotation
AVL Trees / Slide 21 Example 2 Cont’d New case
- What is heel and toe polka
- Chapter review motion part a vocabulary review answer key
- Factoring polynomials
- Explain threaded binary tree
- Zegour
- árvore avl simulador
- Avl tree applet
- Avl tree animation
- Split avl tree
- Single rotation in avl tree
- Avl insertion example
- Razes avl
- Arbre avl
- Arbres avl
- Arbre avl algorithme
- Avl tree complexity
- Avl
- Avl tree demo
- Avl delete
- Avl tree of months
- Binary search
- Alabama virtual libary