AVL tree self-adjusting tree Lai Ah Fur. AVL tree discovers: Adelson-Velskii and Landis balanced Binary search tree the depth of the tree: O(lg N) definition:

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

AVL Trees binary tree for every node x, define its balance factor
Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
CS Data Structures Chapter 10 Search Structures (Selected Topics)
8.1 何謂高度平衡二元搜尋樹 8.2 高度平衡二元搜尋樹的加入 8.3 高度平衡二元搜尋樹的刪除
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
Red Black Trees Colored Nodes Definition Binary search tree.
Chapter 4: Trees AVL Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
§4 AVL Trees Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root smalllarge Problem : Although T p = O( height ),
CS Data Structures Chapter 10 Search Structures.
Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
Balancing Trees Tricks to amaze your friends. Background BSTs where introduced because in theory they give nice fast search time. BSTs where introduced.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
Data Structures Chapter 10: Efficient Binary Search Trees 10-1.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
Bottom-Up Red-Black Trees Top-down red-black trees require O(log n) rotations per insert/delete. Color flips cheaper than rotations. Priority search trees.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
AVL Tree: Balanced Binary Search Tree 9.
AVL Trees CSE, POSTECH.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
AVL TREES.
Chapter 10 Search Structures
AVL Trees binary tree for every node x, define its balance factor
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Introduction Applications Balance Factor Rotations Deletion Example
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Chapter 29 AVL Trees.
Splay Trees Binary search trees.
AVL Trees CENG 213 Data Structures.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Advanced Associative Structures
The DSW Algorithm The building block for tree transformations in this algorithm is the rotation There are two types of rotation, left and right, which.
Tricks to amaze your friends
CS223 Advanced Data Structures and Algorithms
Balanced Binary Search Trees
AVL Search Tree put(9)
Lecture No.20 Data Structures Dr. Sohail Aslam
CS202 - Fundamental Structures of Computer Science II
AVL Tree By Rajanikanth B.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
AVL Trees Dynamic tables may also be maintained as binary search trees. Depending on the order of the symbols putting into the table, the resulting binary.
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
Tree Balancing: AVL Trees
Splay Trees Binary search trees.
Presentation transcript:

AVL tree self-adjusting tree Lai Ah Fur

AVL tree discovers: Adelson-Velskii and Landis balanced Binary search tree the depth of the tree: O(lg N) definition: An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can be differ by at most 1.

Balance factor (BF) 如果 node T 之 the height of left subtree is H l, the height of right subtree is H r, then the BF of node T is H l - H r 高度平衡之 BST: BF(t)<2 … for any node t AVL tree: BF(t)<2 Full binary tree: BF=? complete binary tree: BF=?

Right rotate LL 型

Left rotate RR 型

插入 第一種 LR 型

插入 第二種 LR 型

插入 第三種 LR 型 2 1

插入 第一種 RL 型

插入 第二種 RL 型

插入 第三種 RL 型

Del RR Del RL 刪除 7 或是

刪除 5 Del RL

Insertion case 1: an insertion into the left subtree of the left child of X case 2: an insertion into the right subtree of the left child of X case 3: an insertion into the left subtree of the right child of X case 4: an insertion into the right subtree of the right child of X

case 1: single rotation left subtree 較高.A single rotation switches the role of the parent and the child while maintaining the search order.rotate binary tree with left child static BinaryNode withLeftChild ( BinaryNode k2) { BinaryNode k1=k2.left; k2.left=k1.right; k1.right=k2; return k1; }

K2 K1 C B A K2 K1 C B A case 1 (single L-L rotation, 單一左左迴轉 ) single rotation

case k2 k1 BA c A K2 BC Insert “ 1 ”

case 2: double rotation (double L-R rotation) double rotate binary tree node: first left child with its right child; then, node k3 with new left child. static BinaryNode doubleWithLeftChild( BinaryNode k3) { k3.left=withRightChild(k3.left); return withLeftChild(k3); }

case 2: double rotation K3 K1 D A B K2 C K1 D C K3 K2 AB K3 K2 D A B K1 C +2 0

case 2: double rotation k2 k1 B A D A K2 B D Insert “ 5 ” k3 C C K3

case 3:double rotation (double R-L rotation) double rotate binary tree node: first right child with its left child; then, node k1 with new right child. static BinaryNode doubleWithRightChild( BinaryNode k1) { k1.right=withLeftChild(k1.right); return withRightChild(k1); }

case 3 K3 K1 D B K2 C K1 D C K3 K2 AB K1 K2 D B C A K3 A

R-L type Insert 85 or /

case 4: single rotation right subtree 較高 rotate binary tree with right child static BinaryNode withRightChild ( BinaryNode k1) { BinaryNode k2=k1.right; k1.right=k2.left; k2.left=k1; return k2; }

case 4 (single R-R rotation, 單一右右迴轉 ) K2 K1 C B A K2 K1 C B A single rotation -2 0

R-R type Insert

exercise Insert the following data into the empty AVL tree,

exercise Insert the following data into the empty AVL tree, “ Mar,May,Nov,Aug,Apr,Jane,Dec,July,Feb,June,Oct,Sept ”

3-1 May Nov AprMar +1 Jan +2 Insert Jan Aug 2 May Nov Aug Apr Insert Aug, Apr Mar 1 Nov -2 Insert Mar, May, Nov May 3-2 May Nov AprJan Mar Aug 4-1 May Nov Aug Apr Mar July Feb Jan Dec Insert Dec, July, Feb

4-2 May Nov Aug Apr Mar July Dec Jan Feb 5-1 May Nov Apr Mar July Dec Jan Feb Aug June +2 Insert June 5-2 May Nov Apr Mar JulyDec Jan Feb Aug June 6 May Nov Apr July Dec Feb Aug June Jan Mar Oct Insert Oct -2

The Answer: 7 Apr Dec Feb Aug June Jan Insert Sept July May Nov Mar Sept Oct

Jan Dec Mar AugFebJulyNov Apr JuneMay Oct Sept

After delete 40, … Double R-L rotations

After delete 85, … Double L-R rotations

After delete 40, … Single R-R rotations

After delete 85, … Single L-L rotations

After delete 85, … Single L-L rotations

How a newly arriving element enters … If a newly arriving element endangers the tree balance, how to rectify the problem immediately? By restructuring the tree locally (the AVL method) or by re-creating the tree (the DSW method)

self-adjusting tree But, not all elements are used with the same frequency. 在低層之 node 若 infrequently accessed, 則對於程式效能 影響不大 The strategy in self-adjusting tree is to restructure trees only by moving up the tree those elements that are used more often, creating a kind of “ priority tree ”

Self-restructuring tree Proposed by Brian Allen and Ian Munro and James Bitner Strategy: Single rotation: rotate a child about its parent if an element in a child is accessed unless it is the root Moving to the root: repeat the child- parent rotation until the element being accessed is in the root

splaying A modification of the “ move to the root ” Apply single rotation in pairs in an order depending on the links between the child, parent and grandparent. (node R is accessed) Case 1:node R ’ s parent is the root Case 2:homogeneous configuration: node R is the left child of its parent Q and Q is the left child of its parent P, or R and Q are both right children. Case 3: heterogeneous configuration: node R the right child of its parent Q and Q is the left child of its parent P, or R is the left child of Q and Q is the right child of P

3 cases of splaying Accessing node R

Examples of splaying Restructuring a tree with splaying (a-c) after accessing T and (c-d) then R

Algorithm of splaying Splaying(P,Q,R) while R is not the root if the R ’ s parent is the root perform a singular splay, rotate R about its parent; else if R is in homogeneous configuration with its predecessor perform a homoegeous splay, first rotate Q about P and then R about Q; else if R is in heterogeneous configuration with its predecessor perform a heterogeneous splay, first rotate R about Q and then about P;

The problem of splaying Splaying is a strategy focusing upon the elements rather than the shape of the tree. It may perform well in situation in which some elements are used much more frequently than others If the elements near the root are accessed with about the same frequency as elements on the lowest levels, then splaying may not be the best choice.

semisplaying A modification that requires only one rotation for a homogeneous splay and continues splaying with the parent of the accessed node.

Example of semisplaying (a)-(c) accessing T and restructuring the tree with semisplaying (c ) -(d)accessing T again